Skip to content

Instantly share code, notes, and snippets.

@mmanishh
Created September 8, 2022 21:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mmanishh/df6f88c78e3adb65f15fb9b1a987bf77 to your computer and use it in GitHub Desktop.
Save mmanishh/df6f88c78e3adb65f15fb9b1a987bf77 to your computer and use it in GitHub Desktop.
/* 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: 'manish@gmail.com',
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: 'john@gmail.com',
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: 'test@gmail.com',
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