FormidableLabs/mock-raf

Name: mock-raf

Owner: Formidable

Description: A simple mock for requestAnimationFrame testing with fake timers

Created: 2015-08-16 21:12:08.0

Updated: 2017-12-16 14:29:54.0

Pushed: 2017-01-18 20:59:01.0

Homepage: null

Size: 6

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

mock-raf

A simple mock for requestAnimationFrame testing with fake timers.

Adapted with gratitude from react-motion. Original source here.

Basic Usage
createMockRaf = require('mock-raf');
mockRaf = createMockRaf();

tub out your `requestAnimationFrame` method
n.stub(window, 'requestAnimationFrame').callsFake(mockRaf.raf);

ake 10 `requestAnimationFrame` steps (your callback will fire 10 times)
Raf.step({ count: 10 });
API
createMockRaf()

Creates a mockRaf instance, exposing the functions you'll use to interact with the mock.

Returns:

mockRaf = createMockRaf();
now()

Returns the current now value of the mock. Starts at 0 and increases with each step() taken. Useful for stubbing out performance.now() or a polyfill when using requestAnimationFrame with timers.

raf()

Replacement for requestAnimationFrame or a polyfill. Adds a callback to be fired on the next step.

cancel()

Replacement for cancelAnimationFrame or a polyfill. Removes all currently scheduled requestAnimationFrame callbacks from the queue.

step(options)

Takes requestAnimationFrame steps. Fires currently queued callbacks for each step and increments now time for each step. The primary way to interact with a mockRaf instance for testing.

Options

step() takes an optional options object:

time

Type: Number Default: 1000 / 60

The time that should pass during each requestAnimationFrame step in milliseconds. Default is roughly equivalent to default browser behavior.

count

Type: Number Default: 1

The number of steps to take.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.