Skip to content

Commit fd98175

Browse files
committed
clean up tests by migrating frequent mocks to mock files
1 parent b0548ea commit fd98175

File tree

6 files changed

+36
-36
lines changed

6 files changed

+36
-36
lines changed

server/controllers/user.controller/__tests__/authManagement/3rdPartyManagement.test.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@ jest.mock('../../helpers', () => ({
99
...jest.requireActual('../../helpers'),
1010
saveUser: jest.fn()
1111
}));
12-
jest.mock('../../../../utils/mail', () => ({
13-
mailerService: {
14-
send: jest.fn()
15-
}
16-
}));
12+
jest.mock('../../../../utils/mail');
1713

1814
describe('user.controller > auth management > 3rd party auth', () => {
1915
let request: any;

server/controllers/user.controller/__tests__/authManagement/passwordManagement.test.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,7 @@ import { mailerService } from '../../../../utils/mail';
1414
import { UserDocument } from '../../../../types';
1515

1616
jest.mock('../../../../models/user');
17-
jest.mock('../../../../utils/mail', () => ({
18-
mailerService: {
19-
send: jest.fn()
20-
}
21-
}));
17+
jest.mock('../../../../utils/mail');
2218
jest.mock('../../helpers', () => ({
2319
...jest.requireActual('../../helpers'),
2420
generateToken: jest.fn()

server/controllers/user.controller/__tests__/authManagement/updateSettings.test.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,7 @@ import { mailerService } from '../../../../utils/mail';
1010
import { UserDocument } from '../../../../types';
1111

1212
jest.mock('../../../../models/user');
13-
jest.mock('../../../../utils/mail', () => ({
14-
mailerService: {
15-
send: jest.fn()
16-
}
17-
}));
13+
jest.mock('../../../../utils/mail');
1814
jest.mock('../../helpers', () => ({
1915
...jest.requireActual('../../helpers'),
2016
saveUser: jest.fn(),
@@ -114,10 +110,12 @@ describe('user.controller > auth management', () => {
114110
});
115111
describe('and when there is a currentPassword in the request', () => {
116112
describe('and the current password does not match', () => {
113+
beforeEach(async () => {});
117114
it('returns 401 with a "current password invalid" message', () => {});
118115
it('does not save the user with the new password', () => {});
119116
});
120117
describe('and when the current password does match', () => {
118+
beforeEach(async () => {});
121119
it('calls saveUser with the new password', () => {});
122120
});
123121
});

server/controllers/user.controller/__tests__/signup.test.ts

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,8 @@ import {
1212
import { mailerService } from '../../../utils/mail';
1313

1414
jest.mock('../../../models/user');
15-
jest.mock('../../../utils/mail', () => ({
16-
mailerService: {
17-
send: jest.fn()
18-
}
19-
}));
20-
jest.mock('../../../views/mail', () => ({
21-
renderEmailConfirmation: jest
22-
.fn()
23-
.mockReturnValue({ to: '[email protected]', subject: 'Confirm' })
24-
}));
15+
jest.mock('../../../utils/mail');
16+
jest.mock('../../../views/mail');
2517

2618
describe('user.controller > signup', () => {
2719
let request: any;
@@ -89,17 +81,19 @@ describe('user.controller > signup', () => {
8981

9082
describe('duplicateUserCheck', () => {
9183
it('calls findByEmailOrUsername with the correct params', async () => {
92-
const mockFind = jest.fn().mockResolvedValue(null);
93-
User.findByEmailOrUsername = mockFind;
84+
User.findByEmailOrUsername = jest.fn().mockResolvedValue(null);
9485

9586
request.query = { check_type: 'email', email: '[email protected]' };
9687

9788
await duplicateUserCheck(request, response, next);
9889

99-
expect(mockFind).toHaveBeenCalledWith('[email protected]', {
100-
caseInsensitive: true,
101-
valueType: 'email'
102-
});
90+
expect(User.findByEmailOrUsername).toHaveBeenCalledWith(
91+
92+
{
93+
caseInsensitive: true,
94+
valueType: 'email'
95+
}
96+
);
10397
});
10498

10599
it('returns the correct response body when no matching user is found', async () => {
@@ -254,7 +248,7 @@ describe('user.controller > signup', () => {
254248
User.findById = jest
255249
.fn()
256250
.mockReturnValue({ exec: jest.fn().mockResolvedValue(mockUser) });
257-
(mailerService.send as jest.Mock).mockResolvedValue(true);
251+
mailerService.send = jest.fn().mockResolvedValue(true);
258252

259253
request.user = { id: 'user1' };
260254
request.headers.host = 'localhost:3000';
@@ -263,7 +257,7 @@ describe('user.controller > signup', () => {
263257

264258
expect(User.findById).toHaveBeenCalledWith('user1');
265259
expect(mailerService.send).toHaveBeenCalledWith(
266-
expect.objectContaining({ to: '[email protected]' })
260+
expect.objectContaining({ subject: 'Mock confirm your email' }) // see views/__mocks__/mail.ts
267261
);
268262
expect(mockUser.verified).toBe('resent');
269263
expect(mockUser.verifiedToken).toBeDefined();
@@ -290,9 +284,9 @@ describe('user.controller > signup', () => {
290284
User.findById = jest
291285
.fn()
292286
.mockReturnValue({ exec: jest.fn().mockResolvedValue(mockUser) });
293-
(mailerService.send as jest.Mock).mockRejectedValue(
294-
new Error('Mailer fail')
295-
);
287+
mailerService.send = jest
288+
.fn()
289+
.mockRejectedValue(new Error('Mailer fail'));
296290

297291
request.user = { id: 'user1' };
298292
request.headers.host = 'localhost:3000';

server/utils/__mocks__/mail.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export const mailerService = {
2+
send: jest.fn().mockResolvedValue({ success: true }),
3+
sendMail: jest.fn().mockResolvedValue({ success: true })
4+
};

server/views/__mocks__/mail.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export const renderAccountConsolidation = jest.fn().mockReturnValue({
2+
3+
subject: 'Mock consolidate your email'
4+
});
5+
export const renderResetPassword = jest.fn().mockReturnValue({
6+
7+
subject: 'Mock reset your password'
8+
});
9+
export const renderEmailConfirmation = jest.fn().mockReturnValue({
10+
11+
subject: 'Mock confirm your email'
12+
});

0 commit comments

Comments
 (0)