Files
project-inter-server/testHelpers/apolloTestServerHelpers.ts

73 lines
2.0 KiB
TypeScript

import { readFileSync } from "fs";
import { ApolloServer } from "@apollo/server";
import { MergedResolvers } from "../src/MergedResolvers";
import { beforeEach } from "@jest/globals";
import { ServerContext } from "../src/ServerContext";
import { InterchangeSystem, InterchangeSystemBuilderArguments } from "../src/entities/InterchangeSystem";
import {
ChapmanApiBasedParkingRepositoryLoader
} from "../src/loaders/parking/ChapmanApiBasedParkingRepositoryLoader";
function setUpTestServer() {
// Leaving this separate from the main server in case
// configuration changes
const typeDefs = readFileSync("./schema.graphqls", "utf8");
return new ApolloServer({
typeDefs,
resolvers: MergedResolvers,
});
}
const systemInfoForTesting: InterchangeSystemBuilderArguments = {
id: "1",
name: "Chapman University",
passioSystemId: "263",
parkingSystemId: ChapmanApiBasedParkingRepositoryLoader.id,
useSelfUpdatingEtas: false,
shuttleStopArrivalDegreeDelta: 0.001,
initialRegion: {
topLeft: { latitude: 33.85733, longitude: -117.89553 },
bottomRight: { latitude: 33.73970, longitude: -117.81878 },
},
};
export function buildSystemForTesting() {
return InterchangeSystem.buildForTesting(
systemInfoForTesting,
);
}
/**
* Returns a `ServerContext` object which can be passed to requests
* for testing.
*/
export function setupTestServerContext() {
const context: { [key: string] : any } = {};
beforeEach(() => {
context.systems = [
buildSystemForTesting(),
];
context.findSystemById = (_: string) => context.systems[0];
});
return context as ServerContext;
}
/**
* Returns an object which holds a test server.
* This server is reset before every test.
* Tests should keep a reference to the holder object,
* and not destructure it.
*/
export function setupTestServerHolder() {
const holder: { [key: string]: any } = {};
beforeEach(() => {
holder.testServer = setUpTestServer();
});
return holder as { testServer: ApolloServer };
}