From 0e4785482e88ed6ce9c2d2658d4e244786f76626 Mon Sep 17 00:00:00 2001 From: Lisa Knox Date: Tue, 16 Jul 2024 09:15:05 -0400 Subject: [PATCH] 21720: * updated use-async-interval test * increased maxEventListeners to silence warnings when running a single test repeatedly * added testByPath script that runs a single spec Arvados-DCO-1.1-Signed-off-by: Lisa Knox --- services/workbench2/cypress.config.ts | 8 +- services/workbench2/package.json | 1 + .../src/common/use-async-interval.cy.js | 98 ++++++++----------- 3 files changed, 50 insertions(+), 57 deletions(-) diff --git a/services/workbench2/cypress.config.ts b/services/workbench2/cypress.config.ts index 9bc6a18801..363c94b95f 100644 --- a/services/workbench2/cypress.config.ts +++ b/services/workbench2/cypress.config.ts @@ -5,6 +5,12 @@ import { defineConfig } from "cypress"; import baseWebpackConfig from "./config/webpack.config"; import path from "path"; +import EventEmitter from "events"; + +// Increase the default max listeners to avoid warnings +// this doesn't matter when running the entire suite, +// but necessary when running a single test repeatedly +EventEmitter.defaultMaxListeners = 100; const webpackConfig = baseWebpackConfig("development"); @@ -48,4 +54,4 @@ export default defineConfig({ }, }, }, -}); \ No newline at end of file +}); diff --git a/services/workbench2/package.json b/services/workbench2/package.json index 0dc3527e9f..6fce1968f1 100644 --- a/services/workbench2/package.json +++ b/services/workbench2/package.json @@ -137,6 +137,7 @@ "test": "CI=true node scripts/test.js", "test-local": "node scripts/test.js", "test-cypress": " yarn cypress run --component", + "testByPath": "yarn cypress run --component --spec", "lint": "tslint src/** -t verbose", "build-css": "node-sass src/ -o src/", "watch-css": "npm run build-css && node-sass src/ -o src/ --watch --recursive" diff --git a/services/workbench2/src/common/use-async-interval.cy.js b/services/workbench2/src/common/use-async-interval.cy.js index 34e751ce08..e032b61e08 100644 --- a/services/workbench2/src/common/use-async-interval.cy.js +++ b/services/workbench2/src/common/use-async-interval.cy.js @@ -5,98 +5,84 @@ import React, { useState, useEffect } from 'react'; import { useAsyncInterval } from './use-async-interval'; -// import { configure, mount } from 'enzyme'; -// import Adapter from 'enzyme-adapter-react-16'; -// import FakeTimers from "@sinonjs/fake-timers"; - -// configure({ adapter: new Adapter() }); -// const clock = FakeTimers.install(); - -// jest.mock('react', () => { -// const originalReact = jest.requireActual('react'); -// const mUseRef = jest.fn(); -// return { -// ...originalReact, -// useRef: mUseRef, -// }; -// }); - -// const TestComponent = (props) => { -// useAsyncInterval(props.callback, 2000); -// return ; -// }; - -const TestComponent = ({asyncCallback}) => { - - useAsyncInterval(asyncCallback, 1000); - +const TestComponent = ({callback}) => { + useAsyncInterval(callback, 1000); return
test
; }; describe('useAsyncInterval', () => { it('should fire repeatedly after the interval', () => { cy.clock(); - const asyncCallback = cy.spy().as('asyncCallback'); - cy.mount(); + const syncCallback = cy.spy().as('syncCallback'); + cy.mount(); - cy.get('@asyncCallback').should('not.have.been.called'); + cy.get('@syncCallback').should('not.have.been.called'); cy.tick(1000); cy.wait(0); - cy.get('@asyncCallback').should('have.been.calledOnce'); + cy.get('@syncCallback').should('have.been.calledOnce'); cy.tick(1000); cy.wait(0); - cy.get('@asyncCallback').should('have.been.calledTwice'); + cy.get('@syncCallback').should('have.been.calledTwice'); cy.tick(1000); cy.wait(0); - cy.get('@asyncCallback').should('have.been.calledThrice'); + cy.get('@syncCallback').should('have.been.calledThrice'); cy.clock().invoke('restore'); }); it('should wait for async callbacks to complete in between polling', async () => { - // const mockedReact = React as jest.Mocked; - // const ref = { current: {} }; - // mockedReact.useRef.mockReturnValue(ref); - - const delayedCallback = jest.fn(() => ( - new Promise((resolve) => { - setTimeout(() => { - resolve(); - }, 2000); - }) - )); - const testComponent = mount( { + return new Promise((resolve) => { + setTimeout(() => { + resolve('done'); + }, 2000); + }); + }).as('delayedCallback'); + + cy.mount(); // cb queued with setInterval but not called - expect(delayedCallback).not.toHaveBeenCalled(); + cy.get('@delayedCallback').should('not.have.been.called'); // Wait 2 seconds for first tick - await clock.tickAsync(2000); + cy.tick(2000); + cy.wait(0); + // First cb called after 2 seconds - expect(delayedCallback).toHaveBeenCalledTimes(1); + cy.get('@delayedCallback').should('have.been.calledOnce'); + // Wait for cb to resolve for 2 seconds - await clock.tickAsync(2000); - expect(delayedCallback).toHaveBeenCalledTimes(1); + cy.tick(2000); + cy.wait(0); + cy.get('@delayedCallback').should('have.been.calledOnce'); // Wait 2 seconds for second tick - await clock.tickAsync(2000); - expect(delayedCallback).toHaveBeenCalledTimes(2); + cy.tick(2000); + cy.wait(0); + cy.get('@delayedCallback').should('have.been.calledTwice'); + // Wait for cb to resolve for 2 seconds - await clock.tickAsync(2000); - expect(delayedCallback).toHaveBeenCalledTimes(2); + cy.tick(2000); + cy.wait(0); + cy.get('@delayedCallback').should('have.been.calledTwice'); // Wait 2 seconds for third tick - await clock.tickAsync(2000); - expect(delayedCallback).toHaveBeenCalledTimes(3); + cy.tick(2000); + cy.wait(0); + cy.get('@delayedCallback').should('have.been.calledThrice'); + // Wait for cb to resolve for 2 seconds - await clock.tickAsync(2000); - expect(delayedCallback).toHaveBeenCalledTimes(3); + cy.tick(2000); + cy.wait(0); + cy.get('@delayedCallback').should('have.been.calledThrice'); }); }); -- 2.30.2