|
| 1 | +import { ICollection, id as convertToMongoId } from 'monk'; |
| 2 | +import { Repository } from './repository.service'; |
| 3 | + |
| 4 | +describe('Repository', () => { |
| 5 | + let repo: Repository<PersonTest>; |
| 6 | + let collection: ICollection<PersonTest>; |
| 7 | + let findOneMock: jest.SpyInstance<Promise<PersonTest>, any[]>; |
| 8 | + let findMock: jest.SpyInstance<Promise<PersonTest[]>, any[]>; |
| 9 | + let insertMock: jest.SpyInstance<Promise<PersonTest>, any[]>; |
| 10 | + let removeMock: jest.SpyInstance< |
| 11 | + Promise<{ |
| 12 | + deletedCount: number; |
| 13 | + result: { |
| 14 | + n: number; |
| 15 | + ok: 0 | 1; |
| 16 | + }; |
| 17 | + }>, |
| 18 | + any[] |
| 19 | + >; |
| 20 | + let findOneAndUpdateMock: jest.SpyInstance<Promise<PersonTest>, any[]>; |
| 21 | + |
| 22 | + beforeEach(async () => { |
| 23 | + collection = { |
| 24 | + findOne: () => null, |
| 25 | + find: () => null, |
| 26 | + insert: () => null, |
| 27 | + remove: () => null, |
| 28 | + findOneAndUpdate: () => null, |
| 29 | + } as any; |
| 30 | + findOneMock = jest.spyOn(collection, 'findOne') as any; |
| 31 | + findMock = jest.spyOn(collection, 'find') as any; |
| 32 | + insertMock = jest.spyOn(collection, 'insert') as any; |
| 33 | + removeMock = jest.spyOn(collection, 'remove') as any; |
| 34 | + findOneAndUpdateMock = jest.spyOn(collection, 'findOneAndUpdate') as any; |
| 35 | + repo = new Repository(collection); |
| 36 | + }); |
| 37 | + |
| 38 | + it('getById() should invoke monk collection.findOne()', async () => { |
| 39 | + const id = '507f191e810c19729de860ea'; |
| 40 | + findOneMock.mockReturnValue(Promise.resolve(getMockPersonTest())); |
| 41 | + const response = await repo.getById(id); |
| 42 | + expect(response).toEqual(getMockPersonTest()); |
| 43 | + expect(findOneMock).toHaveBeenCalledTimes(1); |
| 44 | + expect(findOneMock).toHaveBeenCalledWith({ |
| 45 | + _id: convertToMongoId(id).toHexString(), |
| 46 | + }); |
| 47 | + }); |
| 48 | + |
| 49 | + it('list() should invoke monk collection.find()', async () => { |
| 50 | + const id = '507f191e810c19729de860ea'; |
| 51 | + findMock.mockReturnValue(Promise.resolve([getMockPersonTest()])); |
| 52 | + const response = await repo.list({ |
| 53 | + _id: convertToMongoId(id).toHexString(), |
| 54 | + }); |
| 55 | + expect(response).toEqual([getMockPersonTest()]); |
| 56 | + expect(findMock).toHaveBeenCalledTimes(1); |
| 57 | + expect(findMock).toHaveBeenCalledWith({ |
| 58 | + _id: convertToMongoId(id).toHexString(), |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + it('add() should invoke monk collection.insert()', async () => { |
| 63 | + const personTest = getMockPersonTest(); |
| 64 | + insertMock.mockReturnValue(Promise.resolve(personTest)); |
| 65 | + const response = await repo.add(personTest); |
| 66 | + expect(response).toEqual(getMockPersonTest()); |
| 67 | + expect(insertMock).toHaveBeenCalledTimes(1); |
| 68 | + expect(insertMock).toHaveBeenCalledWith(personTest); |
| 69 | + }); |
| 70 | + |
| 71 | + it('delete() should invoke monk collection.remove()', async () => { |
| 72 | + const mockDeleteResponse = { |
| 73 | + deletedCount: 1, |
| 74 | + result: { |
| 75 | + n: 1, |
| 76 | + ok: 1 as any, |
| 77 | + }, |
| 78 | + }; |
| 79 | + removeMock.mockReturnValue(Promise.resolve(mockDeleteResponse)); |
| 80 | + const id = '507f191e810c19729de860ea'; |
| 81 | + const response = await repo.delete(id); |
| 82 | + expect(response).toEqual(mockDeleteResponse); |
| 83 | + expect(removeMock).toHaveBeenCalledTimes(1); |
| 84 | + expect(removeMock).toHaveBeenCalledWith({ |
| 85 | + _id: convertToMongoId(id).toHexString(), |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + it('edit() should invoke monk collection.findOneAndUpdateMock() and only set certain properties', async () => { |
| 90 | + const personTest = getMockPersonTest(); |
| 91 | + const id = '507f191e810c19729de860ea'; |
| 92 | + findOneAndUpdateMock.mockReturnValue(Promise.resolve(personTest)); |
| 93 | + const response = await repo.edit(id, personTest, ['profession', 'sex']); |
| 94 | + expect(response).toEqual(personTest); |
| 95 | + expect(findOneAndUpdateMock).toHaveBeenCalledTimes(1); |
| 96 | + expect(findOneAndUpdateMock).toHaveBeenCalledWith( |
| 97 | + { _id: convertToMongoId(id).toHexString() }, |
| 98 | + { $set: { profession: 'Software Engineer', sex: 'M' } }, |
| 99 | + ); |
| 100 | + }); |
| 101 | + |
| 102 | + it('edit() should invoke monk collection.findOneAndUpdateMock() and set all properties', async () => { |
| 103 | + const personTest = getMockPersonTest(); |
| 104 | + const id = '507f191e810c19729de860ea'; |
| 105 | + findOneAndUpdateMock.mockReturnValue(Promise.resolve(personTest)); |
| 106 | + const response = await repo.edit(id, personTest); |
| 107 | + expect(response).toEqual(personTest); |
| 108 | + expect(findOneAndUpdateMock).toHaveBeenCalledTimes(1); |
| 109 | + expect(findOneAndUpdateMock).toHaveBeenCalledWith( |
| 110 | + { _id: convertToMongoId(id).toHexString() }, |
| 111 | + { $set: personTest }, |
| 112 | + ); |
| 113 | + }); |
| 114 | +}); |
| 115 | + |
| 116 | +function getMockPersonTest(): PersonTest { |
| 117 | + return { |
| 118 | + name: 'Ben', |
| 119 | + dob: new Date('1988-12-30'), |
| 120 | + sex: 'M', |
| 121 | + profession: 'Software Engineer', |
| 122 | + }; |
| 123 | +} |
| 124 | + |
| 125 | +class PersonTest { |
| 126 | + name: string; |
| 127 | + dob: Date; |
| 128 | + sex: 'M' | 'F'; |
| 129 | + profession: string; |
| 130 | +} |
0 commit comments