Created
November 12, 2020 12:45
-
-
Save icetbr/8edf6c92f30b759f0c03b2556cd4b922 to your computer and use it in GitHub Desktop.
This file contains 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
const db = require('./db'); | |
const server = require('./server'); | |
const sinon = require('sinon'); | |
test('POST /employees transforms the employee data to the legacy format, adds metadata, saves it to the database, saves a copy for history and returns the non transformed employee', async () => { | |
// given our current time | |
const now = new Date('2018-02-21T12:30:20.903Z'); | |
const clock = sinon.useFakeTimers({ now, toFake: ['Date']}); | |
// and a fresh database | |
await db.initialize(); | |
await db.collection('employees').deleteMany(); | |
// and a server listening for requests | |
await server.initialize(); | |
// and some employee data | |
const employee = { name: 'John', jobTitle: 'Programmer' }; | |
// when `POST` to /employees with the employee data | |
const response = await server.inject({ method: 'post', url: '/employees', payload: employee }); | |
const savedEmployees = await db.collection('employees').find().toArray(); | |
// then it transforms the employee to the legacy format | |
const transformedEmployee = { fullname: 'John', occupation: 'Programmer' }; | |
expect(savedEmployees).to.containSubset(transformedEmployee); | |
// and it adds metadata | |
const metadata = { | |
_id: response.result._id, | |
uniqueKey: response.result._id.toString(), | |
updatedAt: now, | |
updatedBy: 'mary@hr.com', | |
isDeleted: false, | |
}; | |
expect(savedEmployees).to.containSubset(transformedEmployee); | |
// and the employee data is saved to the database | |
expect(savedEmployees).to.deep.equal([savedEmployee()]); | |
// and a copy is saved for history | |
const savedEmployeesHistory = await db.collection('employees_history').find().toArray(); | |
const expectedSavedEmployee = { ...expectedEmployee, _id: savedEmployeesHistory[0]._id }; | |
expect(savedEmployeesHistory).to.deep.equal([expectedSavedEmployee]); | |
expect(savedEmployees[0]._id.toString()).to.not.equal(savedEmployeesHistory[0]._id.toString()); | |
// and the non transformed employee is returned | |
expect(response.result).to.deep.equal(returnedEmployee()); | |
// cleanup | |
clock.restore(); | |
await server.stop(); | |
await db.close(); | |
}); | |
test('POST /employees with a missing name returns an error 400', async () => { | |
// given a server listening for requests | |
await server.initialize(); | |
// and an employee with a missing name | |
const employee = { jobTitle: 'Programmer' }; | |
// when `POST` to /employees with the employee data | |
const response = await server.inject({ method: 'post', url: '/employees', payload: employee }); | |
// then an error 400 is returned | |
const errorBadRequest = { | |
statusCode: 400, | |
error: 'Bad Request', | |
message: '"name" is required', | |
}; | |
expect(response.result).to.deep.equal(errorBadRequest); | |
// cleanup | |
await server.stop(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment