Skip to content

Commit

Permalink
fix admin-frontend unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
banders committed May 28, 2024
1 parent edcfc2e commit 2256211
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 38 deletions.
26 changes: 15 additions & 11 deletions admin-frontend/src/store/modules/__tests__/auth.spec.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { beforeEach, describe, expect, it, Mock, vi } from 'vitest';
import { createTestingPinia } from '@pinia/testing';
import { setActivePinia } from 'pinia';
import { authStore } from '../auth';
import { beforeEach, describe, expect, it, Mock, vi } from 'vitest';
import { LOCAL_STORAGE_KEY_JWT } from '../../../common/apiService';
import AuthService from '../../../common/authService';
import { authStore } from '../auth';
vi.mock('../../../common/authService', async (importOriginal) => {
const mod: any = await importOriginal();
const resp: any = {
default: {
...mod.default,
getAuthToken: vi.fn(),
refreshAuthToken: vi.fn()
}
refreshAuthToken: vi.fn(),
},
};
return resp;
});
Expand All @@ -20,19 +21,22 @@ describe('AuthStore', () => {
let pinia;

beforeEach(() => {

pinia = createTestingPinia({ stubActions: false, fakeApp: true, createSpy: vi.fn });
pinia = createTestingPinia({
stubActions: false,
fakeApp: true,
createSpy: vi.fn,
});
setActivePinia(pinia);

auth = authStore(pinia);
});
it('setJwtToken, if provided will set value to local storage', async () => {
await auth.setJwtToken('testToken');
expect(localStorage.getItem('jwtToken')).toBe('testToken');
expect(localStorage.getItem(LOCAL_STORAGE_KEY_JWT)).toBe('testToken');
});
it('setJwtToken, if not provided will remove value to local storage', async () => {
await auth.setJwtToken();
expect(localStorage.getItem('jwtToken')).toBeNull();
expect(localStorage.getItem(LOCAL_STORAGE_KEY_JWT)).toBeNull();
});
it('setCorrelationID, if provided will set value to local storage', async () => {
await auth.setCorrelationID('correlationID');
Expand All @@ -45,9 +49,9 @@ describe('AuthStore', () => {
it('getJwtToken, if token provided by API, should set in localStorage', async () => {
(AuthService.getAuthToken as Mock).mockResolvedValueOnce({
jwtFrontend: 'testToken',
correlationID: 'testCorrelationID'
} );
correlationID: 'testCorrelationID',
});
await auth.getJwtToken();
expect(localStorage.getItem('jwtToken')).toBeTruthy();
expect(localStorage.getItem(LOCAL_STORAGE_KEY_JWT)).toBeTruthy();
});
});
61 changes: 34 additions & 27 deletions admin-frontend/src/store/modules/__tests__/codeStore.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@ import { useCodeStore } from '../codeStore';
// ----------------------------------------------------------------------------

vi.mock('../../../common/apiService', async (importOriginal) => {
const mod: any = await importOriginal()
const mod: any = await importOriginal();
const resp = {
...mod,
default: {
...mod.default,
getEmployeeCountRanges: vi.fn(),
getNaicsCodes: vi.fn(),
}
}
},
};
return resp;
})
});

// ----------------------------------------------------------------------------
// Test Data
Expand All @@ -29,15 +30,15 @@ vi.mock('../../../common/apiService', async (importOriginal) => {
const testEmployeeCountRanges = [
{
employee_count_range_id: 'ea8b2547-4e93-4bfa-aec1-3e90f91027dd',
employee_count_range: '1-99'
employee_count_range: '1-99',
},
{
employee_count_range_id: 'c7e1c454-7db9-46c6-b250-1567a543d22f',
employee_count_range: '100-499'
employee_count_range: '100-499',
},
{
employee_count_range_id: '5f26cc90-7960-4e14-9700-87ecd75f0a0f',
employee_count_range: '500+'
employee_count_range: '500+',
},
];

Expand All @@ -53,53 +54,59 @@ const testNaicsCodes = [
{
naics_code: '3',
naics_label: 'test3',
}
]
},
];

// ----------------------------------------------------------------------------
// Test Suite
// ----------------------------------------------------------------------------

describe("CodeStore", () => {
describe('CodeStore', () => {
let codeStore;
let auth;
let pinia;

beforeEach(() => {

pinia = createTestingPinia({ stubActions: false, fakeApp: true, createSpy: vi.fn, });
setActivePinia(pinia)
pinia = createTestingPinia({
stubActions: false,
fakeApp: true,
createSpy: vi.fn,
});
setActivePinia(pinia);

auth = authStore(pinia);
codeStore = useCodeStore(pinia);
})
});

it('Fetches employee range count and naics code when auth status becomes true', async () => {

// Mock functions in the ApiService which normally make HTTP requests to the backend.
// Instead, have these functions return some test data without any HTTP requests.
(ApiService.getEmployeeCountRanges as Mock).mockResolvedValueOnce(testEmployeeCountRanges);
(ApiService.getEmployeeCountRanges as Mock).mockResolvedValueOnce(
testEmployeeCountRanges,
);
(ApiService.getNaicsCodes as Mock).mockResolvedValueOnce(testNaicsCodes);

// Setup "watches" on CodeStore state variables. When the values of these
// Setup "watches" on CodeStore state variables. When the values of these
// variables change, check that they are what we expect
const { naicsCodes, employeeCountRanges } = storeToRefs(codeStore)
const { naicsCodes, employeeCountRanges } = storeToRefs(codeStore);
watch(naicsCodes, (val) => {
expect(naicsCodes.value?.length).toBe(testNaicsCodes.length);
expect(naicsCodes.value[0].naics_code).toBe(testNaicsCodes[0].naics_code);
})
});
watch(employeeCountRanges, (val) => {
expect(employeeCountRanges.value?.length).toBe(testEmployeeCountRanges.length);
expect(employeeCountRanges.value[0].employee_count_range_id).toBe(testEmployeeCountRanges[0].employee_count_range_id);
})
expect(employeeCountRanges.value?.length).toBe(
testEmployeeCountRanges.length,
);
expect(employeeCountRanges.value[0].employee_count_range_id).toBe(
testEmployeeCountRanges[0].employee_count_range_id,
);
});

// Setting the JWT Token in the AuthService triggers a function inside CodeStore
// called fetchAllCodes(). This is the method that calls the ApiService to fetch
// data, and then sets the data in state variables: naicsCodes and employeeCountRanges.
// (The token isn't validated by auth.setJwtToken(), so even an invalid token should
// cause the events described above)
await auth.setJwtToken("fake token");

})

})
await auth.setJwtToken('fake token');
});
});

0 comments on commit 2256211

Please sign in to comment.