Skip to content

Commit

Permalink
Simplify jest.setup.js
Browse files Browse the repository at this point in the history
  • Loading branch information
pradeepnschrodinger committed Mar 30, 2024
1 parent 37c111b commit f9a3857
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 12 deletions.
5 changes: 0 additions & 5 deletions jest.setup.js
Original file line number Diff line number Diff line change
@@ -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();
};
9 changes: 9 additions & 0 deletions test/FixedDataTableRoot-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
7 changes: 6 additions & 1 deletion test/ReactTouchHandler-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 0 additions & 6 deletions test/plugins/ReorderCell-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,6 @@ class ReorderCellTest extends React.Component {
}

describe('ReorderCell', () => {
beforeEach(() => {
jest
.spyOn(requestAnimationFramePolyfill, 'default')
.mockImplementation((callback) => setTimeout(callback, 16));
});

afterEach(() => {
jest.restoreAllMocks();
});
Expand Down

0 comments on commit f9a3857

Please sign in to comment.