summaryrefslogtreecommitdiffhomepage
path: root/packages/browser/src/helpers/webAuthnAbortService.test.ts
blob: f4e63448eb0345124a34e0dd280797574ce99905 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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);
});