|
| 1 | +import { createMock, DeepMocked } from '@golevelup/nestjs-testing'; |
| 2 | +import { Test, TestingModule } from '@nestjs/testing'; |
| 3 | +import { CatController } from './cat.controller'; |
| 4 | +import { CatDTO } from './cat.dto'; |
| 5 | +import { CatService } from './cat.service'; |
| 6 | +import { Cat } from './interfaces/cat.interface'; |
| 7 | + |
| 8 | +describe('Cat Controller', () => { |
| 9 | + let controller: CatController; |
| 10 | + let service: CatService; |
| 11 | + |
| 12 | + beforeEach(async () => { |
| 13 | + const module: TestingModule = await Test.createTestingModule({ |
| 14 | + controllers: [CatController], |
| 15 | + // If you've looked at the complex sample you'll notice that these functions |
| 16 | + // are a little bit more in depth using mock implementation |
| 17 | + // to give us a little bit more control and flexibility in our tests |
| 18 | + // this is not necessary, but can sometimes be helpful in a test scenario |
| 19 | + providers: [ |
| 20 | + { |
| 21 | + provide: CatService, |
| 22 | + useValue: { |
| 23 | + getAll: jest |
| 24 | + .fn() |
| 25 | + .mockResolvedValue([ |
| 26 | + { name: 'Test Cat 1', breed: 'Test Breed 1', age: 4 }, |
| 27 | + { name: 'Test Cat 2', breed: 'Test Breed 2', age: 3 }, |
| 28 | + { name: 'Test Cat 3', breed: 'Test Breed 3', age: 2 }, |
| 29 | + ]), |
| 30 | + getOne: jest.fn().mockImplementation((id: string) => |
| 31 | + Promise.resolve({ |
| 32 | + name: 'Test Cat 1', |
| 33 | + breed: 'Test Breed 1', |
| 34 | + age: 4, |
| 35 | + _id: id, |
| 36 | + }), |
| 37 | + ), |
| 38 | + getOneByName: jest |
| 39 | + .fn() |
| 40 | + .mockImplementation((name: string) => |
| 41 | + Promise.resolve({ name, breed: 'Test Breed 1', age: 4 }), |
| 42 | + ), |
| 43 | + insertOne: jest |
| 44 | + .fn() |
| 45 | + .mockImplementation((cat: CatDTO) => |
| 46 | + Promise.resolve({ _id: 'a uuid', ...cat }), |
| 47 | + ), |
| 48 | + updateOne: jest |
| 49 | + .fn() |
| 50 | + .mockImplementation((cat: CatDTO) => |
| 51 | + Promise.resolve({ _id: 'a uuid', ...cat }), |
| 52 | + ), |
| 53 | + deleteOne: jest.fn().mockResolvedValue({ deleted: true }), |
| 54 | + }, |
| 55 | + }, |
| 56 | + ], |
| 57 | + }).compile(); |
| 58 | + |
| 59 | + controller = module.get<CatController>(CatController); |
| 60 | + service = module.get<CatService>(CatService); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should be defined', () => { |
| 64 | + expect(controller).toBeDefined(); |
| 65 | + }); |
| 66 | + |
| 67 | + describe('getCats', () => { |
| 68 | + it('should get an array of cats', () => { |
| 69 | + expect(controller.getCats()).resolves.toEqual([ |
| 70 | + { |
| 71 | + name: 'Test Cat 1', |
| 72 | + breed: 'Test Breed 1', |
| 73 | + age: 4, |
| 74 | + }, |
| 75 | + { |
| 76 | + name: 'Test Cat 2', |
| 77 | + breed: 'Test Breed 2', |
| 78 | + age: 3, |
| 79 | + }, |
| 80 | + { |
| 81 | + name: 'Test Cat 3', |
| 82 | + breed: 'Test Breed 3', |
| 83 | + age: 2, |
| 84 | + }, |
| 85 | + ]); |
| 86 | + }); |
| 87 | + }); |
| 88 | + describe('getById', () => { |
| 89 | + it('should get a single cat', () => { |
| 90 | + expect(controller.getById('a strange id')).resolves.toEqual({ |
| 91 | + name: 'Test Cat 1', |
| 92 | + breed: 'Test Breed 1', |
| 93 | + age: 4, |
| 94 | + _id: 'a strange id', |
| 95 | + }); |
| 96 | + expect(controller.getById('a different id')).resolves.toEqual({ |
| 97 | + name: 'Test Cat 1', |
| 98 | + breed: 'Test Breed 1', |
| 99 | + age: 4, |
| 100 | + _id: 'a different id', |
| 101 | + }); |
| 102 | + }); |
| 103 | + }); |
| 104 | + describe('getByName', () => { |
| 105 | + it('should get a cat back', async () => { |
| 106 | + expect(controller.getByName('Ventus')).resolves.toEqual({ |
| 107 | + name: 'Ventus', |
| 108 | + breed: 'Test Breed 1', |
| 109 | + age: 4, |
| 110 | + }); |
| 111 | + // using the really cool @golevelup/nestjs-testing module's utility function here |
| 112 | + // otherwise we need to pass `as any` or we need to mock all 54+ attributes of Document |
| 113 | + const aquaMock = createMock<Cat>({ |
| 114 | + name: 'Aqua', |
| 115 | + breed: 'Maine Coon', |
| 116 | + age: 5, |
| 117 | + }); |
| 118 | + const getByNameSpy = jest |
| 119 | + .spyOn(service, 'getOneByName') |
| 120 | + .mockResolvedValueOnce(aquaMock); |
| 121 | + const getResponse = await controller.getByName('Aqua'); |
| 122 | + expect(getResponse).toEqual(aquaMock); |
| 123 | + expect(getByNameSpy).toBeCalledWith('Aqua'); |
| 124 | + }); |
| 125 | + }); |
| 126 | + describe('newCat', () => { |
| 127 | + it('should create a new cat', () => { |
| 128 | + const newCatDTO: CatDTO = { |
| 129 | + name: 'New Cat 1', |
| 130 | + breed: 'New Breed 1', |
| 131 | + age: 4, |
| 132 | + }; |
| 133 | + expect(controller.newCat(newCatDTO)).resolves.toEqual({ |
| 134 | + _id: 'a uuid', |
| 135 | + ...newCatDTO, |
| 136 | + }); |
| 137 | + }); |
| 138 | + }); |
| 139 | + describe('updateCat', () => { |
| 140 | + it('should update a new cat', () => { |
| 141 | + const newCatDTO: CatDTO = { |
| 142 | + name: 'New Cat 1', |
| 143 | + breed: 'New Breed 1', |
| 144 | + age: 4, |
| 145 | + }; |
| 146 | + expect(controller.updateCat(newCatDTO)).resolves.toEqual({ |
| 147 | + _id: 'a uuid', |
| 148 | + ...newCatDTO, |
| 149 | + }); |
| 150 | + }); |
| 151 | + }); |
| 152 | + describe('deleteCat', () => { |
| 153 | + it('should return that it deleted a cat', () => { |
| 154 | + expect(controller.deleteCat('a uuid that exists')).resolves.toEqual({ |
| 155 | + deleted: true, |
| 156 | + }); |
| 157 | + }); |
| 158 | + it('should return that it did not delete a cat', () => { |
| 159 | + const deleteSpy = jest |
| 160 | + .spyOn(service, 'deleteOne') |
| 161 | + .mockResolvedValueOnce({ deleted: false }); |
| 162 | + expect( |
| 163 | + controller.deleteCat('a uuid that does not exist'), |
| 164 | + ).resolves.toEqual({ deleted: false }); |
| 165 | + expect(deleteSpy).toBeCalledWith('a uuid that does not exist'); |
| 166 | + }); |
| 167 | + }); |
| 168 | +}); |
0 commit comments