Parcourir la source

unit tests work.

Tomi Cvetic il y a 8 ans
Parent
commit
e427f8e626
2 fichiers modifiés avec 35 ajouts et 11 suppressions
  1. 1 1
      core/RESTController.js
  2. 34 10
      core/RESTController.test.js

+ 1 - 1
core/RESTController.js

@@ -47,6 +47,7 @@ class RESTController {
   read (id) {
     const filter = {}
     filter[this.key] = id
+
     return this.model
       .findOne(filter)
       .then(instance => {
@@ -74,7 +75,6 @@ class RESTController {
     return this.model
       .findOne(filter)
       .then(instance => {
-        console.log(instance)
         for (let attribute in data) {
           if (data.hasOwnProperty(attribute) && attribute !== this.key && attribute !== '_id') {
             instance[attribute] = data[attribute]

+ 34 - 10
core/RESTController.test.js

@@ -5,10 +5,10 @@ import { Model } from '../routeGenModel'
 const spyFind = jest.spyOn(Model, 'find')
 const spyFindOne = jest.spyOn(Model, 'findOne')
 const spyCreate = jest.spyOn(Model, 'create')
+const spyUpdate = jest.spyOn(Model, 'update')
 const spyRemove = jest.spyOn(Model, 'remove')
 
 describe('REST Controller', () => {
-  mongoose.connect('mongodb://localhost/rest-controller')
   const ModelController = new RESTController(Model)
   const createdModel = {
     name: 'Gisele'
@@ -18,6 +18,15 @@ describe('REST Controller', () => {
   }
   let createdId = null
 
+  beforeAll(() => {
+    return mongoose.connect('mongodb://localhost/rest-controller')
+  })
+  afterAll(() => {
+    Model.remove({}, err => {
+      console.log(err)
+    })
+  })
+
   it('creates an element', () => {
     return ModelController.create(createdModel)
       .then(response => {
@@ -30,7 +39,7 @@ describe('REST Controller', () => {
         expect(copy).toMatchObject({__v: 0, ...createdModel})
         expect(spyCreate).toHaveBeenCalledWith(createdModel)
         expect(spyCreate).toHaveBeenCalledTimes(1)
-        spyCreate.mockReset()
+        spyCreate.mockClear()
       })
   })
 
@@ -45,7 +54,7 @@ describe('REST Controller', () => {
         expect(copy).toMatchObject({__v: 0, ...createdModel})
         expect(spyFindOne).toHaveBeenCalledWith({ _id: createdId })
         expect(spyFindOne).toHaveBeenCalledTimes(1)
-        spyFindOne.mockReset()
+        spyFindOne.mockClear()
       })
   })
 
@@ -58,10 +67,10 @@ describe('REST Controller', () => {
           copy[key] = response.model[0][key]
         })
         expect(copy).toMatchObject({__v: 0, ...createdModel})
-        expect(response.model).toHaveLength(2)
+        expect(response.model).toHaveLength(1)
         expect(spyFind).toHaveBeenCalledWith({})
         expect(spyFind).toHaveBeenCalledTimes(1)
-        spyFind.mockReset()
+        spyFind.mockClear()
       })
   })
 
@@ -71,12 +80,27 @@ describe('REST Controller', () => {
         // Check the copy due to a bug with jest.
         const copy = {}
         Object.keys(Model.schema.paths).forEach(key => {
-          copy[key] = response.model[0][key]
+          copy[key] = response.model[key]
         })
-        expect(copy).toMatchObject({__v: 0, ...updatedModel})
-        expect(spyFind).toHaveBeenCalledWith({})
-        expect(spyFind).toHaveBeenCalledTimes(1)
-        spyFind.mockReset()
+        expect(copy).toMatchObject({ __v: 0, ...updatedModel })
+        expect(spyFindOne).toHaveBeenCalledWith({ _id: createdId })
+        expect(spyFindOne).toHaveBeenCalledTimes(1)
+        spyFindOne.mockClear()
+      })
+  })
+
+  it('delete elements', () => {
+    return ModelController.delete(createdId)
+      .then(response => {
+        /* / Check the copy due to a bug with jest.
+        const copy = {}
+        Object.keys(Model.schema.paths).forEach(key => {
+          copy[key] = response.model[key]
+        }) */
+        expect(response).toMatchObject({})
+        expect(spyRemove).toHaveBeenCalledWith({ _id: createdId })
+        expect(spyRemove).toHaveBeenCalledTimes(1)
+        spyRemove.mockClear()
       })
   })
 })