123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- import * as dotenv from 'dotenv'
- dotenv.config()
- import mongoose from 'mongoose'
- mongoose.Promise = Promise
- import RESTController from './RESTController'
- 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', () => {
- const ModelController = new RESTController(Model)
- const createdModel = {
- name: 'Gisele'
- }
- const updatedModel = {
- name: 'Adriana'
- }
- let createdId = null
- beforeAll(() => {
- return mongoose.connect('mongodb://localhost/__test_rest-controller__')
- Model.remove({}, err => {
- console.log(err)
- })
- })
- afterAll(() => {
- Model.remove({}, err => {
- console.log(err)
- })
- })
- it('creates an element', () => {
- return ModelController.create(createdModel)
- .then(response => {
- // Check the copy due to a bug with jest.
- const copy = {}
- Object.keys(Model.schema.paths).forEach(key => {
- copy[key] = response[key]
- })
- createdId = response._id
- expect(copy).toMatchObject({__v: 0, ...createdModel})
- expect(spyCreate).toHaveBeenCalledWith(createdModel)
- expect(spyCreate).toHaveBeenCalledTimes(1)
- spyCreate.mockClear()
- })
- })
- it('reads an element', () => {
- return ModelController.read(createdId)
- .then(response => {
- // Check the copy due to a bug with jest.
- const copy = {}
- Object.keys(Model.schema.paths).forEach(key => {
- copy[key] = response[key]
- })
- expect(copy).toMatchObject({__v: 0, ...createdModel})
- expect(spyFindOne).toHaveBeenCalledWith({ _id: createdId })
- expect(spyFindOne).toHaveBeenCalledTimes(1)
- spyFindOne.mockClear()
- })
- })
- it('lists elements', () => {
- return ModelController.list()
- .then(response => {
- // Check the copy due to a bug with jest.
- const copy = {}
- Object.keys(Model.schema.paths).forEach(key => {
- copy[key] = response[0][key]
- })
- expect(copy).toMatchObject({__v: 0, ...createdModel})
- expect(response).toHaveLength(1)
- expect(spyFind).toHaveBeenCalledWith({})
- expect(spyFind).toHaveBeenCalledTimes(1)
- spyFind.mockClear()
- })
- })
- it('update elements', () => {
- return ModelController.update(createdId, { name: 'Adriana' })
- .then(response => {
- // Check the copy due to a bug with jest.
- const copy = {}
- Object.keys(Model.schema.paths).forEach(key => {
- copy[key] = response[key]
- })
- 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 => {
- expect(response).toMatchObject({})
- expect(spyRemove).toHaveBeenCalledWith({ _id: createdId })
- expect(spyRemove).toHaveBeenCalledTimes(1)
- spyRemove.mockClear()
- })
- })
- })
|