From 440bdc9eddca75c1416c4fb16b2d4fe7eb27aa99 Mon Sep 17 00:00:00 2001 From: Brendan Chen Date: Tue, 7 Jan 2025 14:24:21 -0800 Subject: [PATCH] add mocking functions for tests --- src/repositories/ApiBasedRepository.ts | 6 +-- test/repositories/ApiBasedRepositoryTests.ts | 40 ++++++++++++++++++++ 2 files changed, 42 insertions(+), 4 deletions(-) create mode 100644 test/repositories/ApiBasedRepositoryTests.ts diff --git a/src/repositories/ApiBasedRepository.ts b/src/repositories/ApiBasedRepository.ts index 51ebf12..00b1353 100644 --- a/src/repositories/ApiBasedRepository.ts +++ b/src/repositories/ApiBasedRepository.ts @@ -1,16 +1,14 @@ import { GetterRepository } from "./GetterRepository"; import { IEta } from "../entities/entities"; +// TODO: implement + export class ApiBasedRepository implements GetterRepository { public async getEtaForShuttleAndStopId(shuttleId: string, stopId: string): Promise { - // TODO: implement - return null; } public async getEtasForShuttleId(shuttleId: string): Promise<[]> { - // TODO: implement - return []; } diff --git a/test/repositories/ApiBasedRepositoryTests.ts b/test/repositories/ApiBasedRepositoryTests.ts new file mode 100644 index 0000000..4ddabb0 --- /dev/null +++ b/test/repositories/ApiBasedRepositoryTests.ts @@ -0,0 +1,40 @@ +import { beforeEach, describe, jest, test } from "@jest/globals"; + +/** + * Update the global fetch function to return a specific object. + * @param obj + */ +function updateGlobalFetchMockJson(obj: any) { + // @ts-ignore + global.fetch = jest.fn(() => { + return Promise.resolve({ + json: () => Promise.resolve(obj) + }) + }) as jest.Mock; +} + +/** + * Reset the global fetch function mock's JSON to return an empty object. + * @param obj + */ +function resetGlobalFetchMockJson() { + updateGlobalFetchMockJson({}) +} + +beforeEach(() => { + resetGlobalFetchMockJson(); +}) + +describe("getEtaForShuttleAndStopId", () => { + test("getEtaForShuttleAndStopId returns correct ETA data", async () => { + + }); + + test("getEtaForShuttleAndStopId returns null if API call is invalid", async () => { + + }); +}); + +describe("getEtasForShuttleId", () => { + +})