From 764f6e35f07b4c484ef65c5ba800dfefb6de4f82 Mon Sep 17 00:00:00 2001 From: Brendan Chen Date: Mon, 10 Feb 2025 13:56:42 -0800 Subject: [PATCH] add http2 mock and update tests --- .../services/NotificationServiceTests.test.ts | 31 ++++++++++++++----- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/test/services/NotificationServiceTests.test.ts b/test/services/NotificationServiceTests.test.ts index 32e16cd..779c615 100644 --- a/test/services/NotificationServiceTests.test.ts +++ b/test/services/NotificationServiceTests.test.ts @@ -2,12 +2,13 @@ import { beforeEach, describe, expect, it, jest } from "@jest/globals"; import { NotificationService } from "../../src/services/NotificationService"; import { UnoptimizedInMemoryRepository } from "../../src/repositories/UnoptimizedInMemoryRepository"; import fs from "fs"; +import http2 from "http2"; import { IEta, IShuttle, IStop } from "../../src/entities/entities"; -import { resetGlobalFetchMockJson, updateGlobalFetchMockJson } from "../testHelpers/fetchMockHelpers"; import { addMockShuttleToRepository, addMockStopToRepository } from "../testHelpers/repositorySetupHelpers"; +import EventEmitter = require("node:events"); jest.mock("fs"); -jest.mock("node:http2"); +jest.mock("http2"); const sampleKey = `-----BEGIN PRIVATE KEY----- MIGTAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBHkwdwIBAQQgsrmSAZIagOfCP8sB @@ -59,9 +60,27 @@ describe("NotificationService", () => { }; (fs.readFileSync as jest.Mock).mockReturnValue(sampleKey); + }); - resetGlobalFetchMockJson(); - }) + beforeEach(() => { + class MockClient extends EventEmitter { + request = jest.fn((headers: any) => { + const mockRequest: any = new EventEmitter(); + mockRequest.setEncoding = jest.fn(); + mockRequest.write = jest.fn(); + mockRequest.end = jest.fn(() => { + setTimeout(() => { + mockRequest.emit('response', { ':status': 200 }); + }, 10); + }); + return mockRequest; + }); + + close() {}; + } + + (http2.connect as jest.Mock) = jest.fn(() => new MockClient()); + }); describe("reloadAPNsTokenIfTimePassed", () => { it("reloads the token if token hasn't been generated yet", async () => { @@ -120,7 +139,6 @@ describe("NotificationService", () => { const { eta, notificationData1, notificationData2 } = generateNotificationDataAndEta(shuttle, stop); - // Act await notificationService.scheduleNotification(notificationData1); await notificationService.scheduleNotification(notificationData2); @@ -131,7 +149,6 @@ describe("NotificationService", () => { // wait for the change to occur first await waitForCondition(() => !notificationService.isNotificationScheduled(notificationData1)); - expect(fetch as jest.Mock).toHaveBeenCalledTimes(2); const isFirstNotificationScheduled = notificationService.isNotificationScheduled(notificationData1); const isSecondNotificationScheduled = notificationService.isNotificationScheduled(notificationData2); // No longer scheduled after being sent @@ -214,7 +231,7 @@ describe("NotificationService", () => { // Assert await waitForMilliseconds(500); - expect(fetch as jest.Mock).toHaveBeenCalledTimes(0); + expect(http2.connect as jest.Mock).toHaveBeenCalledTimes(0); }); }); });