From 86196b555ae6d611dc53d2870a20c918a258c053 Mon Sep 17 00:00:00 2001 From: Matthew Miller Date: Mon, 20 Jun 2022 11:44:44 -0700 Subject: Update tests for abort support --- packages/browser/src/methods/startAuthentication.test.ts | 13 +++++++++++++ packages/browser/src/methods/startRegistration.test.ts | 13 +++++++++++++ 2 files changed, 26 insertions(+) (limited to 'packages/browser/src') diff --git a/packages/browser/src/methods/startAuthentication.test.ts b/packages/browser/src/methods/startAuthentication.test.ts index f134dae..0ade8fa 100644 --- a/packages/browser/src/methods/startAuthentication.test.ts +++ b/packages/browser/src/methods/startAuthentication.test.ts @@ -10,6 +10,7 @@ import utf8StringToBuffer from '../helpers/utf8StringToBuffer'; import bufferToBase64URLString from '../helpers/bufferToBase64URLString'; import { WebAuthnError } from '../helpers/structs'; import { generateCustomError } from '../helpers/__jest__/generateCustomError'; +import { webauthnAbortService } from '../helpers/webAuthnAbortService'; import { startAuthentication } from './startAuthentication'; @@ -219,6 +220,18 @@ test('should support "cable" transport', async () => { .toEqual("cable"); }); +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; + + // Fire off a request and immediately attempt a second one + startAuthentication(goodOpts1); + await startAuthentication(goodOpts1); + expect(abortSpy).toHaveBeenCalledTimes(1); +}); + describe('WebAuthnError', () => { describe('AbortError', () => { const AbortError = generateCustomError('AbortError'); diff --git a/packages/browser/src/methods/startRegistration.test.ts b/packages/browser/src/methods/startRegistration.test.ts index 58d9acd..70eb975 100644 --- a/packages/browser/src/methods/startRegistration.test.ts +++ b/packages/browser/src/methods/startRegistration.test.ts @@ -8,6 +8,7 @@ import { generateCustomError } from '../helpers/__jest__/generateCustomError'; import { browserSupportsWebauthn } from '../helpers/browserSupportsWebauthn'; import bufferToBase64URLString from '../helpers/bufferToBase64URLString'; import { WebAuthnError } from '../helpers/structs'; +import { webauthnAbortService } from '../helpers/webAuthnAbortService'; import utf8StringToBuffer from '../helpers/utf8StringToBuffer'; @@ -211,6 +212,18 @@ test('should return "cable" transport from response', async () => { expect(response.transports).toEqual(["cable"]); }); +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; + + // Fire off a request and immediately attempt a second one + startRegistration(goodOpts1); + await startRegistration(goodOpts1); + expect(abortSpy).toHaveBeenCalledTimes(1); +}); + describe('WebAuthnError', () => { describe('AbortError', () => { const AbortError = generateCustomError('AbortError'); -- cgit v1.2.3