From 31e9c78ebd8939d4646d158359a216177e6e15dc Mon Sep 17 00:00:00 2001 From: Brendan Chen Date: Wed, 30 Apr 2025 18:09:29 -0700 Subject: [PATCH] add test for closing connection on emitting closure events --- .../AppleNotificationSenderTests.test.ts | 59 ++++++++++++++----- 1 file changed, 43 insertions(+), 16 deletions(-) diff --git a/test/notifications/senders/AppleNotificationSenderTests.test.ts b/test/notifications/senders/AppleNotificationSenderTests.test.ts index 0ccf20d..67c38f3 100644 --- a/test/notifications/senders/AppleNotificationSenderTests.test.ts +++ b/test/notifications/senders/AppleNotificationSenderTests.test.ts @@ -5,29 +5,36 @@ import { AppleNotificationSender, NotificationAlertArguments } from "../../../src/notifications/senders/AppleNotificationSender"; +import { ClientHttp2Session } from "node:http2"; jest.mock("http2"); const sampleKeyBase64 = "LS0tLS1CRUdJTiBQUklWQVRFIEtFWS0tLS0tCk1JR1RBZ0VBTUJNR0J5cUdTTTQ5QWdFR0NDcUdTTTQ5QXdFSEJIa3dkd0lCQVFRZ3NybVNBWklhZ09mQ1A4c0IKV2kyQ0JYRzFPbzd2MWJpc3BJWkN3SXI0UkRlZ0NnWUlLb1pJemowREFRZWhSQU5DQUFUWkh4VjJ3UUpMTUJxKwp5YSt5ZkdpM2cyWlV2NmhyZmUrajA4eXRla1BIalhTMHF6Sm9WRUx6S0hhNkVMOVlBb1pEWEJ0QjZoK2ZHaFhlClNPY09OYmFmCi0tLS0tRU5EIFBSSVZBVEUgS0VZLS0tLS0K"; -function mockHttp2Connect(status: number) { - class MockClient extends EventEmitter { - request = jest.fn((_) => { - 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() {}; +class MockClient extends EventEmitter { + constructor( + private status: number, + ) { + super() } - (http2.connect as jest.Mock) = jest.fn(() => new MockClient()); + request = jest.fn((_) => { + const mockRequest: any = new EventEmitter(); + mockRequest.setEncoding = jest.fn(); + mockRequest.write = jest.fn(); + mockRequest.end = jest.fn(() => { + setTimeout(() => { + mockRequest.emit('response', { ':status': this.status }); + }, 10); + }); + return mockRequest; + }); + + close = jest.fn(() => {}); +} + +function mockHttp2Connect(status: number) { + (http2.connect as jest.Mock) = jest.fn(() => new MockClient(status)); } describe("AppleNotificationSender", () => { @@ -159,5 +166,25 @@ describe("AppleNotificationSender", () => { expect(http2.connect).not.toHaveBeenCalled(); expect(result).toBe(true); }); + + it("registers a handler to close the connection if `close` event fired", async () => { + const connectionCloseEvents = ['close', 'goaway', 'error']; + + await Promise.all(connectionCloseEvents.map(async (event) => { + const mockClient = new MockClient(200); + notificationSender = new AppleNotificationSender(true, mockClient as unknown as ClientHttp2Session); + + const notificationArguments: NotificationAlertArguments = { + title: 'Test notification', + body: '' + }; + + const result = await notificationSender.sendNotificationImmediately('1', notificationArguments); + + mockClient.emit(event); + + expect(mockClient.close).toHaveBeenCalled(); + })); + }); }); });