Skip to content

Commit

Permalink
adding a test for requestAnimationFrame behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
alexreardon committed Oct 4, 2023
1 parent e7e5543 commit d1ae881
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/jest-environment/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export interface Jest {
*/
advanceTimersByTimeAsync(msToRun: number): Promise<void>;
/**
* Advances all timers by the needed milliseconds to execute currently scheduled animation frames.
* Advances all timers by the needed milliseconds to execute callbacks current scheduled with `requestAnimationFrame`.
* `advanceTimersToNextFrame()` helpful way to execute code that is scheduled using `requestAnimationFrame`.
*
* @remarks
Expand Down
23 changes: 23 additions & 0 deletions packages/jest-fake-timers/src/__tests__/modernFakeTimers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,29 @@ describe('FakeTimers', () => {
timers.advanceTimersByTime(1);
expect(runOrder).toEqual(['frame', 'timeout']);
});

it('should call animation frame callbacks with the latest system time', () => {
const global = {
Date,
clearTimeout,
performance,
process,
requestAnimationFrame: () => -1,
setTimeout,
} as unknown as typeof globalThis;

const timers = new FakeTimers({config: makeProjectConfig(), global});
timers.useFakeTimers();

const callback = jest.fn();

global.requestAnimationFrame(callback);

timers.advanceTimersToNextFrame();

// `requestAnimationFrame` callbacks are called with a `DOMHighResTimeStamp`
expect(callback).toHaveBeenCalledWith(global.performance.now());
});
});

describe('reset', () => {
Expand Down

0 comments on commit d1ae881

Please # to comment.