Files
project-inter-server/test/resolvers/QueryResolverTests.test.ts

166 lines
4.7 KiB
TypeScript

import { describe, expect, it } from "@jest/globals";
import {
buildSystemForTesting,
setupTestServerContext,
setupTestServerHolder
} from "../testHelpers/apolloTestServerHelpers";
import assert = require("node:assert");
import { addMockShuttleToRepository, addMockStopToRepository } from "../testHelpers/repositorySetupHelpers";
import { ScheduledNotification } from "../../src/repositories/notifications/NotificationRepository";
// See Apollo documentation for integration test guide
// https://www.apollographql.com/docs/apollo-server/testing/testing
describe("QueryResolvers", () => {
const holder = setupTestServerHolder();
const context = setupTestServerContext();
describe("systems", () => {
it("returns systems from the repository", async () => {
const systems = context.systems;
const query = `
query GetSystems
{
systems {
name
}
}
`;
const response = await holder.testServer.executeOperation({
query,
}, {
contextValue: context
});
assert(response.body.kind === "single");
expect(response.body.singleResult.errors).toBeUndefined();
expect(response.body.singleResult.data?.systems).toHaveLength(systems.length);
});
});
describe("system", () => {
const query = `
query GetSystem($id: ID!)
{
system(id: $id) {
name
}
}
`;
it("returns a system for an ID from the repository", async () => {
context.systems = [
buildSystemForTesting(),
buildSystemForTesting(),
];
context.findSystemById = (_: string) => context.systems[1];
context.systems[1].id = "test-id";
const systems = context.systems;
const systemToGet = systems[1];
const response = await holder.testServer.executeOperation({
query,
variables: {
id: systemToGet.id,
}
}, {
contextValue: context
});
assert(response.body.kind === "single");
expect(response.body.singleResult.errors).toBeUndefined();
expect(response.body.singleResult.data?.system).toBeDefined();
});
it("returns null if there is no system", async () => {
context.findSystemById = (_: string) => null;
const response = await holder.testServer.executeOperation({
query,
variables: {
id: "nonexistent-id",
}
}, {
contextValue: context
});
assert(response.body.kind === "single");
expect(response.body.singleResult.errors).toBeUndefined();
expect(response.body.singleResult.data?.system).toBeNull();
});
});
describe("isNotificationScheduled and secondsThresholdForNotification", () => {
const query = `
query IsNotificationScheduled($input: NotificationInput!) {
isNotificationScheduled(input: $input)
secondsThresholdForNotification(input: $input)
}
`;
it("returns correct data if the notification is scheduled", async () => {
// Arrange
const shuttle = await addMockShuttleToRepository(context.systems[0].shuttleRepository, "1");
const stop = await addMockStopToRepository(context.systems[0].shuttleRepository, "1")
const notification: ScheduledNotification = {
shuttleId: shuttle.id,
stopId: stop.id,
deviceId: "1",
secondsThreshold: 240,
};
await context.systems[0].notificationRepository.addOrUpdateNotification(notification);
const notificationLookup: any = {
...notification,
}
delete notificationLookup.secondsThreshold;
// Act
const response = await holder.testServer.executeOperation({
query,
variables: {
input: notificationLookup,
}
}, {
contextValue: context,
});
// Assert
assert(response.body.kind === "single");
expect(response.body.singleResult.errors).toBeUndefined();
expect(response.body.singleResult.data?.secondsThresholdForNotification).toEqual(240);
expect(response.body.singleResult.data?.isNotificationScheduled).toBe(true);
});
it("returns false/null data if the notification isn't scheduled", async () => {
// Act
const response = await holder.testServer.executeOperation({
query,
variables: {
input: {
shuttleId: "1",
stopId: "1",
deviceId: "1",
},
}
}, {
contextValue: context,
});
// Assert
assert(response.body.kind === "single");
expect(response.body.singleResult.errors).toBeUndefined();
expect(response.body.singleResult.data?.isNotificationScheduled).toBe(false);
expect(response.body.singleResult.data?.secondsThresholdForNotification).toBe(null);
});
});
});