Skip to content

Commit

Permalink
Test: added a way to do time mock testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Zeh Fernando committed Dec 14, 2018
1 parent 710f00b commit c9fcbc2
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ dist
node_modules
npm-debug.log
temp
yarn-error.log
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"babel-core": "7.0.0-bridge.0",
"babel-jest": "^23.6.0",
"jest": "^23.6.0",
"mockdate": "^2.0.2",
"rimraf": "^2.6.2",
"rollup": "^0.67.4",
"rollup-plugin-node-resolve": "^4.0.0",
Expand Down
77 changes: 77 additions & 0 deletions test/unit/utils/timeMock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import MockDate from "mockdate";

const originalRequestAnimationFrame = global.requestAnimationFrame;
const originalCancelAnimationFrame = global.cancelAnimationFrame;

let enabled = false;

/**
* Disable requestAnimationFrame() and use timers
* Then we can use:
* - jest.useFakeTimers()
* - jest.runAllTimers()
* - jest.runOnlyPendingTimers()
* - jest.advanceTimersByTime()
* - jest.clearAllTimers()
* to simulate time.
*/
export function enableTimeMocks() {
// Ensure requestAnimationFrame() uses time functions
if (!enabled) {
useFixedTime();

let lastTime = 0;
enabled = true;
global.requestAnimationFrame = callback => {
const currTime = new Date().getTime();
const timeToCall = Math.max(0, 16 - (currTime - lastTime));
const id = setTimeout(() => {
callback(currTime + timeToCall);
}, timeToCall);
lastTime = currTime + timeToCall;
return id;
};

global.cancelAnimationFrame = function(id) {
clearTimeout(id);
};

// Enable fake timers on jest
jest.useFakeTimers();

timeTravel(0);
}
}

export function timeTravel(durationMs, step = 100) {
if (enabled) {
const tickTravel = tickDurationMs => {
jest.advanceTimersByTime(tickDurationMs);
const now = Date.now();
MockDate.set(new Date(now + tickDurationMs));
};

let done = 0;
while (durationMs - done > step) {
tickTravel(step);
done += step;
}
tickTravel(durationMs - done);
}
}

export function disableTimeMocks() {
if (enabled) {
enabled = false;

jest.clearAllTimers();
jest.useRealTimers();

global.requestAnimationFrame = originalRequestAnimationFrame;
global.cancelAnimationFrame = originalCancelAnimationFrame;
}
}

export function useFixedTime() {
MockDate.set(1540326022034);
}
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2962,6 +2962,11 @@ mkdirp@^0.5.0, mkdirp@^0.5.1:
dependencies:
minimist "0.0.8"

mockdate@^2.0.2:
version "2.0.2"
resolved "https://registry.yarnpkg.com/mockdate/-/mockdate-2.0.2.tgz#5ae0c0eaf8fe23e009cd01f9889b42c4f634af12"
integrity sha1-WuDA6vj+I+AJzQH5iJtCxPY0rxI=

moremath@^2.0.2:
version "2.0.2"
resolved "https://registry.yarnpkg.com/moremath/-/moremath-2.0.2.tgz#d3574121e2084d4645ab6c9ba7d253e72b11fece"
Expand Down

0 comments on commit c9fcbc2

Please sign in to comment.