Skip to content

Commit

Permalink
Merge pull request #435 from DSACMS/cb3c/install-vitest-javascript-un…
Browse files Browse the repository at this point in the history
…it-testing

FFS-2455: Install vitest javascript unit testing and refactor JS code that calls Internal API's
  • Loading branch information
jeffcatania-usds authored Feb 7, 2025
2 parents 218cd58 + 9cd2f8a commit 44f2dc8
Show file tree
Hide file tree
Showing 19 changed files with 1,839 additions and 108 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/js-vitest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: Vitest Javascript Unit Tests

on:
pull_request:
paths: ['app/**']

jobs:
vitest:
name: Vitest
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Ruby & Javascript
uses: ./.github/actions/setup-languages
- name: Run Vitest
working-directory: app
run: npm run test
5 changes: 4 additions & 1 deletion app/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,7 @@ terraform.tfstate.backup
/config/credentials/production.key

#Profiler
/profiler
/profiler

# Vitest
.vitest
4 changes: 3 additions & 1 deletion app/app/javascript/controllers/cbv/employer_search.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Controller } from "@hotwired/stimulus"
import { loadPinwheel, initializePinwheel, fetchToken, trackUserAction } from "../../utilities/pinwheel"
import { loadPinwheel, initializePinwheel } from "../../utilities/pinwheel"
import { fetchToken } from '../../utilities/api';
import { trackUserAction } from '../../utilities/api';

export default class extends Controller {
static targets = [
Expand Down
19 changes: 19 additions & 0 deletions app/app/javascript/utilities/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { fetchInternal } from './fetchInternal';

export const PINWHEEL_USER_ACTION = '/api/pinwheel/user_action';
export const PINWHEEL_TOKENS_GENERATE = '/api/pinwheel/tokens';

export const trackUserAction = (eventName, attributes, scope="pinwheel") => {
return fetchInternal(PINWHEEL_USER_ACTION, {
method: 'post',
body: JSON.stringify({ [scope]: { event_name: eventName, attributes } }),
})
};

export const fetchToken = (response_type, id, locale) => {
return fetchInternal(PINWHEEL_TOKENS_GENERATE, {
method: 'post',
body: JSON.stringify({ response_type, id, locale }),
})
};

20 changes: 20 additions & 0 deletions app/app/javascript/utilities/fetchInternal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import CSRF from './csrf';


export const fetchInternal = (uri, params) => {
const defaultHeaders = {
'X-CSRF-Token': CSRF.token,
'Content-Type': 'application/json'
};

return fetch(uri, addHeadersToParams(defaultHeaders, params))
.then(response => response.json());
};
const addHeadersToParams = (defaultHeaders, params) => {
const requestedHeaders = params.headers;
params.headers = requestedHeaders ?
Object.assign({}, defaultHeaders, requestedHeaders) :
defaultHeaders;

return params;
};
20 changes: 20 additions & 0 deletions app/app/javascript/utilities/fetchInternalAPIService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import CSRF from './csrf';


export const fetchInternalAPIService = (uri, params) => {
const defaultHeaders = {
'X-CSRF-Token': CSRF.token,
'Content-Type': 'application/json'
};

return fetch(uri, _addHeadersToParams(defaultHeaders, params))
.then(response => response.json());
};
const _addHeadersToParams = (defaultHeaders, params) => {
const requestedHeaders = params.headers;
params.headers = requestedHeaders ?
Object.assign({}, defaultHeaders, requestedHeaders) :
defaultHeaders;

return params;
};
23 changes: 0 additions & 23 deletions app/app/javascript/utilities/pinwheel.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import loadScript from 'load-script';
import CSRF from './csrf';

const PINWHEEL_TOKENS_GENERATE = '/api/pinwheel/tokens';
const PINWHEEL_USER_ACTION = '/api/pinwheel/user_action';

export function loadPinwheel() {
return new Promise((resolve, reject) => {
Expand All @@ -25,24 +22,4 @@ export function initializePinwheel(Pinwheel, linkToken, callbacks) {
return Pinwheel;
}

export const trackUserAction = (eventName, attributes) => {
return fetch(PINWHEEL_USER_ACTION, {
method: 'post',
headers: {
'X-CSRF-Token': CSRF.token,
'Content-Type': 'application/json'
},
body: JSON.stringify({ pinwheel: { event_name: eventName, attributes } }),
}).then(response => response.json());
}

export const fetchToken = (response_type, id, locale) => {
return fetch(PINWHEEL_TOKENS_GENERATE, {
method: 'post',
headers: {
'X-CSRF-Token': CSRF.token,
'Content-Type': 'application/json'
},
body: JSON.stringify({ response_type, id, locale }),
}).then(response => response.json());
}
Loading

0 comments on commit 44f2dc8

Please sign in to comment.