|
| 1 | +import { Test, TestingModule } from "@nestjs/testing"; |
| 2 | +import { NotFoundException } from "@nestjs/common"; |
| 3 | +import { ConfigService } from "@nestjs/config"; |
| 4 | +import { EventsController } from "./event.controller"; |
| 5 | +import { EventsService } from "./event.service"; |
| 6 | +import { ViewService } from "../view/view.service"; |
| 7 | +import { AbilitiesGuard } from "../auth/ability/abilities.guard"; |
| 8 | +import { |
| 9 | + mockConfigService, |
| 10 | + mockEventsService, |
| 11 | + mockViewService, |
| 12 | +} from "../mocks/EventMock"; |
| 13 | + |
| 14 | +describe("EventsController (Unit)", () => { |
| 15 | + let controller: EventsController; |
| 16 | + |
| 17 | + beforeEach(async () => { |
| 18 | + const testingModule: TestingModule = await Test.createTestingModule({ |
| 19 | + controllers: [EventsController], |
| 20 | + providers: [ |
| 21 | + { provide: ConfigService, useValue: mockConfigService }, |
| 22 | + { provide: EventsService, useValue: mockEventsService }, |
| 23 | + { provide: ViewService, useValue: mockViewService }, |
| 24 | + ], |
| 25 | + }) |
| 26 | + .overrideGuard(AbilitiesGuard) |
| 27 | + .useValue({}) |
| 28 | + .compile(); |
| 29 | + |
| 30 | + controller = testingModule.get<EventsController>(EventsController); |
| 31 | + }); |
| 32 | + |
| 33 | + beforeEach(() => { |
| 34 | + jest.clearAllMocks(); |
| 35 | + }); |
| 36 | + |
| 37 | + describe("create", () => { |
| 38 | + it("should delegate creation to eventsService", async () => { |
| 39 | + const dto = { name: "Event" }; |
| 40 | + const created = { _id: "e1", ...dto }; |
| 41 | + mockEventsService.create.mockResolvedValue(created); |
| 42 | + |
| 43 | + const result = await controller.create(dto as any); |
| 44 | + |
| 45 | + expect(mockEventsService.create).toHaveBeenCalledWith(dto); |
| 46 | + expect(result).toEqual(created); |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + describe("update", () => { |
| 51 | + it("should delegate update to eventsService", async () => { |
| 52 | + const id = "507f1f77bcf86cd799439011"; |
| 53 | + const dto = { name: "Updated" }; |
| 54 | + const updated = { _id: id, ...dto }; |
| 55 | + mockEventsService.update.mockResolvedValue(updated); |
| 56 | + |
| 57 | + const result = await controller.update(id, dto as any); |
| 58 | + |
| 59 | + expect(mockEventsService.update).toHaveBeenCalledWith(id, dto); |
| 60 | + expect(result).toEqual(updated); |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + describe("findAll", () => { |
| 65 | + it("should delegate filtering to eventsService", async () => { |
| 66 | + const query = { page: 0, pageSize: 10, order: "asc", status: "upcoming" }; |
| 67 | + const events = [{ _id: "e1" }]; |
| 68 | + mockEventsService.findAll.mockResolvedValue(events); |
| 69 | + |
| 70 | + const result = await controller.findAll(query as any); |
| 71 | + |
| 72 | + expect(mockEventsService.findAll).toHaveBeenCalledWith(query); |
| 73 | + expect(result).toEqual(events); |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + describe("eventPage", () => { |
| 78 | + it("should render /event-page with parsed query, namespace and site key", async () => { |
| 79 | + const req = { |
| 80 | + url: "/event?foo=bar", |
| 81 | + params: { namespace: "main" }, |
| 82 | + }; |
| 83 | + const res = {}; |
| 84 | + |
| 85 | + await controller.eventPage(req as any, res as any); |
| 86 | + |
| 87 | + expect(mockConfigService.get).toHaveBeenCalledWith("recaptcha_sitekey"); |
| 88 | + expect(mockViewService.render).toHaveBeenCalledWith( |
| 89 | + req, |
| 90 | + res, |
| 91 | + "/event-page", |
| 92 | + expect.objectContaining({ |
| 93 | + foo: "bar", |
| 94 | + nameSpace: "main", |
| 95 | + sitekey: "test-site-key", |
| 96 | + }) |
| 97 | + ); |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + describe("eventViewPage", () => { |
| 102 | + it("should render /event-view-page when event exists", async () => { |
| 103 | + const req = { |
| 104 | + url: "/event/hash-1?lang=en", |
| 105 | + params: { namespace: "main", data_hash: "hash-1" }, |
| 106 | + }; |
| 107 | + const res = {}; |
| 108 | + const fullEvent = { _id: "e1", data_hash: "hash-1" }; |
| 109 | + |
| 110 | + mockEventsService.getFullEventByHash.mockResolvedValue(fullEvent); |
| 111 | + |
| 112 | + await controller.eventViewPage(req as any, res as any); |
| 113 | + |
| 114 | + expect(mockEventsService.getFullEventByHash).toHaveBeenCalledWith("hash-1"); |
| 115 | + expect(mockConfigService.get).toHaveBeenCalledWith("recaptcha_sitekey"); |
| 116 | + expect(mockViewService.render).toHaveBeenCalledWith( |
| 117 | + req, |
| 118 | + res, |
| 119 | + "/event-view-page", |
| 120 | + expect.objectContaining({ |
| 121 | + lang: "en", |
| 122 | + fullEvent, |
| 123 | + namespace: "main", |
| 124 | + sitekey: "test-site-key", |
| 125 | + }) |
| 126 | + ); |
| 127 | + }); |
| 128 | + |
| 129 | + it("should throw NotFoundException when service returns empty event", async () => { |
| 130 | + const req = { |
| 131 | + url: "/event/hash-1", |
| 132 | + params: { namespace: "main", data_hash: "hash-1" }, |
| 133 | + }; |
| 134 | + const res = {}; |
| 135 | + |
| 136 | + mockEventsService.getFullEventByHash.mockResolvedValue(null); |
| 137 | + |
| 138 | + await expect(controller.eventViewPage(req as any, res as any)).rejects.toThrow( |
| 139 | + NotFoundException |
| 140 | + ); |
| 141 | + expect(mockViewService.render).not.toHaveBeenCalled(); |
| 142 | + }); |
| 143 | + |
| 144 | + it("should rethrow service errors and log only non-NotFound errors", async () => { |
| 145 | + const req = { |
| 146 | + url: "/event/hash-1", |
| 147 | + params: { namespace: "main", data_hash: "hash-1" }, |
| 148 | + }; |
| 149 | + const res = {}; |
| 150 | + const error = new Error("unexpected"); |
| 151 | + const loggerErrorSpy = jest.spyOn((controller as any).logger, "error"); |
| 152 | + |
| 153 | + mockEventsService.getFullEventByHash.mockRejectedValue(error); |
| 154 | + |
| 155 | + await expect(controller.eventViewPage(req as any, res as any)).rejects.toThrow( |
| 156 | + "unexpected" |
| 157 | + ); |
| 158 | + expect(loggerErrorSpy).toHaveBeenCalled(); |
| 159 | + }); |
| 160 | + }); |
| 161 | +}); |
0 commit comments