From 803f96ca81fa113a23aa8243dfa438f2f778daa9 Mon Sep 17 00:00:00 2001 From: Matthew Miller Date: Mon, 20 Jun 2022 11:55:30 -0700 Subject: Add tests for WebAuthnAbortService --- .../src/helpers/webAuthnAbortService.test.ts | 42 ++++++++++++++++++++++ .../src/methods/startAuthentication.test.ts | 3 +- .../browser/src/methods/startRegistration.test.ts | 3 +- 3 files changed, 44 insertions(+), 4 deletions(-) create mode 100644 packages/browser/src/helpers/webAuthnAbortService.test.ts (limited to 'packages/browser/src') diff --git a/packages/browser/src/helpers/webAuthnAbortService.test.ts b/packages/browser/src/helpers/webAuthnAbortService.test.ts new file mode 100644 index 0000000..f4e6344 --- /dev/null +++ b/packages/browser/src/helpers/webAuthnAbortService.test.ts @@ -0,0 +1,42 @@ +import { webauthnAbortService } from './webAuthnAbortService'; + +test('should create a new abort signal every time', () => { + const signal1 = webauthnAbortService.createNewAbortSignal(); + const signal2 = webauthnAbortService.createNewAbortSignal(); + + expect(signal2).not.toBe(signal1); +}); + +test('should call abort() on existing controller when creating a new signal', () => { + // Populate `.controller` + webauthnAbortService.createNewAbortSignal(); + + // Spy on the existing instance of AbortController + const abortSpy = jest.fn(); + // @ts-ignore + webauthnAbortService.controller?.abort = abortSpy; + + // Generate a new signal, which should call `abort()` on the existing controller + webauthnAbortService.createNewAbortSignal(); + expect(abortSpy).toHaveBeenCalledTimes(1); +}); + +test('should reset controller', () => { + // Reset the service + webauthnAbortService.reset(); + + // Populate `.controller` + webauthnAbortService.createNewAbortSignal(); + + // Spy on the existing instance of AbortController + const abortSpy = jest.fn(); + // @ts-ignore + webauthnAbortService.controller?.abort = abortSpy; + + // Reset the service + webauthnAbortService.reset(); + + // Generate a new signal, which should NOT call `abort()` because the controller was cleared + webauthnAbortService.createNewAbortSignal(); + expect(abortSpy).toHaveBeenCalledTimes(0); +}); diff --git a/packages/browser/src/methods/startAuthentication.test.ts b/packages/browser/src/methods/startAuthentication.test.ts index 0ade8fa..89a0938 100644 --- a/packages/browser/src/methods/startAuthentication.test.ts +++ b/packages/browser/src/methods/startAuthentication.test.ts @@ -223,8 +223,7 @@ test('should support "cable" transport', async () => { test('should cancel an existing call when executed again', async () => { const abortSpy = jest.spyOn(AbortController.prototype, 'abort'); // Reset the abort service so we get an accurate call count - // @ts-ignore - webauthnAbortService.controller = undefined; + webauthnAbortService.reset(); // Fire off a request and immediately attempt a second one startAuthentication(goodOpts1); diff --git a/packages/browser/src/methods/startRegistration.test.ts b/packages/browser/src/methods/startRegistration.test.ts index 70eb975..93400c4 100644 --- a/packages/browser/src/methods/startRegistration.test.ts +++ b/packages/browser/src/methods/startRegistration.test.ts @@ -215,8 +215,7 @@ test('should return "cable" transport from response', async () => { test('should cancel an existing call when executed again', async () => { const abortSpy = jest.spyOn(AbortController.prototype, 'abort'); // Reset the abort service so we get an accurate call count - // @ts-ignore - webauthnAbortService.controller = undefined; + webauthnAbortService.reset(); // Fire off a request and immediately attempt a second one startRegistration(goodOpts1); -- cgit v1.2.3