mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-17 07:50:31 +00:00
Move testHelpers directory up one level
This commit is contained in:
66
testHelpers/apolloTestServerHelpers.ts
Normal file
66
testHelpers/apolloTestServerHelpers.ts
Normal file
@@ -0,0 +1,66 @@
|
||||
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 } 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 = {
|
||||
id: "1",
|
||||
name: "Chapman University",
|
||||
passioSystemId: "263",
|
||||
parkingSystemId: ChapmanApiBasedParkingRepositoryLoader.id,
|
||||
};
|
||||
|
||||
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 };
|
||||
}
|
||||
Reference in New Issue
Block a user