UNPKG

@shopify/jest-dom-mocks

Version:
54 lines (52 loc) 1.88 kB
class AnimationFrame { constructor() { this.isUsingMockAnimationFrame = false; this.queued = {}; this.currentAnimationFrame = 0; this.requestAnimationFrame = callback => { this.currentAnimationFrame += 1; this.queued[this.currentAnimationFrame] = callback; return this.currentAnimationFrame; }; this.cancelAnimationFrame = frame => { delete this.queued[frame]; }; } mock() { if (this.isUsingMockAnimationFrame) { throw new Error('The animation frame is already mocked, but you tried to mock it again.'); } this.isUsingMockAnimationFrame = true; this.originalRequestAnimationFrame = window.requestAnimationFrame; window.requestAnimationFrame = this.requestAnimationFrame; this.originalCancelAnimationFrame = window.cancelAnimationFrame; window.cancelAnimationFrame = this.cancelAnimationFrame; } restore() { if (!this.isUsingMockAnimationFrame) { throw new Error('The animation frame is already real, but you tried to restore it again.'); } this.isUsingMockAnimationFrame = false; window.requestAnimationFrame = this.originalRequestAnimationFrame; window.cancelAnimationFrame = this.originalCancelAnimationFrame; } isMocked() { return this.isUsingMockAnimationFrame; } runFrame() { this.ensureAnimationFrameIsMock(); // We need to do it this way so that frames that queue other frames // don't get removed Object.keys(this.queued).forEach(frame => { const callback = this.queued[frame]; delete this.queued[frame]; callback(Date.now()); }); } ensureAnimationFrameIsMock() { if (!this.isUsingMockAnimationFrame) { throw new Error('You must call animationFrame.mock() before interacting with the mock request- or cancel- AnimationFrame methods.'); } } } export { AnimationFrame as default };