update tests for mutation resolvers

This commit is contained in:
2025-03-25 15:42:42 -07:00
parent 14a3738fba
commit 96e7e0297b
2 changed files with 43 additions and 11 deletions

View File

@@ -1,4 +1,4 @@
import { NotificationInput, NotificationResponse, Resolvers } from "../generated/graphql"; import { NotificationResponse, Resolvers } from "../generated/graphql";
import { ServerContext } from "../ServerContext"; import { ServerContext } from "../ServerContext";
import { import {
ETANotificationScheduler, ETANotificationScheduler,

View File

@@ -6,7 +6,7 @@ import {
addMockSystemToRepository addMockSystemToRepository
} from "../testHelpers/repositorySetupHelpers"; } from "../testHelpers/repositorySetupHelpers";
import assert = require("node:assert"); import assert = require("node:assert");
import { NotificationInput } from "../../src/generated/graphql"; import { NotificationSchedulingArguments } from "../../src/generated/graphql";
describe("MutationResolvers", () => { describe("MutationResolvers", () => {
const holder = setupTestServerHolder() const holder = setupTestServerHolder()
@@ -25,7 +25,7 @@ describe("MutationResolvers", () => {
describe("scheduleNotification", () => { describe("scheduleNotification", () => {
const query = ` const query = `
mutation ScheduleNotification($input: NotificationInput!) { mutation ScheduleNotification($input: NotificationSchedulingArguments!) {
scheduleNotification(input: $input) { scheduleNotification(input: $input) {
success success
message message
@@ -38,7 +38,7 @@ describe("MutationResolvers", () => {
} }
` `
function assertFailedResponse(response: any, notificationInput: NotificationInput) { function assertFailedResponse(response: any, notificationInput: NotificationSchedulingArguments) {
assert(response.body.kind === "single"); assert(response.body.kind === "single");
expect(response.body.singleResult.errors).toBeUndefined(); expect(response.body.singleResult.errors).toBeUndefined();
const notificationResponse = response.body.singleResult.data?.scheduleNotification as any; const notificationResponse = response.body.singleResult.data?.scheduleNotification as any;
@@ -53,6 +53,33 @@ describe("MutationResolvers", () => {
const shuttle = await addMockShuttleToRepository(context.repository, system.id); const shuttle = await addMockShuttleToRepository(context.repository, system.id);
const stop = await addMockStopToRepository(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 = { const notificationInput = {
deviceId: "1", deviceId: "1",
shuttleId: shuttle.id, shuttleId: shuttle.id,
@@ -65,9 +92,8 @@ describe("MutationResolvers", () => {
const notificationResponse = response.body.singleResult.data?.scheduleNotification as any; const notificationResponse = response.body.singleResult.data?.scheduleNotification as any;
expect(notificationResponse?.success).toBe(true); 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 () => { it("fails if the shuttle ID doesn't exist", async () => {
@@ -100,7 +126,7 @@ describe("MutationResolvers", () => {
describe("cancelNotification", () => { describe("cancelNotification", () => {
const query = ` const query = `
mutation CancelNotification($input: NotificationInput!) { mutation CancelNotification($input: NotificationLookupArguments!) {
cancelNotification(input: $input) { cancelNotification(input: $input) {
success success
message message
@@ -118,23 +144,29 @@ describe("MutationResolvers", () => {
const shuttle = await addMockShuttleToRepository(context.repository, system.id); const shuttle = await addMockShuttleToRepository(context.repository, system.id);
const stop = await addMockStopToRepository(context.repository, system.id); const stop = await addMockStopToRepository(context.repository, system.id);
const notificationInput = { const notificationInput: any = {
deviceId: "1", deviceId: "1",
shuttleId: shuttle.id, shuttleId: shuttle.id,
stopId: stop.id, stopId: stop.id,
secondsThreshold: 180,
} }
await context.notificationService.scheduleNotification(notificationInput); 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"); assert(response.body.kind === "single");
expect(response.body.singleResult.errors).toBeUndefined(); expect(response.body.singleResult.errors).toBeUndefined();
const notificationResponse = response.body.singleResult.data?.cancelNotification as any; const notificationResponse = response.body.singleResult.data?.cancelNotification as any;
expect(notificationResponse.success).toBe(true); 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 () => { it("fails if the notification doesn't exist", async () => {