12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- import mongoose from 'mongoose'
- const table = '__routeGenTest__'
- const db = `mongodb://localhost/${table}`
- mongoose.connect(db)
- import routeGen from './routeGen'
- import Model from './routeGenModel'
- import { model, agency, newModel } from './_samples'
- // jest.mock('./projects/model')
- // import Project from './projects/model'
- const spyFindOne = jest.spyOn(Model, 'findOne')
- const spyFind = jest.spyOn(Model, 'find')
- // const spySave = jest.spyOn(Model, 'save')
- const spyRemove = jest.spyOn(Model, 'remove')
- class Result {
- constructor () {
- this._body = null
- this._status = null
- }
- status (value) {
- this._status = value
- }
- send (data) {
- console.log('SEND', data)
- this._body = data
- }
- json (data) {
- console.log('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()
- })
- describe('get handler', () => {
- it('get without arguments returns a list of items', () => {
- // call the GET handler
- const reqList = { params: {}, query: { limit: 2.1, offset: 1.1 } }
- route.get(reqList, res)
- expect(res._body).toEqual(model.slice(1, 3))
- expect(res._status).toBe(200)
- expect(spyFind).toHaveBeenCalledWith({'history.head': true}, {limit: 2, skip: 1}, expect.anything())
- })
- 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 returns queried item by id', () => {
- const reqElem = { params: { id: '18a0d6883d34293254afae42' }, query: {} }
- route.get(reqElem, res)
- expect(res._body).toEqual(projects[0])
- expect(res._status).toBe(200)
- })
- it('get with arguments return queried item by tag (only HEAD in history)', () => {
- const reqElem = { params: { id: 'JU_CSD' }, query: {} }
- route.get(reqElem, res)
- expect(res._body).toEqual(projects[1])
- expect(res._status).toBe(200)
- })
- it('get returns error, when item is not found', () => {
- const reqElem = { params: { id: '99a0d6883d34293254afae42' }, query: {} }
- route.get(reqElem, res)
- expect(res._body).toEqual(Error('item not found'))
- expect(res._status).toBe(404)
- console.log(spyFind.mock, spyFindOne.mock)
- expect(spyFindOne.mock).toBe(true)
- })
- })
- describe('post handler', () => {
- it('post creates a new history object', () => {
- console.log(newModel)
- const reqElem = { body: newModel }
- route.post(reqElem, res)
- console.log(res)
- expect(res._body).toEqual({_id: expect.anything()})
- expect(res._status).toBe(200)
- console.log(res)
- route.get({params: {id: res._body}, query: {}}, res)
- // expect(res._body).toEqual(true)
- console.log(res)
- })
- })
- })
|