Created
August 12, 2021 23:03
-
-
Save nathansmith/c40434673bfe9d345a4487f0dfd007d5 to your computer and use it in GitHub Desktop.
Example of how to test `requestAnimationFrame` with Jest.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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