add http2 mock and update tests

This commit is contained in:
2025-02-10 13:56:42 -08:00
parent b5954e251d
commit 764f6e35f0

View File

@@ -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);
});
});
});