12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- import router, { getHandler, postHandler, putHandler, deleteHandler } from './route'
- import samples from './_sampleData'
- import sinon from 'sinon'
- import mongoose from 'mongoose'
- jest.mock('./model')
- function ResClass () {
- this._status = null
- this._data = null
- this.status = (status) => { this._status = status }
- this.send = (data) => { this._data = data }
- this.json = (data) => {
- this._data = data
- this._status = 200
- }
- }
- describe('Route handlers', () => {
- it('handles the GET request for lists.', () => {
- // if no _id is passed, return a list of objects.
- // apply the limits correctly
- const reqList = { params: [], query: {limit: 2.4, offset: 1.2} }
- const res = new ResClass()
- const returnValue = getHandler(reqList, res)
- expect(returnValue).toBeUndefined()
- expect(res._data).toBe(samples)
- expect(res._status).toBe(200)
- expect(ProjectFind.called).toBe(true)
- expect(ProjectFind.firstCall.args[0]).toEqual({})
- expect(ProjectFind.firstCall.args[1]).toEqual({limit: 2, skip: 1})
- })
- it('handles the GET request for single documents.', () => {
- const reqOne = { params: ['/58d3d6883d34293254afae42'], query: undefined }
- const res = new ResClass()
- const returnValue = getHandler(reqOne, res)
- expect(returnValue).toBeUndefined()
- expect(res._data).toBe(samples[0])
- expect(res._status).toBe(200)
- expect(ProjectFindOne.called).toBe(true)
- expect(ProjectFindOne.firstCall.args[0]).toEqual({_id: mongoose.Types.ObjectId('58d3d6883d34293254afae42')})
- })
- it('handles the POST request.', () => {
- const reqOne = { params: ['/58d3d6883d34293254afae42'], query: undefined }
- const res = new ResClass()
- const returnValue = postHandler(reqOne, res)
- expect(returnValue).toBeUndefined()
- expect(res._data).toBe(samples[0])
- expect(res._status).toBe(200)
- expect(ProjectFindOne.called).toBe(true)
- expect(ProjectFindOne.firstCall.args[0]).toEqual({_id: mongoose.Types.ObjectId('58d3d6883d34293254afae42')})
- })
- })
|