import { beforeEach, describe, expect, it } from "@jest/globals"; import { ApolloServer } from "@apollo/server"; import { ServerContext } from "../../src/ServerContext"; import { UnoptimizedInMemoryRepository } from "../../src/repositories/UnoptimizedInMemoryRepository"; import { setUpTestServer } from "../testHelpers/apolloSetupHelpers"; import { generateMockRoutes, generateMockShuttles, generateMockStops } from "../generators"; import { addMockRouteToRepository, addMockShuttleToRepository, addMockStopToRepository, addMockSystemToRepository } from "../testHelpers/repositorySetupHelpers"; import { ISystem } from "../../src/entities/entities"; import assert = require("node:assert"); describe("SystemResolvers", () => { let testServer: ApolloServer; let repository: UnoptimizedInMemoryRepository; beforeEach(async () => { testServer = setUpTestServer(); repository = new UnoptimizedInMemoryRepository(); }); let mockSystem: ISystem; beforeEach(async () => { mockSystem = await addMockSystemToRepository(repository); }); // TODO: Consolidate these into one single method taking an object async function getResponseFromQueryNeedingSystemId(query: string) { return await testServer.executeOperation({ query, variables: { systemId: mockSystem.id, }, }, { contextValue: { repository }, }); } describe("routes", () => { const query = ` query GetSystemRoutes($systemId: ID!) { system(id: $systemId) { routes { id name } } } `; it("gets routes associated with system id", async () => { const expectedRoutes = generateMockRoutes(); await Promise.all(expectedRoutes.map(async (route) => { route.systemId = mockSystem.id; await repository.addOrUpdateRoute(route); })); const response = await getResponseFromQueryNeedingSystemId(query); assert(response.body.kind === "single"); expect(response.body.singleResult.errors).toBeUndefined() const routes = (response.body.singleResult.data as any).system.routes; expect(routes.length === expectedRoutes.length); }); }); describe("stops", () => { const query = ` query GetSystemStops($systemId: ID!) { system(id: $systemId) { stops { id name } } } `; it("gets stops associated with system id", async () => { const expectedStops = generateMockStops(); await Promise.all(expectedStops.map(async (stop) => { stop.systemId = mockSystem.id; await repository.addOrUpdateStop(stop); })); const response = await getResponseFromQueryNeedingSystemId(query); assert(response.body.kind === "single"); expect(response.body.singleResult.errors).toBeUndefined() const stops = (response.body.singleResult.data as any).system.stops; expect(stops.length === expectedStops.length); }); }); describe("stop", () => { async function getResponseForStopQuery(stopId: string) { const query = ` query GetSystemStop($systemId: ID!, $stopId: ID!) { system(id: $systemId) { stop(id: $stopId) { id name } } } `; return await testServer.executeOperation({ query, variables: { systemId: mockSystem.id, stopId: stopId, }, }, { contextValue: { repository, } }); } it("gets the stop with the correct id", async () => { const mockStop = await addMockStopToRepository(repository, mockSystem.id); const response = await getResponseForStopQuery(mockStop.id); assert(response.body.kind === "single"); expect(response.body.singleResult.errors).toBeUndefined(); const stop = (response.body.singleResult.data as any).system.stop; expect(stop.id).toEqual(mockStop.id); expect(stop.name).toEqual(mockStop.name); }); it("returns null if the stop isn't associated with the system", async () => { const updatedSystem = { ...mockSystem, id: "2", } await repository.addOrUpdateSystem(updatedSystem); const mockStop = await addMockStopToRepository(repository, updatedSystem.id); const response = await getResponseForStopQuery(mockStop.id); assert(response.body.kind === "single"); expect(response.body.singleResult.errors).toBeUndefined(); const stop = (response.body.singleResult.data as any).system.stop; expect(stop).toBeNull(); }); it("returns null if there is no stop", async () => { const response = await getResponseForStopQuery("1"); assert(response.body.kind === "single"); expect(response.body.singleResult.errors).toBeUndefined(); const stop = (response.body.singleResult.data as any).system.stop; expect(stop).toBeNull(); }); }); describe("route", () => { async function getResponseForRouteQuery(routeId: string) { const query = ` query GetSystemRoute($systemId: ID!, $routeId: ID!) { system(id: $systemId) { route(id: $routeId) { id name } } } `; return await testServer.executeOperation({ query, variables: { systemId: mockSystem.id, routeId, }, }, { contextValue: { repository, } }); } it("gets the route with the correct id", async () => { const mockRoute = await addMockRouteToRepository(repository, mockSystem.id); const response = await getResponseForRouteQuery(mockRoute.id); assert(response.body.kind === "single"); expect(response.body.singleResult.errors).toBeUndefined(); const route = (response.body.singleResult.data as any).system.route; expect(route.id).toEqual(mockRoute.id); expect(route.name).toEqual(mockRoute.name); }); it("returns null if the route isn't associated with the system", async () => { const updatedSystem = { ...mockSystem, id: "2", } await repository.addOrUpdateSystem(updatedSystem); const mockRoute = await addMockRouteToRepository(repository, updatedSystem.id); const response = await getResponseForRouteQuery(mockRoute.id); assert(response.body.kind === "single"); expect(response.body.singleResult.errors).toBeUndefined(); const route = (response.body.singleResult.data as any).system.route; expect(route).toBeNull(); }); it("returns null if there is no route", async () => { const response = await getResponseForRouteQuery("nonexistent-route-id"); assert(response.body.kind === "single"); expect(response.body.singleResult.errors).toBeUndefined(); const route = (response.body.singleResult.data as any).system.route; expect(route).toBeNull(); }); }); describe("shuttle", () => { async function getResponseForShuttleQuery(shuttleId: string) { const query = ` query GetSystemShuttle($systemId: ID!, $shuttleId: ID!) { system(id: $systemId) { shuttle(id: $shuttleId) { id name } } } `; return await testServer.executeOperation({ query, variables: { systemId: mockSystem.id, shuttleId: shuttleId, } }, { contextValue: { repository, } }); } it("gets the shuttle with the correct id", async () => { const mockShuttle = await addMockShuttleToRepository(repository, mockSystem.id); const response = await getResponseForShuttleQuery(mockShuttle.id); assert(response.body.kind === "single"); expect(response.body.singleResult.errors).toBeUndefined(); const shuttle = (response.body.singleResult.data as any).system.shuttle; expect(shuttle.id).toEqual(mockShuttle.id); expect(shuttle.name).toEqual(mockShuttle.name); }); it("returns null if the shuttle isn't associated with the system", async () => { const updatedSystem = { ...mockSystem, id: "2", } await repository.addOrUpdateSystem(updatedSystem); const mockShuttle = await addMockShuttleToRepository(repository, updatedSystem.id); const response = await getResponseForShuttleQuery(mockShuttle.id); assert(response.body.kind === "single"); expect(response.body.singleResult.errors).toBeUndefined(); const shuttle = (response.body.singleResult.data as any).system.shuttle; expect(shuttle).toBeNull(); }); it("returns null if there is no shuttle", async () => { const response = await getResponseForShuttleQuery("nonexistent-shuttle-id"); assert(response.body.kind === "single"); expect(response.body.singleResult.errors).toBeUndefined(); const shuttle = (response.body.singleResult.data as any).system.shuttle; expect(shuttle).toBeNull(); }); }); describe("shuttles", () => { const query = ` query GetSystemShuttle($systemId: ID!) { system(id: $systemId) { shuttles { id name } } } `; it("gets shuttles associated with system id", async () => { const expectedShuttles = generateMockShuttles(); await Promise.all(expectedShuttles.map(async (shuttle) => { shuttle.systemId = mockSystem.id; await repository.addOrUpdateShuttle(shuttle); })); const response = await getResponseFromQueryNeedingSystemId(query); assert(response.body.kind === "single"); expect(response.body.singleResult.errors).toBeUndefined() const shuttles = (response.body.singleResult.data as any).system.shuttles; expect(shuttles.length === expectedShuttles.length); }); }); });