21720: fixed file-viewer-action test
[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 const TestComponent = ({callback}) => {
9   useAsyncInterval(callback, 1000);
10   return <div>test</div>;
11 };
12
13 describe('useAsyncInterval', () => {
14   it('should fire repeatedly after the interval', () => {
15     cy.clock();
16     const syncCallback = cy.spy().as('syncCallback');
17     cy.mount(<TestComponent callback={syncCallback} />);
18
19     cy.get('@syncCallback').should('not.have.been.called');
20
21     cy.tick(1000);
22     cy.wait(0);
23     
24     cy.get('@syncCallback').should('have.been.calledOnce');
25     
26     cy.tick(1000);
27     cy.wait(0);
28
29     cy.get('@syncCallback').should('have.been.calledTwice');
30
31     cy.tick(1000);
32     cy.wait(0);
33
34     cy.get('@syncCallback').should('have.been.calledThrice');
35     cy.clock().invoke('restore');
36   });
37
38     it('should wait for async callbacks to complete in between polling', async () => {
39         cy.clock();
40
41         const delayedCallback = cy.stub().callsFake(() => {
42             return new Promise((resolve) => {
43               setTimeout(() => {
44                 resolve('done');
45               }, 2000);
46             });
47           }).as('delayedCallback');
48
49         cy.mount(<TestComponent
50             callback={delayedCallback}
51         />);
52
53         // cb queued with setInterval but not called
54         cy.get('@delayedCallback').should('not.have.been.called');
55
56         // Wait 2 seconds for first tick
57         cy.tick(2000);
58         cy.wait(0);
59
60         // First cb called after 2 seconds
61         cy.get('@delayedCallback').should('have.been.calledOnce');
62
63         // Wait for cb to resolve for 2 seconds
64         cy.tick(2000);
65         cy.wait(0);
66         cy.get('@delayedCallback').should('have.been.calledOnce');
67
68         // Wait 2 seconds for second tick
69         cy.tick(2000);
70         cy.wait(0);
71         cy.get('@delayedCallback').should('have.been.calledTwice');
72
73         // Wait for cb to resolve for 2 seconds
74         cy.tick(2000);
75         cy.wait(0);
76         cy.get('@delayedCallback').should('have.been.calledTwice');
77
78         // Wait 2 seconds for third tick
79         cy.tick(2000);
80         cy.wait(0);
81         cy.get('@delayedCallback').should('have.been.calledThrice');
82
83         // Wait for cb to resolve for 2 seconds
84         cy.tick(2000);
85         cy.wait(0);
86         cy.get('@delayedCallback').should('have.been.calledThrice');
87     });
88 });