1*8d307f52SEvan Baconimport { delayAsync, resolveWithTimeout, waitForActionAsync } from '../delay';
2*8d307f52SEvan Bacon
3*8d307f52SEvan BaconafterEach(() => {
4*8d307f52SEvan Bacon  jest.useRealTimers();
5*8d307f52SEvan Bacon});
6*8d307f52SEvan Bacon
7*8d307f52SEvan Bacondescribe(delayAsync, () => {
8*8d307f52SEvan Bacon  it(`await for a given duration of milliseconds`, async () => {
9*8d307f52SEvan Bacon    jest.useFakeTimers();
10*8d307f52SEvan Bacon    const promise = delayAsync(100);
11*8d307f52SEvan Bacon    jest.advanceTimersByTime(100);
12*8d307f52SEvan Bacon    await promise;
13*8d307f52SEvan Bacon  });
14*8d307f52SEvan Bacon});
15*8d307f52SEvan Bacon
16*8d307f52SEvan Bacondescribe(waitForActionAsync, () => {
17*8d307f52SEvan Bacon  it(`wait for a given action to return a truthy value`, async () => {
18*8d307f52SEvan Bacon    const fn = jest.fn(() => 'd');
19*8d307f52SEvan Bacon    const result = await waitForActionAsync({
20*8d307f52SEvan Bacon      action: fn,
21*8d307f52SEvan Bacon      interval: 100,
22*8d307f52SEvan Bacon      maxWaitTime: 1000,
23*8d307f52SEvan Bacon    });
24*8d307f52SEvan Bacon    expect(result).toEqual('d');
25*8d307f52SEvan Bacon    expect(fn).toHaveBeenCalledTimes(1);
26*8d307f52SEvan Bacon  });
27*8d307f52SEvan Bacon  it(`times out waiting for a given action to return a truthy value`, async () => {
28*8d307f52SEvan Bacon    const fn = jest.fn(() => '');
29*8d307f52SEvan Bacon    const result = await waitForActionAsync({
30*8d307f52SEvan Bacon      action: fn,
31*8d307f52SEvan Bacon      interval: 80,
32*8d307f52SEvan Bacon      maxWaitTime: 100,
33*8d307f52SEvan Bacon    });
34*8d307f52SEvan Bacon    expect(result).toEqual('');
35*8d307f52SEvan Bacon    expect(fn).toHaveBeenCalledTimes(2);
36*8d307f52SEvan Bacon  }, 500);
37*8d307f52SEvan Bacon});
38*8d307f52SEvan Bacon
39*8d307f52SEvan Bacondescribe(resolveWithTimeout, () => {
40*8d307f52SEvan Bacon  it(`times out`, async () => {
41*8d307f52SEvan Bacon    jest.useFakeTimers();
42*8d307f52SEvan Bacon    // Create a function that never resolves.
43*8d307f52SEvan Bacon    const fn = jest.fn(() => new Promise(() => {}));
44*8d307f52SEvan Bacon
45*8d307f52SEvan Bacon    const promise = resolveWithTimeout(fn, { timeout: 50, errorMessage: 'Timeout' });
46*8d307f52SEvan Bacon    jest.advanceTimersByTime(50);
47*8d307f52SEvan Bacon    await expect(promise).rejects.toThrowError('Timeout');
48*8d307f52SEvan Bacon
49*8d307f52SEvan Bacon    // Ensure the function was called.
50*8d307f52SEvan Bacon    expect(fn).toBeCalled();
51*8d307f52SEvan Bacon  });
52*8d307f52SEvan Bacon  it(`resolves in time`, async () => {
53*8d307f52SEvan Bacon    jest.useFakeTimers();
54*8d307f52SEvan Bacon    // Create a function that never resolves.
55*8d307f52SEvan Bacon    const fn = jest.fn(async () => 'foobar');
56*8d307f52SEvan Bacon
57*8d307f52SEvan Bacon    const promise = resolveWithTimeout(fn, { timeout: 50 });
58*8d307f52SEvan Bacon    jest.advanceTimersByTime(49);
59*8d307f52SEvan Bacon    await expect(promise).resolves.toEqual('foobar');
60*8d307f52SEvan Bacon    // Ensure the function was called.
61*8d307f52SEvan Bacon    expect(fn).toBeCalled();
62*8d307f52SEvan Bacon  });
63*8d307f52SEvan Bacon});
64