|
@@ -1,38 +1,19 @@
|
|
|
-import routeGen from './routeGen'
|
|
|
-import { model, agency } from './_samples'
|
|
|
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 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
|
|
@@ -42,9 +23,11 @@ class Result {
|
|
|
this._status = value
|
|
|
}
|
|
|
send (data) {
|
|
|
+ console.log('SEND', data)
|
|
|
this._body = data
|
|
|
}
|
|
|
json (data) {
|
|
|
+ console.log('JSON', data)
|
|
|
this._status = 200
|
|
|
this._body = data
|
|
|
}
|
|
@@ -60,30 +43,55 @@ describe('routeGen for versioned models', () => {
|
|
|
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)
|
|
|
+ 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)
|
|
|
+ })
|
|
|
})
|
|
|
- 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)
|
|
|
+ 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)
|
|
|
+ })
|
|
|
})
|
|
|
})
|