-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test: added a way to do time mock testing
- Loading branch information
Zeh Fernando
committed
Dec 14, 2018
1 parent
710f00b
commit c9fcbc2
Showing
4 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,4 @@ dist | |
node_modules | ||
npm-debug.log | ||
temp | ||
yarn-error.log |
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
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
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); | ||
} |
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