Created
September 8, 2022 21:08
-
-
Save mmanishh/df6f88c78e3adb65f15fb9b1a987bf77 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* eslint-disable no-unused-vars */ | |
/* eslint-disable no-undef */ | |
const request = require('supertest'); | |
const { | |
expect, | |
describe, | |
it, | |
} = require('@jest/globals'); | |
const app = require('../src/app'); | |
// Mock the overall database layer (connection etc..) | |
jest.mock('sequelize', () => require('./_mocks/sequelize')); | |
// Mock the User model with some test data and add both model method overrides | |
// and model instance overrides for our test assertations | |
jest.mock('../src/models/user', () => () => { | |
const SequelizeMock = require('sequelize-mock'); | |
const crypto = require('crypto'); | |
const dbMock = new SequelizeMock(); | |
const userMockModel = dbMock.define('user'); | |
const users = [ | |
{ | |
id: '17ecdb90-dc9b-4b68-b8fb-ca4f7545ebc0', | |
name: 'Manish Maharjan', | |
email: '[email protected]', | |
role: 'user', | |
password: crypto.createHash('md5').update('hello@123').digest('hex'), | |
status: true, | |
createdAt: new Date(), | |
updatedAt: new Date(), | |
}, | |
{ | |
id: '7ed49084-650b-4906-8b5f-2d1a240cbe43', | |
name: 'John Bar', | |
email: '[email protected]', | |
password: 'hello@123', | |
role: 'user', | |
status: true, | |
createdAt: '2022-09-07T13:47:28.801Z', | |
updatedAt: '2022-09-07T14:49:31.567Z', | |
}]; | |
const userTestObject = userMockModel.build(users[0]); | |
userTestObject.update = (data) => { | |
userTestObject.isUpdated = true; | |
userTestObject.name = data.name; | |
return Promise.resolve(); | |
}; | |
userTestObject.destroy = () => { | |
userTestObject.isDestroyed = true; | |
return Promise.resolve(); | |
}; | |
const testModelInstances = [ | |
userTestObject, | |
userMockModel.build(users[1]), | |
]; | |
// Mock model method overrides for tests below | |
userMockModel.findAll = () => Promise.resolve(testModelInstances); | |
userMockModel.findOne = (query) => { | |
console.log(query) | |
const { where } = query; | |
const { email, id } = where; | |
if (email) return Promise.resolve(testModelInstances.find((e) => e._values.email === email)); | |
if (id) return Promise.resolve(testModelInstances.find((e) => e._values.id === id)); | |
}; | |
// userMockModel.findByPk = (query) => () => Promise.resolve(testModelInstances[0]); | |
userMockModel.findByPk = (query) => { | |
const { where } = query; | |
const { id } = where; | |
if (id) return Promise.resolve(testModelInstances.find((e) => e._values.id === id)); | |
}; | |
userMockModel.create = (data) => { | |
testModelInstances.push(data); | |
return Promise.resolve(); | |
}; | |
// Mock test helper methods | |
userMockModel.mockHelperGetLastCreated = () => testModelInstances[testModelInstances.length - 1]; | |
userMockModel.mockHelperIsUpdateCalled = () => testModelInstances[0].isUpdated; | |
userMockModel.mockHelperIsDestroyCalled = () => testModelInstances[0].isDestroyed; | |
return userMockModel; | |
}); | |
let authorizationToken; | |
let userData; | |
const userId = '17ecdb90-dc9b-4b68-b8fb-ca4f7545ebc0'; | |
const userPass = { | |
email: '[email protected]', | |
password: 'hello@123', | |
}; | |
const userDummy = { | |
name: 'Testing User', | |
role: 'user', | |
status: true, | |
...userPass, | |
}; | |
beforeAll(async () => { | |
jest.clearAllMocks(); | |
}); | |
describe('user-mock', () => { | |
test('register user with correct payload', async () => { | |
const res = await request(app) | |
.post('/api/v1/auth/register') | |
.send(userDummy); | |
expect(res.statusCode).toBe(200); | |
}); | |
test('get all users', async (done) => { | |
const res = await request(app) | |
.get('/api/v1/users'); | |
expect(res.statusCode).toBe(200); | |
expect(res.body.data.length).toEqual(3); | |
done(); | |
}); | |
test('get user by id', async (done) => { | |
const res = await request(app) | |
.get(`/api/v1/users/${userId}`); | |
expect(res.statusCode).toBe(200); | |
done(); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment