|
| 1 | +import mongoose from 'mongoose'; |
| 2 | +import loadFixture from 'mongoose-fixture-loader'; |
| 3 | +import Interface from 'forest-express'; |
| 4 | +import ResourcesRemover from '../../../src/services/resources-remover'; |
| 5 | +import mongooseConnect from '../../utils/mongoose-connect'; |
| 6 | +import { InvalidParameterError } from '../../../src/services/errors'; |
| 7 | + |
| 8 | +describe('service > resources-remover', () => { |
| 9 | + let IslandModel; |
| 10 | + |
| 11 | + beforeAll(() => { |
| 12 | + Interface.Schemas = { |
| 13 | + schemas: { |
| 14 | + Island: { |
| 15 | + name: 'Island', |
| 16 | + idField: '_id', |
| 17 | + searchFields: ['name'], |
| 18 | + fields: [ |
| 19 | + { field: '_id', type: 'String' }, |
| 20 | + { field: 'name', type: 'String' }, |
| 21 | + ], |
| 22 | + }, |
| 23 | + }, |
| 24 | + }; |
| 25 | + |
| 26 | + return mongooseConnect() |
| 27 | + .then(() => { |
| 28 | + const IslandSchema = mongoose.Schema({ |
| 29 | + name: { type: String }, |
| 30 | + }); |
| 31 | + |
| 32 | + IslandModel = mongoose.model('Island', IslandSchema); |
| 33 | + return IslandModel.remove({}); |
| 34 | + }); |
| 35 | + }); |
| 36 | + |
| 37 | + afterAll(() => mongoose.connection.close()); |
| 38 | + |
| 39 | + beforeEach(async () => { |
| 40 | + await IslandModel.remove({}); |
| 41 | + await loadFixture(IslandModel, [ |
| 42 | + { name: 'Kauai', _id: '56cb91bdc3464f14678934ca' }, |
| 43 | + { name: 'Oahu', _id: '56cb91bdc3464f14678934cb' }, |
| 44 | + ]); |
| 45 | + }); |
| 46 | + |
| 47 | + |
| 48 | + it('should throw error if ids is not an array or empty', () => { |
| 49 | + expect.assertions(3); |
| 50 | + expect(() => new ResourcesRemover(null, []).perform()).toThrow(InvalidParameterError); |
| 51 | + expect(() => new ResourcesRemover(null, 'foo').perform()).toThrow(InvalidParameterError); |
| 52 | + expect(() => new ResourcesRemover(null, {}).perform()).toThrow(InvalidParameterError); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should remove one resource with existing ID', async () => { |
| 56 | + expect.assertions(1); |
| 57 | + await new ResourcesRemover(IslandModel, ['56cb91bdc3464f14678934ca']).perform(); |
| 58 | + const documentsCount = await IslandModel.countDocuments(); |
| 59 | + expect(documentsCount).toStrictEqual(1); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should remove all resources with existing ID', async () => { |
| 63 | + expect.assertions(1); |
| 64 | + await new ResourcesRemover(IslandModel, ['56cb91bdc3464f14678934ca', '56cb91bdc3464f14678934cb']).perform(); |
| 65 | + const documentsCount = await IslandModel.countDocuments(); |
| 66 | + expect(documentsCount).toStrictEqual(0); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should not remove resource with not existing ID', async () => { |
| 70 | + expect.assertions(1); |
| 71 | + await new ResourcesRemover(IslandModel, ['56cb91bdc3464f14678934cd']).perform(); |
| 72 | + const documentsCount = await IslandModel.countDocuments(); |
| 73 | + expect(documentsCount).toStrictEqual(2); |
| 74 | + }); |
| 75 | +}); |
0 commit comments