From 6d1a85c2b4576242d9182235e25ed932f0e623ee Mon Sep 17 00:00:00 2001 From: Brendan Chen Date: Mon, 10 Feb 2025 14:02:49 -0800 Subject: [PATCH] use 403 status for non-successful mock --- .../services/NotificationServiceTests.test.ts | 39 +++++++++++-------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/test/services/NotificationServiceTests.test.ts b/test/services/NotificationServiceTests.test.ts index 779c615..9ef66c6 100644 --- a/test/services/NotificationServiceTests.test.ts +++ b/test/services/NotificationServiceTests.test.ts @@ -42,6 +42,26 @@ async function waitForMilliseconds(ms: number): Promise { await new Promise((resolve) => setTimeout(resolve, ms)); } +function mockHttp2Connect(status: number) { + 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': status }); + }, 10); + }); + return mockRequest; + }); + + close() {}; + } + + (http2.connect as jest.Mock) = jest.fn(() => new MockClient()); +} + describe("NotificationService", () => { let repository: UnoptimizedInMemoryRepository let notificationService: NotificationService; @@ -63,23 +83,7 @@ describe("NotificationService", () => { }); 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()); + mockHttp2Connect(200); }); describe("reloadAPNsTokenIfTimePassed", () => { @@ -178,6 +182,7 @@ describe("NotificationService", () => { const shuttle = await addMockShuttleToRepository(repository, "1"); const stop = await addMockStopToRepository(repository, "1"); const { eta, notificationData1 } = generateNotificationDataAndEta(shuttle, stop) + mockHttp2Connect(403); // Act await notificationService.scheduleNotification(notificationData1);