21720:
[arvados.git] / services / workbench2 / src / common / use-async-interval.cy.js
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import React, { useState, useEffect } from 'react';
6 import { useAsyncInterval } from './use-async-interval';
7
8 // import { configure, mount } from 'enzyme';
9 // import Adapter from 'enzyme-adapter-react-16';
10 // import FakeTimers from "@sinonjs/fake-timers";
11
12 // configure({ adapter: new Adapter() });
13 // const clock = FakeTimers.install();
14
15 // jest.mock('react', () => {
16 //     const originalReact = jest.requireActual('react');
17 //     const mUseRef = jest.fn();
18 //     return {
19 //         ...originalReact,
20 //         useRef: mUseRef,
21 //     };
22 // });
23
24 // const TestComponent = (props) => {
25 //     useAsyncInterval(props.callback, 2000);
26 //     return <span />;
27 // };
28
29 const TestComponent = ({asyncCallback}) => {
30
31   useAsyncInterval(asyncCallback, 1000);
32
33   return <div>test</div>;
34 };
35
36 describe('useAsyncInterval', () => {
37   it('should fire repeatedly after the interval', () => {
38     cy.clock();
39     const asyncCallback = cy.spy().as('asyncCallback');
40     cy.mount(<TestComponent asyncCallback={asyncCallback} />);
41
42     cy.get('@asyncCallback').should('not.have.been.called');
43
44     cy.tick(1000);
45     cy.wait(0);
46     
47     cy.get('@asyncCallback').should('have.been.calledOnce');
48     
49     cy.tick(1000);
50     cy.wait(0);
51
52     cy.get('@asyncCallback').should('have.been.calledTwice');
53
54     cy.tick(1000);
55     cy.wait(0);
56
57     cy.get('@asyncCallback').should('have.been.calledThrice');
58     cy.clock().invoke('restore');
59   });
60
61     it('should wait for async callbacks to complete in between polling', async () => {
62         // const mockedReact = React as jest.Mocked<typeof React>;
63         // const ref = { current: {} };
64         // mockedReact.useRef.mockReturnValue(ref);
65
66         const delayedCallback = jest.fn(() => (
67             new Promise((resolve) => {
68                 setTimeout(() => {
69                     resolve();
70                 }, 2000);
71             })
72         ));
73         const testComponent = mount(<TestComponent
74             callback={delayedCallback}
75         />);
76
77         // cb queued with setInterval but not called
78         expect(delayedCallback).not.toHaveBeenCalled();
79
80         // Wait 2 seconds for first tick
81         await clock.tickAsync(2000);
82         // First cb called after 2 seconds
83         expect(delayedCallback).toHaveBeenCalledTimes(1);
84         // Wait for cb to resolve for 2 seconds
85         await clock.tickAsync(2000);
86         expect(delayedCallback).toHaveBeenCalledTimes(1);
87
88         // Wait 2 seconds for second tick
89         await clock.tickAsync(2000);
90         expect(delayedCallback).toHaveBeenCalledTimes(2);
91         // Wait for cb to resolve for 2 seconds
92         await clock.tickAsync(2000);
93         expect(delayedCallback).toHaveBeenCalledTimes(2);
94
95         // Wait 2 seconds for third tick
96         await clock.tickAsync(2000);
97         expect(delayedCallback).toHaveBeenCalledTimes(3);
98         // Wait for cb to resolve for 2 seconds
99         await clock.tickAsync(2000);
100         expect(delayedCallback).toHaveBeenCalledTimes(3);
101     });
102 });