-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
82849d4
commit 8adf7e4
Showing
9 changed files
with
429 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,219 @@ | ||
import { contactService } from '../../../src/services'; | ||
import contactController from '../../../src/controllers/contact'; | ||
import { Request, Response } from 'express'; | ||
|
||
// Mock config library - @see {@link https://stackoverflow.com/a/64819698} | ||
jest.mock('config'); | ||
|
||
const mockResponse = () => { | ||
const res: { status?: jest.Mock; json?: jest.Mock; end?: jest.Mock } = {}; | ||
res.status = jest.fn().mockReturnValue(res); | ||
res.json = jest.fn().mockReturnValue(res); | ||
|
||
return res; | ||
}; | ||
|
||
let res = mockResponse(); | ||
beforeEach(() => { | ||
res = mockResponse(); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
const CURRENT_CONTEXT = { authType: 'BEARER', tokenPayload: null }; | ||
|
||
describe('contactController', () => { | ||
const next = jest.fn(); | ||
|
||
describe('searchContacts', () => { | ||
const searchContactsSpy = jest.spyOn(contactService, 'searchContacts'); | ||
|
||
it('should return 200 if all good', async () => { | ||
const req = { | ||
query: { userId: '5e3f0c19-8664-4a43-ac9e-210da336e923' }, | ||
currentContext: CURRENT_CONTEXT | ||
} as unknown as Request; | ||
|
||
const contacts = [ | ||
{ | ||
contactId: 'contact123', | ||
userId: 'user123', | ||
firstName: 'John', | ||
lastName: 'Doe', | ||
phoneNumber: '123-456-7890', | ||
email: '[email protected]', | ||
contactPreference: 'email', | ||
contactApplicantRelationship: 'applicant' | ||
} | ||
]; | ||
|
||
searchContactsSpy.mockResolvedValue(contacts); | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
await contactController.searchContacts(req as any, res as unknown as Response, next); | ||
|
||
expect(searchContactsSpy).toHaveBeenCalledTimes(1); | ||
expect(searchContactsSpy).toHaveBeenCalledWith({ | ||
userId: ['5e3f0c19-8664-4a43-ac9e-210da336e923'], | ||
contactId: undefined, | ||
email: undefined, | ||
firstName: undefined, | ||
lastName: undefined, | ||
contactApplicantRelationship: undefined, | ||
phoneNumber: undefined | ||
}); | ||
expect(res.status).toHaveBeenCalledWith(200); | ||
expect(res.json).toHaveBeenCalledWith(contacts); | ||
}); | ||
|
||
it('adds dashes to user IDs', async () => { | ||
const req = { | ||
query: { userId: '5e3f0c1986644a43ac9e210da336e923,8b9dedd279d442c6b82f52844a8e2757' }, | ||
currentContext: CURRENT_CONTEXT | ||
} as unknown as Request; | ||
|
||
const contacts = [ | ||
{ | ||
contactId: 'contact123', | ||
userId: 'user123', | ||
firstName: 'John', | ||
lastName: 'Doe', | ||
phoneNumber: '123-456-7890', | ||
email: '[email protected]', | ||
contactPreference: 'email', | ||
contactApplicantRelationship: 'applicant' | ||
} | ||
]; | ||
|
||
searchContactsSpy.mockResolvedValue(contacts); | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
await contactController.searchContacts(req as any, res as unknown as Response, next); | ||
|
||
expect(searchContactsSpy).toHaveBeenCalledTimes(1); | ||
expect(searchContactsSpy).toHaveBeenCalledWith({ | ||
userId: ['5e3f0c19-8664-4a43-ac9e-210da336e923', '8b9dedd2-79d4-42c6-b82f-52844a8e2757'], | ||
contactId: undefined, | ||
email: undefined, | ||
firstName: undefined, | ||
lastName: undefined, | ||
contactApplicantRelationship: undefined, | ||
phoneNumber: undefined | ||
}); | ||
}); | ||
|
||
it('adds dashes to contact IDs', async () => { | ||
const req = { | ||
query: { contactId: '5e3f0c1986644a43ac9e210da336e923,8b9dedd279d442c6b82f52844a8e2757' }, | ||
currentContext: CURRENT_CONTEXT | ||
} as unknown as Request; | ||
|
||
const contacts = [ | ||
{ | ||
contactId: 'contact123', | ||
userId: 'user123', | ||
firstName: 'John', | ||
lastName: 'Doe', | ||
phoneNumber: '123-456-7890', | ||
email: '[email protected]', | ||
contactPreference: 'email', | ||
contactApplicantRelationship: 'applicant' | ||
} | ||
]; | ||
|
||
searchContactsSpy.mockResolvedValue(contacts); | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
await contactController.searchContacts(req as any, res as unknown as Response, next); | ||
|
||
expect(searchContactsSpy).toHaveBeenCalledTimes(1); | ||
expect(searchContactsSpy).toHaveBeenCalledWith({ | ||
contactId: ['5e3f0c19-8664-4a43-ac9e-210da336e923', '8b9dedd2-79d4-42c6-b82f-52844a8e2757'], | ||
userId: undefined, | ||
email: undefined, | ||
firstName: undefined, | ||
lastName: undefined, | ||
contactApplicantRelationship: undefined, | ||
phoneNumber: undefined | ||
}); | ||
}); | ||
|
||
it('calls next if the contact service fails to list contacts', async () => { | ||
const req = { | ||
query: { userId: '5e3f0c19-8664-4a43-ac9e-210da336e923' }, | ||
currentContext: CURRENT_CONTEXT | ||
} as unknown as Request; | ||
|
||
searchContactsSpy.mockImplementationOnce(() => { | ||
throw new Error(); | ||
}); | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
await contactController.searchContacts(req as any, res as unknown as Response, next); | ||
|
||
expect(searchContactsSpy).toHaveBeenCalledTimes(1); | ||
expect(searchContactsSpy).toHaveBeenCalledWith({ | ||
userId: ['5e3f0c19-8664-4a43-ac9e-210da336e923'], | ||
contactId: undefined, | ||
email: undefined, | ||
firstName: undefined, | ||
lastName: undefined, | ||
contactApplicantRelationship: undefined, | ||
phoneNumber: undefined | ||
}); | ||
expect(res.status).toHaveBeenCalledTimes(0); | ||
expect(next).toHaveBeenCalledTimes(1); | ||
}); | ||
}); | ||
|
||
describe('updateContact', () => { | ||
const upsertContactsSpy = jest.spyOn(contactService, 'upsertContacts'); | ||
|
||
it('should return 200 if contact is updated successfully', async () => { | ||
const req = { | ||
body: { | ||
userId: '5e3f0c19-8664-4a43-ac9e-210da336e923', | ||
email: '[email protected]', | ||
firstName: 'First', | ||
lastName: 'Last' | ||
}, | ||
currentContext: CURRENT_CONTEXT | ||
} as unknown as Request; | ||
|
||
upsertContactsSpy.mockResolvedValue(); | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
await contactController.updateContact(req as any, res as unknown as Response, next); | ||
|
||
expect(upsertContactsSpy).toHaveBeenCalledTimes(1); | ||
expect(upsertContactsSpy).toHaveBeenCalledWith([req.body], req.currentContext); | ||
expect(res.status).toHaveBeenCalledWith(200); | ||
}); | ||
|
||
it('calls next if the contact service fails to update contact', async () => { | ||
const req = { | ||
body: { | ||
userId: '5e3f0c19-8664-4a43-ac9e-210da336e923', | ||
email: '[email protected]', | ||
firstName: 'First', | ||
lastName: 'Last' | ||
}, | ||
currentContext: CURRENT_CONTEXT | ||
} as unknown as Request; | ||
|
||
upsertContactsSpy.mockImplementationOnce(() => { | ||
throw new Error(); | ||
}); | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
await contactController.updateContact(req as any, res as unknown as Response, next); | ||
|
||
expect(upsertContactsSpy).toHaveBeenCalledTimes(1); | ||
expect(upsertContactsSpy).toHaveBeenCalledWith([req.body], req.currentContext); | ||
expect(res.status).toHaveBeenCalledTimes(0); | ||
expect(next).toHaveBeenCalledTimes(1); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.