-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdatabaseController.test.js
35 lines (27 loc) · 1.05 KB
/
databaseController.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
const { registerUser } = require('./backend/controllers/databaseController');
const { User } = require('./backend/models/userModel');
const jwt = require('jsonwebtoken');
// Mocking dependencies
jest.mock('./backend/models/userModel');
jest.mock('jsonwebtoken');
describe('databaseController.registerUser', () => {
let req, res, next;
beforeEach(() => {
req = { body: {}, cookies: {} };
res = { locals: { username: 'testuser', accessToken: 'access', refreshToken: 'refresh' } };
next = jest.fn();
});
test('should update user profile when user already exists', async () => {
jwt.verify
.mockReturnValueOnce({ username: 'testuser' })
.mockReturnValueOnce({ accessToken: 'access' })
.mockReturnValueOnce({ refreshToken: 'refresh' });
User.findOne.mockResolvedValue({ username: 'testuser' });
await registerUser(req, res, next);
expect(User.findOneAndUpdate).toHaveBeenCalledWith(
{ username: 'testuser' },
{ refresh_token: 'refresh' },
{ new: true },
);
expect(next).toHaveBeenCalled();
});