react / react-0.13.3 / examples / basic-commonjs / node_modules / reactify / node_modules / react-tools / src / test / mock-timers.js
81152 viewsvar mocks = require('mocks');12var timers = {};3// Keep a fake timestamp4// move on the time when runTimersToTime() is called5var now = 0;67// add a timer of type either 'timeout' or 'interval'8function _setTimer(type, callback, delay) {9var token = null;10do {11token = Math.floor(Math.random() * 4294967296) + 1;12} while (timers[token]);13timers[token] = {14type: type,15callback: callback,16// Add some random msecs to the delay to mimic code execution time.17time: now + delay + Math.floor(Math.random() * 5),18interval: (type == 'interval' ? delay : 0)19};20return token;21}2223// clear a timer of type either 'timeout' or 'interval'24function _clearTimer(type, token) {25if (timers[token] && timers[token].type == type) {26delete timers[token];27}28}2930function _setTimeout(callback, delay) {31return _setTimer('timeout', callback, delay);32}3334function _setInterval(callback, delay) {35return _setTimer('interval', callback, delay);36}3738function _clearTimeout(token) {39_clearTimer('timeout', token);40}4142function _clearInterval(token) {43_clearTimer('interval', token);44}4546// Run timer of given token47function _runTimer(token) {48// Skip non-existing token49if (timers[token]) {50if (timers[token].type == 'timeout') {51// for 'timeout', run callback and delete the timer52var callback = timers[token].callback;53delete timers[token];54callback();55} else if (timers[token].type == 'interval') {56// for 'interval', run callback and set the next invoke time57// Add some random msecs to the delay to mimic code execution time.58timers[token].time =59now + timers[token].interval + Math.floor(Math.random() * 5);60timers[token].callback();61}62}63}6465function _runTimersOnce() {66// Invoke all timers once regardsless of the delay67for (var token in timers) {68_runTimer(token);69}70}7172function _runTimersToTime(delay) {73var toRunToken = _getNextTimerToken();74if (!toRunToken) {75return;76}7778var minTime = timers[toRunToken].time;79if (now + delay < minTime) {80// Termination when there's no more timers to invoke81now += delay;82} else {83// Recursively invoke the next to-run timer84delay -= (minTime - now);85now = minTime;86_runTimer(toRunToken);87_runTimersToTime(delay);88}89}9091function _runTimersRepeatedly() {92// Only run a generous 1000 timers and then bail, since we may have entered93// a loop if we have more than that.94var max_timers = 1000;9596var token;97for (var ii = 0; ii < max_timers; ii++) {98token = _getNextTimerToken();99100if (!token) {101break;102}103104_runTimer(token);105}106107if (ii === max_timers) {108throw new Error("More timers still exist after " + max_timers + " timers!");109}110}111112function _clearTimers() {113for (var token in timers) {114delete timers[token];115}116}117118function _getNextTimerToken() {119var nextTimerToken = null;120var minTime = 31536000000; // One year121// Find the next to invoke timer122for (var token in timers) {123if (timers[token].time < minTime) {124nextTimerToken = token;125minTime = timers[token].time;126}127}128return nextTimerToken;129}130131var mockTimers = {132setTimeout: _setTimeout,133clearTimeout: _clearTimeout,134setInterval: _setInterval,135clearInterval: _clearInterval,136137/**138* Copyright 2013-2014, Facebook, Inc.139* All rights reserved.140*141* This source code is licensed under the BSD-style license found in the142* LICENSE file in the root directory of this source tree. An additional grant143* of patent rights can be found in the PATENTS file in the same directory.144*145* Iteratively run callbacks in time order during the time from now to146* now + delay.147* If one callback register another timer which should be run during now to148* now + delay, the new timer will also be run in the right order.149*150* @param delay151*/152runTimersToTime: _runTimersToTime,153154/**155* Run all registered timer once. Newly registered timers will not be run.156*/157runTimersOnce: _runTimersOnce,158159/**160* Iteratively run callbacks until there are no timers left to call. Will161* stop after a maximum number of iterations to avoid infinite loop.162*163* @param maximum iterations (optional)164*/165runTimersRepeatedly: _runTimersRepeatedly,166167/**168* Clear all timers169*/170clearTimers: _clearTimers,171172/**173* Get the number of remaining timers174*/175getTimersCount: function() {176return Object.keys(timers).length;177}178};179180module.exports.installMockTimers = function(window) {181window._originalTimeouts = {182setTimeout: window.setTimeout,183clearTimeout: window.clearTimeout,184setInterval: window.setInterval,185clearInterval: window.clearInterval186};187window.setTimeout =188mocks.getMockFunction().mockImplementation(mockTimers.setTimeout);189window.clearTimeout =190mocks.getMockFunction().mockImplementation(mockTimers.clearTimeout);191window.setInterval =192mocks.getMockFunction().mockImplementation(mockTimers.setInterval);193window.clearInterval =194mocks.getMockFunction().mockImplementation(mockTimers.clearInterval);195window.mockRunTimersOnce =196mocks.getMockFunction().mockImplementation(mockTimers.runTimersOnce);197window.mockRunTimersToTime =198mocks.getMockFunction().mockImplementation(mockTimers.runTimersToTime);199window.mockRunTimersRepeatedly =200mocks.getMockFunction().mockImplementation(mockTimers.runTimersRepeatedly);201window.mockClearTimers =202mocks.getMockFunction().mockImplementation(mockTimers.clearTimers);203window.mockGetTimersCount =204mocks.getMockFunction().mockImplementation(mockTimers.getTimersCount);205};206207module.exports.uninstallMockTimers = function(window) {208window.setTimeout = window._originalTimeouts.setTimeout;209window.clearTimeout = window._originalTimeouts.clearTimeout;210window.setInterval = window._originalTimeouts.setInterval;211window.clearInterval = window._originalTimeouts.clearInterval;212window._originalTimeouts = undefined;213window.mockRunTimersOnce = undefined;214window.mockRunTimersToTime = undefined;215window.mockRunTimersRepeatedly = undefined;216window.mockClearTimers = undefined;217window.mockGetTimersCount = undefined;218};219220221