diff --git a/src/resolvers/MutationResolvers.ts b/src/resolvers/MutationResolvers.ts index 93ae763..90dd4c8 100644 --- a/src/resolvers/MutationResolvers.ts +++ b/src/resolvers/MutationResolvers.ts @@ -1,4 +1,4 @@ -import { NotificationInput, NotificationResponse, Resolvers } from "../generated/graphql"; +import { NotificationResponse, Resolvers } from "../generated/graphql"; import { ServerContext } from "../ServerContext"; import { ETANotificationScheduler, diff --git a/test/resolvers/MutationResolverTests.test.ts b/test/resolvers/MutationResolverTests.test.ts index d4f5318..83eb1ef 100644 --- a/test/resolvers/MutationResolverTests.test.ts +++ b/test/resolvers/MutationResolverTests.test.ts @@ -6,7 +6,7 @@ import { addMockSystemToRepository } from "../testHelpers/repositorySetupHelpers"; import assert = require("node:assert"); -import { NotificationInput } from "../../src/generated/graphql"; +import { NotificationSchedulingArguments } from "../../src/generated/graphql"; describe("MutationResolvers", () => { const holder = setupTestServerHolder() @@ -25,7 +25,7 @@ describe("MutationResolvers", () => { describe("scheduleNotification", () => { const query = ` - mutation ScheduleNotification($input: NotificationInput!) { + mutation ScheduleNotification($input: NotificationSchedulingArguments!) { scheduleNotification(input: $input) { success message @@ -38,7 +38,7 @@ describe("MutationResolvers", () => { } ` - function assertFailedResponse(response: any, notificationInput: NotificationInput) { + function assertFailedResponse(response: any, notificationInput: NotificationSchedulingArguments) { assert(response.body.kind === "single"); expect(response.body.singleResult.errors).toBeUndefined(); const notificationResponse = response.body.singleResult.data?.scheduleNotification as any; @@ -53,6 +53,33 @@ describe("MutationResolvers", () => { const shuttle = await addMockShuttleToRepository(context.repository, system.id); const stop = await addMockStopToRepository(context.repository, system.id); + const notificationInput = { + deviceId: "1", + shuttleId: shuttle.id, + stopId: stop.id, + secondsThreshold: 240, + }; + const response = await getServerResponse(query, notificationInput); + + assert(response.body.kind === "single"); + expect(response.body.singleResult.errors).toBeUndefined(); + + const expectedNotificationData: any = { + ...notificationInput, + } + delete expectedNotificationData.secondsThreshold; + const notificationResponse = response.body.singleResult.data?.scheduleNotification as any; + expect(notificationResponse?.success).toBe(true); + expect(notificationResponse?.data).toEqual(expectedNotificationData); + + expect(context.notificationService.getSecondsThresholdForScheduledNotification(expectedNotificationData)).toBe(240); + }); + + it("adds a notification with the default seconds threshold if none is provided", async () => { + const system = await addMockSystemToRepository(context.repository); + const shuttle = await addMockShuttleToRepository(context.repository, system.id); + const stop = await addMockStopToRepository(context.repository, system.id); + const notificationInput = { deviceId: "1", shuttleId: shuttle.id, @@ -65,9 +92,8 @@ describe("MutationResolvers", () => { const notificationResponse = response.body.singleResult.data?.scheduleNotification as any; expect(notificationResponse?.success).toBe(true); - expect(notificationResponse?.data).toEqual(notificationInput); - expect(context.notificationService.isNotificationScheduled(notificationInput)).toBe(true); + expect(context.notificationService.getSecondsThresholdForScheduledNotification(notificationInput)).toBe(180); }); it("fails if the shuttle ID doesn't exist", async () => { @@ -100,7 +126,7 @@ describe("MutationResolvers", () => { describe("cancelNotification", () => { const query = ` - mutation CancelNotification($input: NotificationInput!) { + mutation CancelNotification($input: NotificationLookupArguments!) { cancelNotification(input: $input) { success message @@ -118,23 +144,29 @@ describe("MutationResolvers", () => { const shuttle = await addMockShuttleToRepository(context.repository, system.id); const stop = await addMockStopToRepository(context.repository, system.id); - const notificationInput = { + const notificationInput: any = { deviceId: "1", shuttleId: shuttle.id, stopId: stop.id, + secondsThreshold: 180, } await context.notificationService.scheduleNotification(notificationInput); - const response = await getServerResponse(query, notificationInput); + const notificationLookup = { + ...notificationInput + } + delete notificationLookup.secondsThreshold; + + const response = await getServerResponse(query, notificationLookup); assert(response.body.kind === "single"); expect(response.body.singleResult.errors).toBeUndefined(); const notificationResponse = response.body.singleResult.data?.cancelNotification as any; expect(notificationResponse.success).toBe(true); - expect(notificationResponse.data).toEqual(notificationInput); + expect(notificationResponse.data).toEqual(notificationLookup); - expect(context.notificationService.isNotificationScheduled(notificationInput)).toBe(false); + expect(context.notificationService.isNotificationScheduled(notificationLookup)).toBe(false); }); it("fails if the notification doesn't exist", async () => {