mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-17 16:00:32 +00:00
add http2 mock and update tests
This commit is contained in:
@@ -2,12 +2,13 @@ import { beforeEach, describe, expect, it, jest } from "@jest/globals";
|
|||||||
import { NotificationService } from "../../src/services/NotificationService";
|
import { NotificationService } from "../../src/services/NotificationService";
|
||||||
import { UnoptimizedInMemoryRepository } from "../../src/repositories/UnoptimizedInMemoryRepository";
|
import { UnoptimizedInMemoryRepository } from "../../src/repositories/UnoptimizedInMemoryRepository";
|
||||||
import fs from "fs";
|
import fs from "fs";
|
||||||
|
import http2 from "http2";
|
||||||
import { IEta, IShuttle, IStop } from "../../src/entities/entities";
|
import { IEta, IShuttle, IStop } from "../../src/entities/entities";
|
||||||
import { resetGlobalFetchMockJson, updateGlobalFetchMockJson } from "../testHelpers/fetchMockHelpers";
|
|
||||||
import { addMockShuttleToRepository, addMockStopToRepository } from "../testHelpers/repositorySetupHelpers";
|
import { addMockShuttleToRepository, addMockStopToRepository } from "../testHelpers/repositorySetupHelpers";
|
||||||
|
import EventEmitter = require("node:events");
|
||||||
|
|
||||||
jest.mock("fs");
|
jest.mock("fs");
|
||||||
jest.mock("node:http2");
|
jest.mock("http2");
|
||||||
|
|
||||||
const sampleKey = `-----BEGIN PRIVATE KEY-----
|
const sampleKey = `-----BEGIN PRIVATE KEY-----
|
||||||
MIGTAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBHkwdwIBAQQgsrmSAZIagOfCP8sB
|
MIGTAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBHkwdwIBAQQgsrmSAZIagOfCP8sB
|
||||||
@@ -59,9 +60,27 @@ describe("NotificationService", () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
(fs.readFileSync as jest.Mock).mockReturnValue(sampleKey);
|
(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", () => {
|
describe("reloadAPNsTokenIfTimePassed", () => {
|
||||||
it("reloads the token if token hasn't been generated yet", async () => {
|
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);
|
const { eta, notificationData1, notificationData2 } = generateNotificationDataAndEta(shuttle, stop);
|
||||||
|
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
await notificationService.scheduleNotification(notificationData1);
|
await notificationService.scheduleNotification(notificationData1);
|
||||||
await notificationService.scheduleNotification(notificationData2);
|
await notificationService.scheduleNotification(notificationData2);
|
||||||
@@ -131,7 +149,6 @@ describe("NotificationService", () => {
|
|||||||
// wait for the change to occur first
|
// wait for the change to occur first
|
||||||
await waitForCondition(() => !notificationService.isNotificationScheduled(notificationData1));
|
await waitForCondition(() => !notificationService.isNotificationScheduled(notificationData1));
|
||||||
|
|
||||||
expect(fetch as jest.Mock).toHaveBeenCalledTimes(2);
|
|
||||||
const isFirstNotificationScheduled = notificationService.isNotificationScheduled(notificationData1);
|
const isFirstNotificationScheduled = notificationService.isNotificationScheduled(notificationData1);
|
||||||
const isSecondNotificationScheduled = notificationService.isNotificationScheduled(notificationData2);
|
const isSecondNotificationScheduled = notificationService.isNotificationScheduled(notificationData2);
|
||||||
// No longer scheduled after being sent
|
// No longer scheduled after being sent
|
||||||
@@ -214,7 +231,7 @@ describe("NotificationService", () => {
|
|||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
await waitForMilliseconds(500);
|
await waitForMilliseconds(500);
|
||||||
expect(fetch as jest.Mock).toHaveBeenCalledTimes(0);
|
expect(http2.connect as jest.Mock).toHaveBeenCalledTimes(0);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user