Skip to content

Instantly share code, notes, and snippets.

@nathansmith
Created August 12, 2021 23:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nathansmith/c40434673bfe9d345a4487f0dfd007d5 to your computer and use it in GitHub Desktop.
Save nathansmith/c40434673bfe9d345a4487f0dfd007d5 to your computer and use it in GitHub Desktop.
Example of how to test `requestAnimationFrame` with Jest.
import React from 'react';
import { render } from 'react-dom';
// Test subject.
import MyComponent from './MyComponent';
// ====================
// Describe `fileName`.
// ====================
describe('EXAMPLE DESCRIBE', () => {
// ======
// Setup.
// ======
beforeEach(() => {
// Fake timers.
jest.useFakeTimers();
// Override.
window.requestAnimationFrame = (f) => setTimeout(() => f(), 0);
});
// =====
// Test.
// =====
test('EXAMPLE TEST', () => {
// ============
// Dummy props.
// ============
const props = {
foo: true,
};
// =======
// Render.
// =======
const component = <MyComponent {...props} />;
const container = document.createElement('div');
render(component, container);
// =============
// Fast forward.
// =============
jest.runAllTimers();
/*
NOTE:
By the time you get to here, anything dependent
on `requestAnimationFrame` should now be ready.
*/
expect(true).toBe(true);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment