123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- import * as dotenv from 'dotenv'
- dotenv.config()
- import mongoose from 'mongoose'
- mongoose.Promise = Promise
- import RESTControllerVersioned from './RESTControllerVersioned'
- 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 Versioned', () => {
- const ModelController = new RESTControllerVersioned(Model)
- const createdModel = {
- name: 'Gisele',
- tag: 'gisele'
- }
- const updatedModel = {
- name: 'Adriana'
- }
- const createdHistory = {
- author: 'Tomi',
- version: '1st shot',
- comment: 'Nothing to brag about',
- head: true
- }
- const updatedHistory = {
- author: 'Ben',
- version: '2nd shot',
- comment: 'I\'m a little narcissict',
- head: true
- }
- let createdId = null
- beforeAll(() => {
- return mongoose.connect('mongodb://localhost/__test_rest-controller_versioned__')
- Model.remove({}, err => {
- console.log(err)
- })
- })
- afterAll(() => {
- Model.remove({}, err => {
- console.log(err)
- })
- })
- it('creates an element', () => {
- return ModelController.create(createdModel, createdHistory.author, createdHistory.version, createdHistory.comment)
- .then(response => {
- // Check the copy due to a bug with jest.
- const copy = JSON.parse(JSON.stringify(response))
- createdId = response._id
- expect(copy).toMatchObject({__v: 0, _history: createdHistory, ...createdModel })
- expect(spyCreate).toHaveBeenCalledTimes(1)
- spyCreate.mockClear()
- })
- })
- it('reads an element', () => {
- return ModelController.read(createdModel.tag)
- .then(response => {
- // Check the copy due to a bug with jest.
- const copy = JSON.parse(JSON.stringify(response))
- expect(copy).toMatchObject({__v: 0, ...createdModel})
- expect(spyFindOne).toHaveBeenCalledWith({ '_history.head': true, tag: createdModel.tag })
- 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 = JSON.parse(JSON.stringify(response))
- expect(copy[0]).toMatchObject({__v: 0, ...createdModel})
- expect(response).toHaveLength(1)
- // expect(spyFind).toHaveBeenCalledWith({})
- expect(spyFind).toHaveBeenCalledTimes(1)
- spyFind.mockClear()
- })
- })
- it('update elements', () => {
- return ModelController.update('gisele', updatedModel, updatedHistory.author, updatedHistory.version, updatedHistory.comment)
- .then(response => {
- // Check the copy due to a bug with jest.
- const copy = JSON.parse(JSON.stringify(response))
- expect(copy).toMatchObject({ __v: 0, ...updatedModel })
- expect(spyFindOne).toHaveBeenCalledWith({ '_history.head': true, tag: 'gisele' })
- expect(spyFindOne).toHaveBeenCalledTimes(1)
- spyFindOne.mockClear()
- })
- })
- it('delete elements', () => {
- return ModelController.delete('gisele')
- .then(response => {
- // Check the copy due to a bug with jest.
- const copy = JSON.parse(JSON.stringify(response))
- expect(copy).toMatchObject({ tag: 'gisele', _history: { head: false } })
- expect(spyFindOne).toHaveBeenCalledWith({ '_history.head': true, tag: 'gisele' })
- expect(spyFindOne).toHaveBeenCalledTimes(1)
- spyFindOne.mockClear()
- })
- })
- it('lists elements after all operations', () => {
- return ModelController.list()
- .then(response => {
- // Check the copy due to a bug with jest.
- const copy = JSON.parse(JSON.stringify(response))
- expect(copy).toEqual([])
- expect(spyFind).toHaveBeenCalledWith({'_history.head': true})
- expect(spyFind).toHaveBeenCalledTimes(1)
- spyFind.mockClear()
- })
- })
- it('lists elements after all operations', () => {
- return ModelController.list({})
- .then(response => {
- // Check the copy due to a bug with jest.
- const copy = JSON.parse(JSON.stringify(response))
- expect(copy[0]).toMatchObject({ ...createdModel, _history: { head: false } })
- expect(copy[1]).toMatchObject({ ...updatedModel, _history: { head: false } })
- expect(response).toHaveLength(2)
- // expect(spyFind).toHaveBeenCalledWith({})
- expect(spyFind).toHaveBeenCalledTimes(1)
- spyFind.mockClear()
- })
- })
- })
|