|
1 | 1 | import { Test, TestingModule } from "@nestjs/testing"; |
2 | 2 | import { ResourcesController } from "./resources.controller"; |
3 | 3 | import { ResourcesService } from "./resources.service"; |
| 4 | +import { CreateResourceDto } from "./dto/create-resource.dto"; |
| 5 | +import { CustomRequest } from "../global/types/CustomRequest"; |
| 6 | +import { BadRequestException, NotFoundException } from "@nestjs/common"; |
| 7 | +import { UpdateResourceDto } from "./dto/update-resource.dto"; |
| 8 | + |
| 9 | +const requestMock = {} as unknown as CustomRequest; |
| 10 | +const dtoCreateMock = { |
| 11 | + url: "https://chingu.com", |
| 12 | + title: "Chingu", |
| 13 | +} as CreateResourceDto; |
| 14 | + |
| 15 | +//inavlid team id constant |
| 16 | +const invalidTeamId = 9999; |
| 17 | + |
| 18 | +// dto for updating a resource url |
| 19 | +const dtoUpdateMock = { |
| 20 | + url: "https://chingu-2.com", |
| 21 | +} as UpdateResourceDto; |
| 22 | +const mockTeamId: number = 1; |
| 23 | + |
| 24 | +const mockResource = { |
| 25 | + id: 1, |
| 26 | + url: "https://chingu.com", |
| 27 | + title: "Chingu", |
| 28 | + teamMemberId: 1, |
| 29 | + createdAt: new Date(Date.now()), |
| 30 | + updatedAt: new Date(Date.now()), |
| 31 | +}; |
| 32 | + |
| 33 | +const mockUpdatedResource = { |
| 34 | + ...mockResource, |
| 35 | + url: "https://chingu-2.com", |
| 36 | +}; |
| 37 | + |
| 38 | +const mockResourceArr = [ |
| 39 | + { |
| 40 | + id: 1, |
| 41 | + url: "https://chingu.com", |
| 42 | + title: "Chingu", |
| 43 | + teamMemberId: 1, |
| 44 | + addedBy: { |
| 45 | + member: { |
| 46 | + firstName: "Larry", |
| 47 | + lastName: "Castro", |
| 48 | + id: "18093ad0-88ef-4bcd-bee8-322749c876bd", |
| 49 | + avatar: "https://gravatar.com/avatar/90383a4ee0fb891c1ec3374e6a593a6c6fd88166d4fd45f796dabeaba7af836d?s=200&r=g&d=wavatar\n", |
| 50 | + }, |
| 51 | + }, |
| 52 | + createdAt: new Date(Date.now()), |
| 53 | + updatedAt: new Date(Date.now()), |
| 54 | + }, |
| 55 | + { |
| 56 | + id: 2, |
| 57 | + url: "https://Nestjs.com", |
| 58 | + title: "Nestjs", |
| 59 | + teamMemberId: 2, |
| 60 | + addedBy: { |
| 61 | + member: { |
| 62 | + firstName: "John", |
| 63 | + lastName: "Doe", |
| 64 | + id: "18093ad0-88ef-4bcd-bee8-322749c876bd", |
| 65 | + avatar: "https://gravatar.com/avatar/90383a4ee0fb891c1ec3374e6a593a6c6fd88166d4fd45f796dabeaba7af836d?s=200&r=g&d=wavatar\n", |
| 66 | + }, |
| 67 | + }, |
| 68 | + createdAt: new Date(Date.now()), |
| 69 | + updatedAt: new Date(Date.now()), |
| 70 | + }, |
| 71 | +]; |
| 72 | + |
| 73 | +const mockResourcesService = { |
| 74 | + createNewResource: jest.fn(), |
| 75 | + findAllResources: jest.fn(), |
| 76 | + updateResource: jest.fn(), |
| 77 | + removeResource: jest.fn(), |
| 78 | +}; |
4 | 79 |
|
5 | 80 | describe("ResourcesController", () => { |
6 | 81 | let controller: ResourcesController; |
7 | 82 |
|
8 | 83 | beforeEach(async () => { |
9 | 84 | const module: TestingModule = await Test.createTestingModule({ |
10 | 85 | controllers: [ResourcesController], |
11 | | - providers: [ |
12 | | - { |
13 | | - provide: ResourcesService, |
14 | | - useValue: {}, |
15 | | - }, |
16 | | - ], |
17 | | - }).compile(); |
| 86 | + providers: [ResourcesService], |
| 87 | + }) |
| 88 | + .overrideProvider(ResourcesService) |
| 89 | + .useValue(mockResourcesService) |
| 90 | + .compile(); |
18 | 91 |
|
19 | 92 | controller = module.get<ResourcesController>(ResourcesController); |
20 | 93 | }); |
21 | 94 |
|
22 | 95 | it("should be defined", () => { |
23 | 96 | expect(controller).toBeDefined(); |
24 | 97 | }); |
| 98 | + |
| 99 | + describe("createNewResource", () => { |
| 100 | + it("createNewResource service should be defined", async () => { |
| 101 | + expect(controller.createNewResource).toBeDefined(); |
| 102 | + }); |
| 103 | + |
| 104 | + it("should create a new resource", async () => { |
| 105 | + mockResourcesService.createNewResource.mockResolvedValueOnce( |
| 106 | + mockResource, |
| 107 | + ); |
| 108 | + |
| 109 | + const result = await controller.createNewResource( |
| 110 | + requestMock, |
| 111 | + mockTeamId, |
| 112 | + dtoCreateMock, |
| 113 | + ); |
| 114 | + |
| 115 | + expect(result).toEqual(mockResource); |
| 116 | + expect(mockResourcesService.createNewResource).toHaveBeenCalledWith( |
| 117 | + requestMock, |
| 118 | + dtoCreateMock, |
| 119 | + mockTeamId, |
| 120 | + ); |
| 121 | + }); |
| 122 | + it("should throw badRequest exception for invalid dto", async () => { |
| 123 | + mockResourcesService.createNewResource.mockRejectedValueOnce( |
| 124 | + new BadRequestException(), |
| 125 | + ); |
| 126 | + await expect( |
| 127 | + controller.createNewResource(requestMock, mockTeamId, { |
| 128 | + url: "https://chingu.com", |
| 129 | + } as CreateResourceDto), |
| 130 | + ).rejects.toThrow(BadRequestException); |
| 131 | + expect(mockResourcesService.createNewResource).toHaveBeenCalledWith( |
| 132 | + requestMock, |
| 133 | + dtoCreateMock, |
| 134 | + mockTeamId, |
| 135 | + ); |
| 136 | + }); |
| 137 | + it("should throw notFound exception for invalid teamId", async () => { |
| 138 | + mockResourcesService.createNewResource.mockRejectedValueOnce( |
| 139 | + new NotFoundException(), |
| 140 | + ); |
| 141 | + await expect( |
| 142 | + controller.createNewResource(requestMock, 9999, dtoCreateMock), |
| 143 | + ).rejects.toThrow(NotFoundException); |
| 144 | + expect(mockResourcesService.createNewResource).toHaveBeenCalledWith( |
| 145 | + requestMock, |
| 146 | + dtoCreateMock, |
| 147 | + invalidTeamId, |
| 148 | + ); |
| 149 | + }); |
| 150 | + }); |
| 151 | + describe("findAllResources", () => { |
| 152 | + it("findAllResources service should be defined", async () => { |
| 153 | + expect(controller.findAllResources).toBeDefined(); |
| 154 | + }); |
| 155 | + |
| 156 | + it("should find all resources", async () => { |
| 157 | + mockResourcesService.findAllResources.mockResolvedValueOnce( |
| 158 | + mockResourceArr, |
| 159 | + ); |
| 160 | + |
| 161 | + const result = await controller.findAllResources( |
| 162 | + requestMock, |
| 163 | + mockTeamId, |
| 164 | + ); |
| 165 | + |
| 166 | + expect(result).toBeArray; |
| 167 | + expect(result).toHaveLength(2); |
| 168 | + expect(result[0]).toEqual({ |
| 169 | + id: expect.any(Number), |
| 170 | + url: expect.any(String), |
| 171 | + title: expect.any(String), |
| 172 | + teamMemberId: expect.any(Number), |
| 173 | + addedBy: { |
| 174 | + member: { |
| 175 | + firstName: expect.any(String), |
| 176 | + lastName: expect.any(String), |
| 177 | + id: expect.any(String), |
| 178 | + avatar: expect.any(String), |
| 179 | + }, |
| 180 | + }, |
| 181 | + createdAt: expect.any(Date), |
| 182 | + updatedAt: expect.any(Date), |
| 183 | + }); |
| 184 | + expect(mockResourcesService.findAllResources).toHaveBeenCalledWith( |
| 185 | + requestMock, |
| 186 | + mockTeamId, |
| 187 | + ); |
| 188 | + }); |
| 189 | + it("should throw notFound exception for invalid teamId", async () => { |
| 190 | + mockResourcesService.findAllResources.mockRejectedValueOnce( |
| 191 | + new NotFoundException(), |
| 192 | + ); |
| 193 | + await expect( |
| 194 | + controller.findAllResources(requestMock, invalidTeamId), |
| 195 | + ).rejects.toThrow(NotFoundException); |
| 196 | + expect(mockResourcesService.findAllResources).toHaveBeenCalledWith( |
| 197 | + requestMock, |
| 198 | + invalidTeamId, |
| 199 | + ); |
| 200 | + }); |
| 201 | + }); |
| 202 | + describe("Update Resource", () => { |
| 203 | + it("updateResource service should be defined", async () => { |
| 204 | + expect(controller.updateResource).toBeDefined(); |
| 205 | + }); |
| 206 | + |
| 207 | + it("should update a resource", async () => { |
| 208 | + mockResourcesService.updateResource.mockResolvedValueOnce( |
| 209 | + mockUpdatedResource, |
| 210 | + ); |
| 211 | + |
| 212 | + const result = await controller.updateResource( |
| 213 | + requestMock, |
| 214 | + mockResource.id, |
| 215 | + dtoUpdateMock, |
| 216 | + ); |
| 217 | + |
| 218 | + expect(result).toEqual(mockUpdatedResource); |
| 219 | + expect(mockResourcesService.updateResource).toHaveBeenCalledWith( |
| 220 | + requestMock, |
| 221 | + mockResource.id, |
| 222 | + dtoUpdateMock, |
| 223 | + ); |
| 224 | + }); |
| 225 | + it("should throw notFound exception for invalid resourceId", async () => { |
| 226 | + mockResourcesService.updateResource.mockRejectedValueOnce( |
| 227 | + new NotFoundException(), |
| 228 | + ); |
| 229 | + await expect( |
| 230 | + controller.updateResource( |
| 231 | + requestMock, |
| 232 | + invalidTeamId, |
| 233 | + dtoUpdateMock, |
| 234 | + ), |
| 235 | + ).rejects.toThrow(NotFoundException); |
| 236 | + expect(mockResourcesService.updateResource).toHaveBeenCalledWith( |
| 237 | + requestMock, |
| 238 | + invalidTeamId, |
| 239 | + dtoUpdateMock, |
| 240 | + ); |
| 241 | + }); |
| 242 | + }); |
| 243 | + |
| 244 | + describe("Remove Resource", () => { |
| 245 | + it("removeResource service should be defined", async () => { |
| 246 | + expect(controller.removeResource).toBeDefined(); |
| 247 | + }); |
| 248 | + |
| 249 | + it("should delete a resource", async () => { |
| 250 | + mockResourcesService.removeResource.mockResolvedValueOnce( |
| 251 | + mockResource, |
| 252 | + ); |
| 253 | + |
| 254 | + const result = await controller.removeResource(requestMock, 1); |
| 255 | + |
| 256 | + expect(result).toEqual(mockResource); |
| 257 | + expect(mockResourcesService.removeResource).toHaveBeenCalledWith( |
| 258 | + requestMock, |
| 259 | + mockResource.id, |
| 260 | + ); |
| 261 | + }); |
| 262 | + it("should throw notFound exception for invalid resourceId", async () => { |
| 263 | + mockResourcesService.removeResource.mockRejectedValueOnce( |
| 264 | + new NotFoundException(), |
| 265 | + ); |
| 266 | + await expect( |
| 267 | + controller.removeResource(requestMock, invalidTeamId), |
| 268 | + ).rejects.toThrow(NotFoundException); |
| 269 | + expect(mockResourcesService.removeResource).toHaveBeenCalledWith( |
| 270 | + requestMock, |
| 271 | + invalidTeamId, |
| 272 | + ); |
| 273 | + }); |
| 274 | + }); |
25 | 275 | }); |
0 commit comments