GithubHelp home page GithubHelp logo

pmndrs / mock-raf Goto Github PK

View Code? Open in Web Editor NEW

This project forked from formidablelabs/mock-raf

9.0 2.0 2.0 20 KB

A simple mock for requestAnimationFrame testing with fake timers

License: MIT License

JavaScript 100.00%

mock-raf's Introduction

@react-spring/mock-raf

A simple mock for requestAnimationFrame testing with fake timers.

Adapted with gratitude from react-motion.

Example

To use with jest/jasmine and react-spring:

import createMockRaf from '@react-spring/mock-raf'
import { Globals, FrameLoop, SpringValue } from 'react-spring'

let mockRaf
beforeEach(() => {
  mockRaf = createMockRaf()
  Globals.assign({
    now: mockRaf.now,
    performanceNow: mockRaf.now,
    requestAnimationFrame: mockRaf.raf,
    cancelAnimationFrame: mockRaf.cancel,
    frameLoop: new FrameLoop(),
  })
})

it('animates from 0 to 1', () => {
  const value = new SpringValue(0)
  value.start(1)

  expect(value.get()).toBe(0)
  mockRaf.flush()
  expect(value.get()).toBe(1)
})

API

createMockRaf()

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

Returns:

  • now()
  • raf()
  • cancel()
  • step()
const 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.

 

flush()

Continuously iterate the requestAnimationFrame queue until empty.

Useful for jumping to the end of an animation (or group of animations).

 

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.

mock-raf's People

Contributors

aleclarson avatar alexlande avatar appsforartists avatar boygirl avatar eirslett avatar jcready avatar selbekk avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

libin1991

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.