|
| 1 | +import { DeleteResult } from 'mongodb' |
| 2 | +import { Test, TestingModule } from '@nestjs/testing' |
| 3 | +import { IdentitiesController } from './identities.controller' |
| 4 | +import { IdentitiesService } from './identities.service' |
| 5 | +import { IdentitiesDto } from './dto/identities.dto' |
| 6 | +import { Identities } from './schemas/identities.schema' |
| 7 | +import { HttpException, HttpStatus } from '@nestjs/common' |
| 8 | +import { Types } from 'mongoose' |
| 9 | +import { Response, Request } from 'express' |
| 10 | +import { getMockReq, getMockRes } from '@jest-mock/express' |
| 11 | + |
| 12 | +describe('IdentitiesController', () => { |
| 13 | + let controller: IdentitiesController |
| 14 | + let service: IdentitiesService |
| 15 | + const date = new Date() |
| 16 | + const _id = new Types.ObjectId() |
| 17 | + const { res, mockClear } = getMockRes() |
| 18 | + const object: Identities = { |
| 19 | + _id, |
| 20 | + metadata: { |
| 21 | + createdAt: date, |
| 22 | + createdBy: 'console', |
| 23 | + lastUpdateAt: date, |
| 24 | + lastUpdateBy: 'console', |
| 25 | + }, |
| 26 | + info: [], |
| 27 | + } |
| 28 | + |
| 29 | + beforeEach(async () => { |
| 30 | + mockClear() |
| 31 | + const module: TestingModule = await Test.createTestingModule({ |
| 32 | + controllers: [IdentitiesController], |
| 33 | + providers: [ |
| 34 | + IdentitiesService, |
| 35 | + { |
| 36 | + provide: IdentitiesService, |
| 37 | + useValue: { |
| 38 | + search: jest.fn().mockResolvedValue([[object], 1]), |
| 39 | + create: jest.fn().mockResolvedValue(object), |
| 40 | + read: jest.fn().mockResolvedValue(object), |
| 41 | + update: jest.fn().mockResolvedValue(object), |
| 42 | + remove: jest.fn().mockResolvedValue({ |
| 43 | + acknowledged: true, |
| 44 | + deletedCount: 1, |
| 45 | + }), |
| 46 | + }, |
| 47 | + }, |
| 48 | + ], |
| 49 | + }).compile() |
| 50 | + |
| 51 | + controller = module.get<IdentitiesController>(IdentitiesController) |
| 52 | + service = module.get<IdentitiesService>(IdentitiesService) |
| 53 | + }) |
| 54 | + |
| 55 | + describe('search', () => { |
| 56 | + it('should return an array of Identities objects and the total count', async () => { |
| 57 | + const req = getMockReq() |
| 58 | + const query = { _id: _id.toString() } |
| 59 | + const limit = 10 |
| 60 | + const skip = 1 |
| 61 | + const expectedResult: [Identities[], number] = [[object], 1] |
| 62 | + jest.spyOn(service, 'search').mockImplementation(async () => await expectedResult) |
| 63 | + const response = await controller.search(req, res, query, limit.toString(), skip.toString(), null) |
| 64 | + expect(response.json).toHaveBeenCalledWith({ data: expectedResult[0], total: expectedResult[1] }) |
| 65 | + expect(response.status).toHaveBeenCalledWith(HttpStatus.OK) |
| 66 | + }) |
| 67 | + |
| 68 | + it('should return an array of Identities objects with default pagination', async () => { |
| 69 | + const req = getMockReq() |
| 70 | + const query = { _id: _id.toString() } |
| 71 | + const limit = 0 |
| 72 | + const skip = 0 |
| 73 | + const expectedResult: [Identities[], number] = [[object], 1] |
| 74 | + jest.spyOn(service, 'search').mockImplementation(async () => await expectedResult) |
| 75 | + const response = await controller.search(req, res, query, limit.toString(), skip.toString(), null) |
| 76 | + expect(response.json).toHaveBeenCalledWith({ data: expectedResult[0], total: expectedResult[1] }) |
| 77 | + expect(response.status).toHaveBeenCalledWith(HttpStatus.OK) |
| 78 | + }) |
| 79 | + |
| 80 | + it('should throw HttpException with BAD_REQUEST status when an error occurs', async () => { |
| 81 | + const req = {} as Request |
| 82 | + const res = {} as Response |
| 83 | + const query = { someParam: 'value' } |
| 84 | + const limit = 10 |
| 85 | + const skip = 0 |
| 86 | + jest.spyOn(service, 'search').mockRejectedValue(new Error('Something went wrong')) |
| 87 | + |
| 88 | + try { |
| 89 | + await controller.search(req, res, query, limit.toString(), skip.toString(), null) |
| 90 | + } catch (error) { |
| 91 | + const status = error.getStatus() |
| 92 | + const response = error.getResponse() |
| 93 | + |
| 94 | + expect(error).toBeInstanceOf(HttpException) |
| 95 | + expect(status).toBe(400) |
| 96 | + expect(response).toBe('Something went wrong') |
| 97 | + } |
| 98 | + }) |
| 99 | + }) |
| 100 | + |
| 101 | + describe('read', () => { |
| 102 | + it('should return the data successfully', async () => { |
| 103 | + const expectedResult: Identities = object |
| 104 | + jest.spyOn(service, 'read').mockImplementation(async () => await expectedResult) |
| 105 | + const result = await controller.read(_id.toString(), res) |
| 106 | + expect(res.status).toHaveBeenCalledWith(HttpStatus.OK) |
| 107 | + expect(res.json).toHaveBeenCalledWith(expectedResult) |
| 108 | + expect(result).toEqual(res) |
| 109 | + }) |
| 110 | + |
| 111 | + it('should throw an HttpException with the error message', async () => { |
| 112 | + const errorMessage = 'Error message' |
| 113 | + jest.spyOn(service, 'read').mockRejectedValue(new Error(errorMessage)) |
| 114 | + try { |
| 115 | + await controller.read(_id.toString(), res) |
| 116 | + } catch (error) { |
| 117 | + expect(error.getStatus()).toBe(HttpStatus.BAD_REQUEST) |
| 118 | + expect(error.getResponse()).toBe(errorMessage) |
| 119 | + } |
| 120 | + }) |
| 121 | + }) |
| 122 | + |
| 123 | + describe('create', () => { |
| 124 | + it('should return a Identities object', async () => { |
| 125 | + const dto: IdentitiesDto = { someProp: 'value' } |
| 126 | + const expectedResult = object |
| 127 | + jest.spyOn(service, 'create').mockImplementation(async () => await expectedResult) |
| 128 | + const response = await controller.create(dto, res) |
| 129 | + expect(response.status).toHaveBeenCalledWith(HttpStatus.CREATED) |
| 130 | + expect(response.json).toHaveBeenCalledWith(expectedResult) |
| 131 | + }) |
| 132 | + |
| 133 | + it('should throw HttpException with BAD_REQUEST status when an error occurs', async () => { |
| 134 | + const dto: IdentitiesDto = { someProp: 'value' } |
| 135 | + jest.spyOn(service, 'create').mockRejectedValue(new Error('Something went wrong')) |
| 136 | + |
| 137 | + try { |
| 138 | + await controller.create(dto, res) |
| 139 | + } catch (error) { |
| 140 | + expect(error).toBeInstanceOf(HttpException) |
| 141 | + expect(error.getStatus()).toBe(400) |
| 142 | + expect(error.getResponse()).toBe('Something went wrong') |
| 143 | + } |
| 144 | + }) |
| 145 | + }) |
| 146 | + |
| 147 | + describe('update', () => { |
| 148 | + it('should return a Identities object', async () => { |
| 149 | + const id = _id.toString() |
| 150 | + const dto: IdentitiesDto = { someProp: 'value' } |
| 151 | + const expectedResult: Identities = object |
| 152 | + jest.spyOn(service, 'update').mockImplementation(async () => await expectedResult) |
| 153 | + const response = await controller.update(id, dto, res) |
| 154 | + expect(response.status).toHaveBeenCalledWith(HttpStatus.OK) |
| 155 | + expect(response.json).toHaveBeenCalledWith(expectedResult) |
| 156 | + }) |
| 157 | + |
| 158 | + it('should throw HttpException with BAD_REQUEST status when an error occurs', async () => { |
| 159 | + const id = _id.toString() |
| 160 | + const dto: IdentitiesDto = { someProp: 'value' } |
| 161 | + jest.spyOn(service, 'update').mockRejectedValue(new Error('Something went wrong')) |
| 162 | + |
| 163 | + try { |
| 164 | + await controller.update(id, dto, res) |
| 165 | + } catch (error) { |
| 166 | + expect(error).toBeInstanceOf(HttpException) |
| 167 | + expect(error.getStatus()).toBe(400) |
| 168 | + expect(error.getResponse()).toBe('Something went wrong') |
| 169 | + } |
| 170 | + }) |
| 171 | + }) |
| 172 | + |
| 173 | + describe('remove', () => { |
| 174 | + it('should return a Identities object', async () => { |
| 175 | + const id = _id.toString() |
| 176 | + const expectedResult: DeleteResult = { |
| 177 | + acknowledged: true, |
| 178 | + deletedCount: 1, |
| 179 | + } |
| 180 | + jest.spyOn(service, 'remove').mockImplementation(async () => await expectedResult) |
| 181 | + const response = await controller.remove(id, res) |
| 182 | + expect(response.status).toHaveBeenCalledWith(HttpStatus.OK) |
| 183 | + expect(response.json).toHaveBeenCalledWith(expectedResult) |
| 184 | + }) |
| 185 | + |
| 186 | + it('should throw HttpException with BAD_REQUEST status when an error occurs', async () => { |
| 187 | + const id = _id.toString() |
| 188 | + jest.spyOn(service, 'remove').mockRejectedValue(new Error('Something went wrong')) |
| 189 | + |
| 190 | + try { |
| 191 | + await controller.remove(id, res) |
| 192 | + } catch (error) { |
| 193 | + expect(error).toBeInstanceOf(HttpException) |
| 194 | + expect(error.getStatus()).toBe(400) |
| 195 | + expect(error.getResponse()).toBe('Something went wrong') |
| 196 | + } |
| 197 | + }) |
| 198 | + }) |
| 199 | +}) |
0 commit comments