├── .gitignore ├── LICENSE.txt ├── README.md ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015-2016 Formidable Labs 4 | 5 | Copyright (c) 2015 Benjamin San Souci 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in all 15 | copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | SOFTWARE. 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Maintenance Status][maintenance-image]](#maintenance-status) 2 | 3 | # mock-raf 4 | 5 | A simple mock for `requestAnimationFrame` testing with fake timers. 6 | 7 | Adapted with gratitude from [`react-motion`](https://github.com/chenglou/react-motion/blob/dafff3f2b00ac11f39d91f3363cc97de664b2406/test/createMockRaf.js). Original source [here](https://github.com/chenglou/react-motion/blob/dafff3f2b00ac11f39d91f3363cc97de664b2406/test/createMockRaf.js). 8 | 9 | ## Basic Usage 10 | 11 | ```js 12 | var createMockRaf = require('mock-raf'); 13 | var mockRaf = createMockRaf(); 14 | 15 | // Stub out your `requestAnimationFrame` method 16 | sinon.stub(window, 'requestAnimationFrame').callsFake(mockRaf.raf); 17 | 18 | // Take 10 `requestAnimationFrame` steps (your callback will fire 10 times) 19 | mockRaf.step({ count: 10 }); 20 | ``` 21 | 22 | ## API 23 | 24 | ### `createMockRaf()` 25 | 26 | Creates a `mockRaf` instance, exposing the functions you'll use to interact with the mock. 27 | 28 | Returns: 29 | 30 | - now() 31 | - raf() 32 | - cancel() 33 | - step() 34 | 35 | ```js 36 | var mockRaf = createMockRaf(); 37 | ``` 38 | 39 | ### `now()` 40 | 41 | 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. 42 | 43 | ### `raf()` 44 | 45 | Replacement for `requestAnimationFrame` or a polyfill. Adds a callback to be fired on the next step. 46 | 47 | ### `cancel()` 48 | 49 | Replacement for `cancelAnimationFrame` or a polyfill. Removes all currently scheduled `requestAnimationFrame` callbacks from the queue. 50 | 51 | ### `step(options)` 52 | 53 | 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. 54 | 55 | #### Options 56 | 57 | `step()` takes an optional `options` object: 58 | 59 | ##### `time` 60 | 61 | Type: `Number` Default: `1000 / 60` 62 | 63 | The time that should pass during each `requestAnimationFrame` step in milliseconds. Default is roughly equivalent to default browser behavior. 64 | 65 | ##### `count` 66 | 67 | Type: `Number` Default: `1` 68 | 69 | The number of steps to take. 70 | 71 | ### Maintenance Status 72 | 73 | **Archived:** This project is no longer maintained by Formidable. We are no longer responding to issues or pull requests unless they relate to security concerns. We encourage interested developers to fork this project and make it their own! 74 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var assign = require('object-assign'); 2 | 3 | module.exports = function () { 4 | var allCallbacks = {}; 5 | var callbacksLength = 0; 6 | 7 | var now = 0; 8 | 9 | var getNow = function () { 10 | return now; 11 | }; 12 | 13 | var raf = function (callback) { 14 | callbacksLength += 1; 15 | 16 | allCallbacks[callbacksLength] = callback; 17 | 18 | return callbacksLength; 19 | }; 20 | 21 | var cancel = function (id) { 22 | delete allCallbacks[id]; 23 | }; 24 | 25 | var step = function (opts) { 26 | var options = assign({}, { 27 | time: 1000 / 60, 28 | count: 1 29 | }, opts); 30 | 31 | var oldAllCallbacks; 32 | 33 | for (var i = 0; i < options.count; i++) { 34 | oldAllCallbacks = allCallbacks; 35 | allCallbacks = {}; 36 | 37 | now += options.time; 38 | 39 | Object.keys(oldAllCallbacks).forEach(function (id) { 40 | var callback = oldAllCallbacks[id]; 41 | callback(now); 42 | }); 43 | } 44 | } 45 | 46 | return { 47 | now: getNow, 48 | raf: raf, 49 | cancel: cancel, 50 | step: step 51 | }; 52 | }; 53 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "mock-raf", 3 | "version": "1.0.1", 4 | "description": "A simple mock for requestAnimationFrame testing with fake timers.", 5 | "main": "index.js", 6 | "license": "MIT", 7 | "dependencies": { 8 | "object-assign": "^3.0.0" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "https://github.com/FormidableLabs/mock-raf.git" 13 | } 14 | } 15 | --------------------------------------------------------------------------------