From 4ffdd35b2107acb479f5d0e6fbd87e2e53c5cdbf Mon Sep 17 00:00:00 2001 From: Brendan Chen Date: Tue, 18 Nov 2025 20:02:06 -0800 Subject: [PATCH] Add other tests (one for multiple events, one for not emitting the event) --- .../ShuttleRepositorySharedTests.test.ts | 77 ++++++++++++++++--- 1 file changed, 67 insertions(+), 10 deletions(-) diff --git a/src/repositories/shuttle/__tests__/ShuttleRepositorySharedTests.test.ts b/src/repositories/shuttle/__tests__/ShuttleRepositorySharedTests.test.ts index f12f4d9..e8e4aa2 100644 --- a/src/repositories/shuttle/__tests__/ShuttleRepositorySharedTests.test.ts +++ b/src/repositories/shuttle/__tests__/ShuttleRepositorySharedTests.test.ts @@ -769,27 +769,84 @@ describe.each(repositoryImplementations)('$name', (holder) => { }); describe("SHUTTLE_WILL_LEAVE_STOP event", () => { - test("emits SHUTTLE_WILL_LEAVE_EVENT as a shuttle is leaving a stop", async () => { + test("emits SHUTTLE_WILL_LEAVE_STOP event when shuttle leaves a stop", async () => { const { stop1, sampleShuttleNotInRepository: shuttle } = await setupRouteAndOrderedStops(); shuttle.coordinates = stop1.coordinates; const listener = jest.fn(); - repository.addListener(ShuttleRepositoryEvent.SHUTTLE_WILL_LEAVE_STOP, listener); + repository.on(ShuttleRepositoryEvent.SHUTTLE_WILL_LEAVE_STOP, listener); // Simulate arrival at stop 1 - await repository.addOrUpdateShuttle(shuttle); + const arrivalTime = new Date("2024-01-15T10:30:00Z"); + await repository.addOrUpdateShuttle(shuttle, arrivalTime.getTime()); - shuttle.coordinates = { latitude: 12.5, longitude: 22.5 }; + shuttle.coordinates = { latitude: 12.5, longitude: 22.5 }; // Not at any stop // Simulate leaving stop 1 + const leaveTime = new Date("2024-01-15T10:32:00Z"); + await repository.addOrUpdateShuttle(shuttle, leaveTime.getTime()); + + expect(listener).toHaveBeenCalledTimes(1); + const emittedPayload = listener.mock.calls[0][0] as any; + expect(emittedPayload.stopArrivalThatShuttleIsLeaving).toEqual({ + shuttleId: shuttle.id, + stopId: stop1.id, + timestamp: arrivalTime, + }); + }); + + test("does not emit event when shuttle was not at a stop", async () => { + const { sampleShuttleNotInRepository } = await setupRouteAndOrderedStops(); + + const listener = jest.fn(); + repository.on(ShuttleRepositoryEvent.SHUTTLE_WILL_LEAVE_STOP, listener); + + // Start at coordinates not at any stop + const shuttle = { ...sampleShuttleNotInRepository, coordinates: { latitude: 12.5, longitude: 22.5 } }; await repository.addOrUpdateShuttle(shuttle); - expect(listener).toHaveBeenCalledWith({ - stopArrivalThatShuttleIsLeaving: { - shuttleId: shuttle.id, - stopId: stop1.id, - timestamp: expect.any(Date), - }, + // Move to different coordinates; still not at any stop + shuttle.coordinates = { latitude: 13.0, longitude: 23.0 }; + await repository.addOrUpdateShuttle(shuttle); + + expect(listener).toHaveBeenCalledTimes(0); + }); + + test("emits multiple events as shuttle leaves multiple stops", async () => { + const { stop1, stop2, sampleShuttleNotInRepository: shuttle } = await setupRouteAndOrderedStops(); + + const listener = jest.fn(); + repository.on(ShuttleRepositoryEvent.SHUTTLE_WILL_LEAVE_STOP, listener); + + // Arrive at stop1 + shuttle.coordinates = stop1.coordinates; + const firstArrivalTime = new Date("2024-01-15T10:30:00Z"); + await repository.addOrUpdateShuttle(shuttle, firstArrivalTime.getTime()); + + // Leave stop1 and arrive at stop2 + shuttle.coordinates = stop2.coordinates; + const secondArrivalTime = new Date("2024-01-15T10:35:00Z"); + await repository.addOrUpdateShuttle(shuttle, secondArrivalTime.getTime()); + + // Leave stop2 + shuttle.coordinates = { latitude: 12.5, longitude: 22.5 }; // Not at any stop + const secondLeaveTime = new Date("2024-01-15T10:40:00Z"); + await repository.addOrUpdateShuttle(shuttle, secondLeaveTime.getTime()); + + expect(listener).toHaveBeenCalledTimes(2); + + const firstPayload = listener.mock.calls[0][0] as any; + expect(firstPayload.stopArrivalThatShuttleIsLeaving).toEqual({ + shuttleId: shuttle.id, + stopId: stop1.id, + timestamp: firstArrivalTime, + }); + + const secondPayload = listener.mock.calls[1][0] as any; + expect(secondPayload.stopArrivalThatShuttleIsLeaving).toEqual({ + shuttleId: shuttle.id, + stopId: stop2.id, + timestamp: secondArrivalTime, }); }); });