Skip to content

Commit eeaa29a

Browse files
committed
add remaining tests for helpers
1 parent 3d78cdc commit eeaa29a

3 files changed

Lines changed: 60 additions & 15 deletions

File tree

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

Lines changed: 59 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
/* eslint-disable no-unused-vars */
22
import crypto from 'crypto';
33

4-
import { userResponse, generateToken } from '../helpers';
4+
import { Response as MockResponse } from 'jest-express/lib/response';
5+
import { Response } from 'express';
6+
import { userResponse, generateToken, userExists, saveUser } from '../helpers';
57
import { createMockUser } from '../__testUtils__';
8+
import { User } from '../../../models/user';
9+
import { UserDocument } from '../../../types';
610

711
jest.mock('../../../models/user');
812

@@ -15,20 +19,19 @@ const mockFullUser = createMockUser({
1519
banned: true
1620
});
1721

22+
const {
23+
name,
24+
tokens,
25+
password,
26+
resetPasswordToken,
27+
banned,
28+
...sanitised
29+
} = mockFullUser;
30+
1831
describe('user.controller > helpers', () => {
1932
describe('userResponse', () => {
2033
it('returns a sanitized PublicUser object', () => {
2134
const result = userResponse(mockFullUser);
22-
23-
const {
24-
name,
25-
tokens,
26-
password,
27-
resetPasswordToken,
28-
banned,
29-
...sanitised
30-
} = mockFullUser;
31-
3235
expect(result).toMatchObject(sanitised);
3336
});
3437
});
@@ -54,4 +57,49 @@ describe('user.controller > helpers', () => {
5457
spy.mockRestore();
5558
});
5659
});
60+
61+
describe('saveUser', () => {
62+
it('returns a response with a sanitised user if user.save succeeds', async () => {
63+
const userWithSuccessfulSave = {
64+
...mockFullUser,
65+
save: jest.fn().mockResolvedValue(null)
66+
};
67+
const response = new MockResponse();
68+
await saveUser(
69+
(response as unknown) as Response,
70+
(userWithSuccessfulSave as unknown) as UserDocument
71+
);
72+
expect(response.json).toHaveBeenCalledWith(sanitised);
73+
});
74+
it('returns a 500 Error if user.save fails', async () => {
75+
const userWithUnsuccessfulSave = {
76+
...mockFullUser,
77+
save: jest.fn().mockRejectedValue('async error')
78+
};
79+
const response = new MockResponse();
80+
await saveUser(
81+
(response as unknown) as Response,
82+
(userWithUnsuccessfulSave as unknown) as UserDocument
83+
);
84+
expect(response.status).toHaveBeenCalledWith(500);
85+
expect(response.json).toHaveBeenCalledWith({
86+
error: 'async error'
87+
});
88+
});
89+
});
90+
91+
describe('userExists', () => {
92+
it('returns true when User.findByUsername returns non-nullish', async () => {
93+
User.findByEmailOrUsername = jest
94+
.fn()
95+
.mockResolvedValue({ id: 'something' });
96+
const exists = await userExists('someusername');
97+
expect(exists).toBe(true);
98+
});
99+
it('returns false when User.findByUsername returns nullish', async () => {
100+
User.findByEmailOrUsername = jest.fn().mockResolvedValue(null);
101+
const exists = await userExists('someusername');
102+
expect(exists).toBe(false);
103+
});
104+
});
57105
});

server/controllers/user.controller/authManagement.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { RequestHandler } from 'express';
2-
import * as core from 'express-serve-static-core';
32
import { User } from '../../models/user';
43
import { saveUser, generateToken, userResponse } from './helpers';
54
import {

server/controllers/user.controller/helpers.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,8 @@ export async function saveUser(res: Response, user: UserDocument) {
6161

6262
/**
6363
* Helper used in other controllers to check if user by username exists.
64-
* @param {string} username
65-
* @return {Promise<boolean>}
6664
*/
67-
export async function userExists(username: string) {
65+
export async function userExists(username: string): Promise<boolean> {
6866
const user = await User.findByUsername(username);
6967
return user != null;
7068
}

0 commit comments

Comments
 (0)