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 const TestComponent = ({callback}) => {
9 useAsyncInterval(callback, 1000);
10 return <div>test</div>;
13 describe('useAsyncInterval', () => {
14 it('should fire repeatedly after the interval', () => {
16 const syncCallback = cy.spy().as('syncCallback');
17 cy.mount(<TestComponent callback={syncCallback} />);
19 cy.get('@syncCallback').should('not.have.been.called');
24 cy.get('@syncCallback').should('have.been.calledOnce');
29 cy.get('@syncCallback').should('have.been.calledTwice');
34 cy.get('@syncCallback').should('have.been.calledThrice');
35 cy.clock().invoke('restore');
38 it('should wait for async callbacks to complete in between polling', async () => {
41 const delayedCallback = cy.stub().callsFake(() => {
42 return new Promise((resolve) => {
47 }).as('delayedCallback');
49 cy.mount(<TestComponent
50 callback={delayedCallback}
53 // cb queued with setInterval but not called
54 cy.get('@delayedCallback').should('not.have.been.called');
56 // Wait 2 seconds for first tick
60 // First cb called after 2 seconds
61 cy.get('@delayedCallback').should('have.been.calledOnce');
63 // Wait for cb to resolve for 2 seconds
66 cy.get('@delayedCallback').should('have.been.calledOnce');
68 // Wait 2 seconds for second tick
71 cy.get('@delayedCallback').should('have.been.calledTwice');
73 // Wait for cb to resolve for 2 seconds
76 cy.get('@delayedCallback').should('have.been.calledTwice');
78 // Wait 2 seconds for third tick
81 cy.get('@delayedCallback').should('have.been.calledThrice');
83 // Wait for cb to resolve for 2 seconds
86 cy.get('@delayedCallback').should('have.been.calledThrice');