From f9a3857aceeb1b30b92dc1b886dd7ca37b32d7ee Mon Sep 17 00:00:00 2001 From: Niranjan Pradeep Date: Sun, 31 Mar 2024 02:58:24 +0530 Subject: [PATCH] Simplify jest.setup.js --- jest.setup.js | 5 ----- test/FixedDataTableRoot-test.js | 9 +++++++++ test/ReactTouchHandler-test.js | 7 ++++++- test/plugins/ReorderCell-test.js | 6 ------ 4 files changed, 15 insertions(+), 12 deletions(-) diff --git a/jest.setup.js b/jest.setup.js index d32d2a85..f1838845 100644 --- a/jest.setup.js +++ b/jest.setup.js @@ -1,9 +1,4 @@ module.exports = async function () { // NOTE (pradeep): Can this be synced with the definitions in webpack configs? globalThis.__DEV__ = true; - - // NOTE (pradeep): `requestAnimationFrame` and `setInterval` is already defined by jsdom. - // However, I'm redefining it to run the callbacks synchronously to make testing easier. - globalThis.requestAnimationFrame = (callback) => callback(); - globalThis.setInterval = (callback, _interval) => callback(); }; diff --git a/test/FixedDataTableRoot-test.js b/test/FixedDataTableRoot-test.js index 2cf7ff4d..22e81f27 100644 --- a/test/FixedDataTableRoot-test.js +++ b/test/FixedDataTableRoot-test.js @@ -13,8 +13,17 @@ import { import { Table, Column } from '../src/index'; import Scrollbar from '../src/plugins/Scrollbar'; +import * as requestAnimationFramePolyfill from '../src/vendor_upstream/core/requestAnimationFramePolyfill'; describe('FixedDataTableRoot', function () { + beforeEach(() => { + // NOTE (pradeep): Call the callback passed to requestAnimationFrame immediately. + // This simplifies the tests by making them behave synchronous. + jest + .spyOn(requestAnimationFramePolyfill, 'default') + .mockImplementation((callback) => callback()); + }); + afterEach(() => { // restore all spys created with spyOn jest.restoreAllMocks(); diff --git a/test/ReactTouchHandler-test.js b/test/ReactTouchHandler-test.js index b9c02046..623cb01e 100644 --- a/test/ReactTouchHandler-test.js +++ b/test/ReactTouchHandler-test.js @@ -9,7 +9,12 @@ describe('ReactTouchHandler', function () { let mockRequestAnimationFramePolyfill; beforeEach(function () { - // See https://stackoverflow.com/a/74269343 + // NOTE (pradeep): Call the callback passed to `setInterval` immediately. + // This simplifies the tests by making them behave synchronous. + jest + .spyOn(globalThis, 'setInterval') + .mockImplementation((callback) => callback()); + mockRequestAnimationFramePolyfill = jest .spyOn(requestAnimationFrame, 'default') .mockImplementation(() => undefined); diff --git a/test/plugins/ReorderCell-test.js b/test/plugins/ReorderCell-test.js index 2784a538..682d6cb2 100644 --- a/test/plugins/ReorderCell-test.js +++ b/test/plugins/ReorderCell-test.js @@ -123,12 +123,6 @@ class ReorderCellTest extends React.Component { } describe('ReorderCell', () => { - beforeEach(() => { - jest - .spyOn(requestAnimationFramePolyfill, 'default') - .mockImplementation((callback) => setTimeout(callback, 16)); - }); - afterEach(() => { jest.restoreAllMocks(); });