Skip to content

Instantly share code, notes, and snippets.

@onimenotsuki
Last active February 10, 2021 17:40
Show Gist options
  • Save onimenotsuki/68a3d500e656f4240de69115eb55b339 to your computer and use it in GitHub Desktop.
Save onimenotsuki/68a3d500e656f4240de69115eb55b339 to your computer and use it in GitHub Desktop.
const Person = require('../../src/api/models/person');
const dbHandler = require('../db-handler');
// Connect to a new in-memory database before running any test
beforeAll(async () => await dbHandler.connect());
// Clear all test data after every test
afterEach(async () => await dbHandler.clearDatabase());
// Remove and close the db and server
afterAll(async () => await dbHandler.closeDatabase());
describe('MODEL :: Person', () => {
const personObj = {
mobilePhoneNumber: '9515041726',
firstName: 'Edgar',
lastName: 'Talledos',
email: 'edgar@evolutel.com.mx',
};
let person;
beforeEach(async () => {
person = await Person.create(personObj);
});
describe('find', () => {
it('should find person by _id', async () => {
const personRecord = await Person.findById(person._id);
expect(personRecord).toMatchObject(personObj);
});
it('should find person by _id and return fullName', async () => {
const personRecord = await Person.findById(person._id);
expect(personRecord.fullName).toMatch(
`${personObj.firstName} ${person.lastName}`,
);
});
});
describe('create', () => {
it('should not throw a error', () => {
expect(() => person).not.toThrow();
});
it('should create person', () => {
expect(person).toMatchObject({
mobilePhoneNumber: '9515041726',
firstName: 'Edgar',
lastName: 'Talledos',
email: 'edgar@evolutel.com.mx',
});
});
it('should mobilePhoneNumber be unique', async () => {
async function createAnotherPerson() {
await Person.create({
mobilePhoneNumber: '9515041726',
firstName: 'Second',
lastName: 'User',
email: 'edgar@evolutel.com.mx',
});
}
await expect(otherPerson()).rejects.toThrow(/duplicate/i);
});
});
});
const dbHandler = require('../db-handler');
const User = require('../../src/api/models/user');
// Connect to a new in-memory database before running any test
beforeAll(async () => await dbHandler.connect());
// Clear all test data after every test
afterEach(async () => await dbHandler.clearDatabase());
// Remove and close the db and server
afterAll(async () => await dbHandler.closeDatabase());
describe('USER', () => {
describe('create', () => {
let user;
beforeEach(async () => {
user = await User.create({
username: 'edgartalledos',
phoneNumber: '9515041726',
firstName: 'Edgar',
lastName: 'Talledos',
password: 'Password12345*',
email: 'edgar@evolutel.com.mx',
});
});
it('should not throw a error', async () => {
expect(() => user).not.toThrow();
});
it('should create user', async () => {
expect(user).toMatchObject({
username: 'edgartalledos',
phoneNumber: '9515041726',
firstName: 'Edgar',
lastName: 'Talledos',
password: 'Password12345*',
email: 'edgar@evolutel.com.mx',
isBot: false,
});
});
it('should email be unique', async () => {
const secondUser = await User.create({
username: 'seconduser',
phoneNumber: '9515041721',
firstName: 'Second',
lastName: 'User',
password: 'Password12345*',
email: 'edgar@evolutel.com.mx',
});
expect(() => secondUser).toThrow(/duplicate key/gi);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment