1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import React, { useState, useEffect } from 'react';
6 import { useAsyncInterval } from './use-async-interval';
8 // import { configure, mount } from 'enzyme';
9 // import Adapter from 'enzyme-adapter-react-16';
10 // import FakeTimers from "@sinonjs/fake-timers";
12 // configure({ adapter: new Adapter() });
13 // const clock = FakeTimers.install();
15 // jest.mock('react', () => {
16 // const originalReact = jest.requireActual('react');
17 // const mUseRef = jest.fn();
24 // const TestComponent = (props) => {
25 // useAsyncInterval(props.callback, 2000);
29 const TestComponent = ({asyncCallback}) => {
31 useAsyncInterval(asyncCallback, 1000);
33 return <div>test</div>;
36 describe('useAsyncInterval', () => {
37 it('should fire repeatedly after the interval', () => {
39 const asyncCallback = cy.spy().as('asyncCallback');
40 cy.mount(<TestComponent asyncCallback={asyncCallback} />);
42 cy.get('@asyncCallback').should('not.have.been.called');
47 cy.get('@asyncCallback').should('have.been.calledOnce');
52 cy.get('@asyncCallback').should('have.been.calledTwice');
57 cy.get('@asyncCallback').should('have.been.calledThrice');
58 cy.clock().invoke('restore');
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);
66 const delayedCallback = jest.fn(() => (
67 new Promise((resolve) => {
73 const testComponent = mount(<TestComponent
74 callback={delayedCallback}
77 // cb queued with setInterval but not called
78 expect(delayedCallback).not.toHaveBeenCalled();
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);
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);
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);