Skip to content

Commit

Permalink
add unit tests for admin functions
Browse files Browse the repository at this point in the history
Signed-off-by: Jason Sherman <[email protected]>
  • Loading branch information
usingtechnology committed Jun 21, 2024
1 parent 00f115f commit 0cb72ff
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 2 deletions.
73 changes: 73 additions & 0 deletions app/tests/unit/forms/admin/controller.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
const controller = require('../../../../src/forms/admin/controller');
const service = require('../../../../src/forms/admin/service');
const { getMockRes } = require('@jest-mock/express');

afterEach(() => {
jest.clearAllMocks();
});

const req = {
params: {},
Expand Down Expand Up @@ -53,3 +58,71 @@ describe('form controller', () => {
expect(service.listFormComponentsProactiveHelp).toBeCalledTimes(1);
});
});

describe('getExternalAPIs', () => {
it('should call service', async () => {
const { res, next } = getMockRes();
service.getExternalAPIs = jest.fn().mockReturnValue({});

await controller.getExternalAPIs(req, res, next);
expect(service.getExternalAPIs).toBeCalledTimes(1);
expect(res.status).toHaveBeenCalledWith(200);
expect(next).not.toHaveBeenCalled();
});

it('should call next when error', async () => {
const { res, next } = getMockRes();
service.getExternalAPIs = jest.fn().mockRejectedValueOnce(new Error('Async error message'));

await controller.getExternalAPIs(req, res, next);
expect(service.getExternalAPIs).toBeCalledTimes(1);
expect(res.status).not.toHaveBeenCalled();
expect(next).toBeCalledTimes(1);
});
});

describe('updateExternalAPI', () => {
it('should call service', async () => {
const { res, next } = getMockRes();
service.updateExternalAPI = jest.fn().mockReturnValue({});

await controller.updateExternalAPI(req, res, next);
expect(service.updateExternalAPI).toBeCalledTimes(1);
expect(res.status).toHaveBeenCalledWith(200);
expect(next).not.toHaveBeenCalled();
});

it('should call next when error', async () => {
const { res, next } = getMockRes();
service.updateExternalAPI = jest.fn().mockRejectedValueOnce(new Error('Async error message'));

await controller.updateExternalAPI(req, res, next);
expect(service.updateExternalAPI).toBeCalledTimes(1);

expect(res.status).not.toHaveBeenCalled();
expect(next).toBeCalledTimes(1);
});
});

describe('getExternalAPIStatusCodes', () => {
it('should call service', async () => {
const { res, next } = getMockRes();
service.getExternalAPIStatusCodes = jest.fn().mockReturnValue({});

await controller.getExternalAPIStatusCodes(req, res, next);
expect(service.getExternalAPIStatusCodes).toBeCalledTimes(1);
expect(res.status).toHaveBeenCalledWith(200);
expect(next).not.toHaveBeenCalled();
});

it('should call next when error', async () => {
const { res, next } = getMockRes();
service.getExternalAPIStatusCodes = jest.fn().mockRejectedValueOnce(new Error('Async error message'));

await controller.getExternalAPIStatusCodes(req, res, next);
expect(service.getExternalAPIStatusCodes).toBeCalledTimes(1);

expect(res.status).not.toHaveBeenCalled();
expect(next).toBeCalledTimes(1);
});
});
50 changes: 50 additions & 0 deletions app/tests/unit/forms/admin/service.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
const { MockModel, MockTransaction } = require('../../../common/dbHelper');
const service = require('../../../../src/forms/admin/service');

jest.mock('../../../../src/forms/common/models/tables/externalAPI', () => MockModel);
jest.mock('../../../../src/forms/common/models/tables/externalAPIStatusCode', () => MockModel);
jest.mock('../../../../src/forms/common/models/views/adminExternalAPI', () => MockModel);

beforeEach(() => {
MockModel.mockReset();
MockTransaction.mockReset();
});

afterEach(() => {
jest.restoreAllMocks();
});

describe('Admin service', () => {
it('createFormComponentsProactiveHelp()', async () => {
const formComponentsHelpInfo = {
Expand Down Expand Up @@ -116,4 +130,40 @@ describe('Admin service', () => {
// test cases
expect(fields).toEqual(formComponentsHelpInfo[1]);
});

it('getExternalAPIs should fetch data', async () => {
await service.getExternalAPIs({});
expect(MockModel.query).toBeCalledTimes(1);
});

it('updateExternalAPI should patch and fetch', async () => {
await service.updateExternalAPI('id', { code: 'APPROVED', allowSendUserToken: true });
expect(MockModel.query).toBeCalledTimes(3);
expect(MockModel.patchAndFetchById).toBeCalledTimes(1);
expect(MockModel.patchAndFetchById).toBeCalledWith('id', {
updatedBy: 'ADMIN',
code: 'APPROVED',
allowSendUserToken: true,
});
});

it('updateExternalAPI should patch and fetch and update user token fields', async () => {
await service.updateExternalAPI('id', { code: 'APPROVED', allowSendUserToken: false });
expect(MockModel.query).toBeCalledTimes(3);
expect(MockModel.patchAndFetchById).toBeCalledTimes(1);
// should also update user token fields...
expect(MockModel.patchAndFetchById).toBeCalledWith('id', {
updatedBy: 'ADMIN',
code: 'APPROVED',
allowSendUserToken: false,
sendUserToken: false,
userTokenHeader: null,
userTokenBearer: false,
});
});

it('getExternalAPIStatusCodes should fetch data', async () => {
await service.getExternalAPIStatusCodes();
expect(MockModel.query).toBeCalledTimes(1);
});
});
2 changes: 0 additions & 2 deletions app/tests/unit/forms/proxy/controller.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ jwtService.validateAccessToken = jest.fn().mockReturnValue(true);
jwtService.getBearerToken = jest.fn().mockReturnValue(bearerToken);
jwtService.getTokenPayload = jest.fn().mockReturnValue({ token: 'payload' });

//const mockInstance = axios.create();
//const mockAxios = new MockAdapter(mockInstance);
// Replace any instances with the mocked instance (a new mock could be used here instead):
jest.mock('axios');
axios.create.mockImplementation(() => axios);
Expand Down

0 comments on commit 0cb72ff

Please sign in to comment.