import { beforeEach, describe, expect, it } from "@jest/globals"; import { setupTestServerContext } from "../testHelpers/apolloTestServerHelpers"; import { addMockRouteToRepository, addMockStopToRepository, addMockSystemToRepository } from "../testHelpers/repositorySetupHelpers"; import { generateMockOrderedStops, generateMockShuttles } from "../generators"; import { IRoute, IStop, ISystem } from "../../src/entities/entities"; import assert = require("node:assert"); describe("RouteResolvers", () => { const context = setupTestServerContext(); let mockSystem: ISystem; let mockRoute: IRoute; let mockStop: IStop; beforeEach(async () => { mockSystem = await addMockSystemToRepository(context.repository); const systemId = mockSystem.id; mockRoute = await addMockRouteToRepository(context.repository, systemId); mockStop = await addMockStopToRepository(context.repository, systemId); }); describe("shuttles", () => { async function getResponseForShuttlesQuery() { const query = ` query GetRouteShuttles($systemId: ID!, $routeId: ID!) { system(id: $systemId) { route(id: $routeId) { shuttles { id name } } } } `; return await context.testServer.executeOperation({ query, variables: { systemId: mockSystem.id, routeId: mockRoute.id, }, }, { contextValue: { repository: context.repository, } }); } it("returns shuttle array if there are shuttles", async () => { const expectedShuttles = generateMockShuttles(); const expectedShuttle = expectedShuttles[0]; expectedShuttle.systemId = mockSystem.id; expectedShuttle.routeId = mockRoute.id; await context.repository.addOrUpdateShuttle(expectedShuttle); const response = await getResponseForShuttlesQuery(); assert(response.body.kind === "single"); expect(response.body.singleResult.errors).toBeUndefined() const shuttle = (response.body.singleResult.data as any).system.route.shuttles[0]; expect(shuttle.id).toEqual(expectedShuttle.id); expect(shuttle.name).toEqual(expectedShuttle.name); }); it("returns empty array if there are no shuttles", async () => { const response = await getResponseForShuttlesQuery(); assert(response.body.kind === "single"); expect(response.body.singleResult.errors).toBeUndefined() const shuttles = (response.body.singleResult.data as any).system.route.shuttles; expect(shuttles.length).toEqual(0); }); }); describe("orderedStop", () => { async function getResponseForOrderedStopQuery() { const query = ` query GetRouteOrderedStop($systemId: ID!, $routeId: ID!, $stopId: ID!) { system(id: $systemId) { route(id: $routeId) { orderedStop(forStopId: $stopId) { stopId } } } } `; return await context.testServer.executeOperation({ query, variables: { systemId: mockSystem.id, routeId: mockRoute.id, stopId: mockStop.id, } }, { contextValue: { repository: context.repository, } }); } it("returns ordered stop using provided data", async () => { const orderedStops = generateMockOrderedStops(); const expectedOrderedStop = orderedStops[0]; expectedOrderedStop.stopId = mockStop.id; expectedOrderedStop.routeId = mockRoute.id; await context.repository.addOrUpdateOrderedStop(expectedOrderedStop); const response = await getResponseForOrderedStopQuery(); assert(response.body.kind === "single"); expect(response.body.singleResult.errors).toBeUndefined(); const orderedStop = (response.body.singleResult.data as any).system.route.orderedStop; expect(orderedStop.stopId).toEqual(expectedOrderedStop.stopId); }); it("returns null if the stop doesn't exist", async () => { const orderedStops = generateMockOrderedStops(); const expectedOrderedStop = orderedStops[0]; expectedOrderedStop.stopId = mockStop.id; expectedOrderedStop.routeId = mockRoute.id; await context.repository.addOrUpdateOrderedStop(expectedOrderedStop); await context.repository.removeStopIfExists(mockStop.id); const response = await getResponseForOrderedStopQuery(); assert(response.body.kind === "single"); expect(response.body.singleResult.errors).toBeUndefined(); const orderedStop = (response.body.singleResult.data as any).system.route.orderedStop; expect(orderedStop).toBeNull(); }); }); });