mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-17 07:50:31 +00:00
197 lines
5.4 KiB
TypeScript
197 lines
5.4 KiB
TypeScript
import { beforeEach, describe, expect, it } from "@jest/globals";
|
|
import { generateMockEtas, generateMockRoutes } from "../testHelpers/mockDataGenerators";
|
|
import { IShuttle, IPassioSystem } from "../../src/entities/entities";
|
|
import { setupTestServerContext, setupTestServerHolder } from "../testHelpers/apolloTestServerHelpers";
|
|
import { addMockShuttleToRepository, addMockSystemToRepository } from "../testHelpers/repositorySetupHelpers";
|
|
import assert = require("node:assert");
|
|
|
|
|
|
describe("ShuttleResolvers", () => {
|
|
const holder = setupTestServerHolder();
|
|
const context = setupTestServerContext();
|
|
|
|
let mockSystem: IPassioSystem;
|
|
let mockShuttle: IShuttle;
|
|
|
|
beforeEach(async () => {
|
|
mockSystem = await addMockSystemToRepository(context.shuttleRepository);
|
|
mockShuttle = await addMockShuttleToRepository(context.shuttleRepository,
|
|
mockSystem.id);
|
|
});
|
|
|
|
|
|
async function addMockEtas(shuttleId: string) {
|
|
const etas = generateMockEtas();
|
|
await Promise.all(etas.map(async (eta) => {
|
|
eta.shuttleId = shuttleId;
|
|
await context.shuttleRepository.addOrUpdateEta(eta);
|
|
}));
|
|
return etas;
|
|
}
|
|
|
|
describe("eta", () => {
|
|
const query = `
|
|
query GetShuttleETAs($systemId: ID!, $shuttleId: ID!, $stopId: ID!)
|
|
{
|
|
system(id: $systemId) {
|
|
shuttle(id: $shuttleId) {
|
|
eta(forStopId: $stopId) {
|
|
secondsRemaining
|
|
}
|
|
}
|
|
}
|
|
}
|
|
`
|
|
it("returns ETA data for stop ID if exists", async () => {
|
|
const etas = await addMockEtas(mockShuttle.id);
|
|
|
|
const mockEta = etas[1];
|
|
|
|
// Act
|
|
const response = await holder.testServer.executeOperation({
|
|
query,
|
|
variables: {
|
|
systemId: mockSystem.id,
|
|
shuttleId: mockShuttle.id,
|
|
stopId: mockEta.stopId,
|
|
},
|
|
}, {
|
|
contextValue: context
|
|
|
|
});
|
|
|
|
// Assert
|
|
assert(response.body.kind === "single");
|
|
expect(response.body.singleResult.errors).toBeUndefined();
|
|
expect((response.body.singleResult.data as
|
|
any).system.shuttle.eta.secondsRemaining).toEqual(mockEta.secondsRemaining);
|
|
});
|
|
|
|
it("returns null if it doesn't exist", async () => {
|
|
const response = await holder.testServer.executeOperation({
|
|
query,
|
|
variables: {
|
|
systemId: mockSystem.id,
|
|
shuttleId: mockShuttle.id,
|
|
stopId: "nonexistent-stop",
|
|
}
|
|
}, {
|
|
contextValue: context
|
|
|
|
});
|
|
|
|
// Assert
|
|
assert(response.body.kind === "single");
|
|
expect(response.body.singleResult.errors).toBeUndefined();
|
|
expect((response.body.singleResult.data as
|
|
any).system.shuttle.eta).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe("etas", () => {
|
|
const query = `
|
|
query GetShuttleETAs($systemId: ID!, $shuttleId: ID!)
|
|
{
|
|
system(id: $systemId) {
|
|
shuttle(id: $shuttleId) {
|
|
etas {
|
|
secondsRemaining
|
|
}
|
|
}
|
|
}
|
|
}
|
|
`
|
|
|
|
it("returns associated ETAs if they exist for the shuttle", async () => {
|
|
const etas = await addMockEtas(mockShuttle.id);
|
|
|
|
const response = await holder.testServer.executeOperation({
|
|
query,
|
|
variables: {
|
|
systemId: mockSystem.id,
|
|
shuttleId: mockShuttle.id,
|
|
},
|
|
}, {
|
|
contextValue: context
|
|
|
|
});
|
|
|
|
assert(response.body.kind === "single");
|
|
expect(response.body.singleResult.errors).toBeUndefined();
|
|
expect((response.body.singleResult.data as
|
|
any).system.shuttle.etas).toHaveLength(etas.length);
|
|
});
|
|
|
|
it("returns empty array if no ETAs exist", async () => {
|
|
const response = await holder.testServer.executeOperation({
|
|
query,
|
|
variables: {
|
|
systemId: mockSystem.id,
|
|
shuttleId: mockShuttle.id,
|
|
},
|
|
}, {
|
|
contextValue: context
|
|
|
|
});
|
|
|
|
assert(response.body.kind === "single");
|
|
expect(response.body.singleResult.errors).toBeUndefined();
|
|
expect((response.body.singleResult.data as
|
|
any).system.shuttle.etas).toHaveLength(0);
|
|
|
|
});
|
|
});
|
|
describe("route", () => {
|
|
const query = `
|
|
query GetShuttleRoute($systemId: ID!, $shuttleId: ID!) {
|
|
system(id: $systemId) {
|
|
shuttle(id: $shuttleId) {
|
|
route {
|
|
color
|
|
id
|
|
name
|
|
polylineCoordinates {
|
|
latitude
|
|
longitude
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
`
|
|
|
|
async function getResponseForQuery() {
|
|
return await holder.testServer.executeOperation({
|
|
query,
|
|
variables: {
|
|
systemId: mockSystem.id,
|
|
shuttleId: mockShuttle.id,
|
|
}
|
|
}, {
|
|
contextValue: context
|
|
|
|
});
|
|
}
|
|
|
|
it("returns the route if it exists", async () => {
|
|
const mockRoute = generateMockRoutes()[0];
|
|
await context.shuttleRepository.addOrUpdateRoute(mockRoute);
|
|
|
|
const response = await getResponseForQuery();
|
|
|
|
assert(response.body.kind === "single");
|
|
expect(response.body.singleResult.errors).toBeUndefined();
|
|
expect((response.body.singleResult.data as any).system.shuttle.route.id).toEqual(mockRoute.id);
|
|
});
|
|
|
|
it("returns null if there is no route", async () => {
|
|
const response = await getResponseForQuery();
|
|
|
|
assert(response.body.kind === "single");
|
|
expect(response.body.singleResult.errors).toBeUndefined();
|
|
expect((response.body.singleResult.data as any).system.shuttle.route).toBeNull();
|
|
});
|
|
|
|
});
|
|
});
|