1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- import routeGen from './routeGen'
- import { model, agency } from './_samples'
- import mongoose from 'mongoose'
- class Model {
- constructor () {
- this.modelName = 'Gisele'
- }
- static find (criteria, options, callback) {
- let { skip, limit } = options
- const myModel = model.slice(skip, skip + limit)
- if (myModel) {
- callback(undefined, myModel)
- } else {
- callback(Error('model not found'), undefined)
- }
- }
- static findOne (criteria, callback) {
- let { _id } = criteria
- const myModel = model.find((m) => {
- return (mongoose.Types.ObjectId(m._id) === _id)
- })
- if (myModel) {
- callback(undefined, myModel)
- } else {
- callback(Error('model not found'), undefined)
- }
- }
- save () {
- throw Error('save not implemented.')
- }
- remove () {
- throw Error('remove not implemented.')
- }
- }
- class Result {
- constructor () {
- this._body = null
- this._status = null
- }
- status (value) {
- this._status = value
- }
- send (data) {
- this._body = data
- }
- json (data) {
- this._status = 200
- this._body = data
- }
- }
- describe('routeGen for versioned models', () => {
- const route = routeGen(Model)
- const res = new Result()
- it('generates the handlers', () => {
- // Generate a version controlled model
- expect(route.get).not.toBeUndefined()
- expect(route.post).not.toBeUndefined()
- expect(route.put).not.toBeUndefined()
- expect(route.del).not.toBeUndefined()
- })
- it('get without arguments calls Model.find with the right parameters', () => {
- // call the GET handler
- const reqList = { params: {}, query: { limit: 2, offset: 1 } }
- route.get(reqList, res)
- expect(res._body).toEqual(model.slice(1, 3))
- expect(res._status).toBe(200)
- })
- it('return empty lists.', () => {
- // call the GET handler
- const reqList = { params: {}, query: { limit: 1, offset: 100 } }
- route.get(reqList, res)
- expect(res._body).toEqual([])
- expect(res._status).toBe(200)
- })
- it('get with arguments calls Model.findOne with the right parameters', () => {
- const reqElem = { params: { id: '58d3d6883d34293254afae42' }, query: {} }
- route.get(reqElem, res)
- expect(res._body).toEqual(model[0])
- expect(res._status).toBe(200)
- })
- it('return error when item is not found', () => {
- const reqElem = { params: { id: '58d3d6883d34343254afae42' }, query: {} }
- route.get(reqElem, res)
- expect(res._body).toEqual(model[0])
- expect(res._status).toBe(200)
- })
- })
|