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

158 lines
4.4 KiB
TypeScript

import { describe, expect, it } from "@jest/globals";
import { generateMockSystems } from "../testHelpers/mockDataGenerators";
import { setupTestServerContext, setupTestServerHolder } from "../testHelpers/apolloTestServerHelpers";
import assert = require("node:assert");
import { ScheduledNotificationData } from "../../src/services/NotificationService";
import { addMockShuttleToRepository, addMockStopToRepository } from "../testHelpers/repositorySetupHelpers";
// See Apollo documentation for integration test guide
// https://www.apollographql.com/docs/apollo-server/testing/testing
describe("QueryResolvers", () => {
const holder = setupTestServerHolder();
const context = setupTestServerContext();
async function addMockSystems() {
const systems = generateMockSystems();
await Promise.all(systems.map(async (system) => {
await context.repository.addOrUpdateSystem(system);
}));
return systems;
}
describe("systems", () => {
it("returns systems from the repository", async () => {
const systems = await addMockSystems();
const query = `
query GetSystems
{
systems {
name
}
}
`;
const response = await holder.testServer.executeOperation({
query,
}, {
contextValue: {
repository: context.repository,
},
});
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 () => {
const systems = await addMockSystems();
const systemToGet = systems[1];
const response = await holder.testServer.executeOperation({
query,
variables: {
id: systemToGet.id,
}
}, {
contextValue: {
repository: context.repository,
}
});
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 () => {
const response = await holder.testServer.executeOperation({
query,
variables: {
id: "nonexistent-id",
}
}, {
contextValue: {
repository: context.repository,
}
});
assert(response.body.kind === "single");
expect(response.body.singleResult.errors).toBeUndefined();
expect(response.body.singleResult.data?.system).toBeNull();
});
});
describe("isNotificationScheduled", () => {
const query = `
query IsNotificationScheduled($input: NotificationInput!) {
isNotificationScheduled(input: $input)
}
`
it("returns true if the notification is scheduled", async () => {
// Arrange
const shuttle = await addMockShuttleToRepository(context.repository, "1");
const stop = await addMockStopToRepository(context.repository, "1")
const notification: ScheduledNotificationData = {
shuttleId: shuttle.id,
stopId: stop.id,
deviceId: "1",
};
await context.notificationService.scheduleNotification(notification);
// Act
const response = await holder.testServer.executeOperation({
query,
variables: {
input: {
...notification,
},
}
}, {
contextValue: context,
});
// Assert
assert(response.body.kind === "single");
expect(response.body.singleResult.errors).toBeUndefined();
expect(response.body.singleResult.data?.isNotificationScheduled).toBe(true);
});
it("returns false 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);
});
});
});