aboutsummaryrefslogtreecommitdiffstats
path: root/node_modules/exponential-backoff/src/delay/always/always.delay.spec.ts
blob: 8a08cd804233a7b86a02f317829f0b349b389d31 (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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { AlwaysDelay } from "./always.delay";
import { IBackOffOptions, getSanitizedOptions } from "../../options";

describe(AlwaysDelay.name, () => {
  let options: IBackOffOptions;
  let delay: AlwaysDelay;

  function initClass() {
    delay = new AlwaysDelay(options);
  }

  beforeEach(() => {
    options = getSanitizedOptions({});
    initClass();
    jest.useFakeTimers();
  });

  it(`when calling #apply, the delay is equal to the starting delay`, async () => {
    const spy = jest.fn();
    delay.apply().then(spy);
    jest.runTimersToTime(options.startingDelay);
    await Promise.resolve();

    expect(spy).toHaveBeenCalledTimes(1);
  });

  it(`when the attempt number is 1, when calling #apply,
  the delay is equal to the starting delay multiplied by the time multiple`, async () => {
    delay.setAttemptNumber(1);

    const spy = jest.fn();
    delay.apply().then(spy);
    jest.runTimersToTime(options.startingDelay * options.timeMultiple);
    await Promise.resolve();

    expect(spy).toHaveBeenCalledTimes(1);
  });

  it(`when the attempt number is 2, when calling #apply,
  the delay is equal to the starting delay multiplied by the time multiple raised by the attempt number`, async () => {
    const attemptNumber = 2;
    delay.setAttemptNumber(attemptNumber);

    const spy = jest.fn();
    delay.apply().then(spy);
    jest.runTimersToTime(
      options.startingDelay * Math.pow(options.timeMultiple, attemptNumber)
    );
    await Promise.resolve();

    expect(spy).toHaveBeenCalledTimes(1);
  });

  it(`when the #maxDelay is less than #startingDelay, when calling #apply,
  the delay is equal to the #maxDelay`, async () => {
    options.maxDelay = options.startingDelay - 1;

    const spy = jest.fn();
    delay.apply().then(spy);
    jest.runTimersToTime(options.maxDelay);
    await Promise.resolve();

    expect(spy).toHaveBeenCalledTimes(1);
  });
});