react / react-0.13.3 / examples / basic-commonjs / node_modules / reactify / node_modules / react-tools / src / test / mock-modules.js
81152 views/**1* Copyright 2013-2014, Facebook, Inc.2* All rights reserved.3*4* This source code is licensed under the BSD-style license found in the5* LICENSE file in the root directory of this source tree. An additional grant6* of patent rights can be found in the PATENTS file in the same directory.7*8* @providesModule mock-modules9*/1011var mocks = require("mocks");12var exportsRegistry = {};13var hasOwn = exportsRegistry.hasOwnProperty;14var explicitMockMap = {};1516function getMock(exports) {17try {18return mocks.generateFromMetadata(mocks.getMetadata(exports));19} catch (err) {20console.warn(err);21return exports;22}23}2425// This function should be called at the bottom of any module that might26// need to be mocked, after the final value of module.exports is known.27exports.register = function(id, module) {28exportsRegistry[id] = {29module: module,30actual: module.exports,31mocked: null // Filled in lazily later.32};3334// If doMock or doNotMock was called earlier, before the module was35// registered, then the choice should have been recorded in36// explicitMockMap. Now that the module is registered, we can finally37// fulfill the request.38if (hasOwn.call(explicitMockMap, id)) {39if (explicitMockMap[id]) {40doMock(id);41} else {42doNotMock(id);43}44}4546return exports;47};4849function resetEntry(id) {50if (hasOwn.call(exportsRegistry, id)) {51delete exportsRegistry[id].module.exports;52delete exportsRegistry[id];53}54}5556exports.dumpCache = function() {57require("mocks").clear();5859// Deleting module.exports will cause the module to be lazily60// reevaluated the next time it is required.61for (var id in exportsRegistry) {62resetEntry(id);63}6465return exports;66};6768exports.getMockMap = function() {69return explicitMockMap;70};7172exports.clearMockMap = function() {73explicitMockMap = {};74};7576exports.setMockMap = function(mockMap) {77exports.dumpCache();78exports.clearMockMap();79for (var id in mockMap) {80if (mockMap[id]) {81doMock(id);82} else {83doNotMock(id);84}85}8687return exports;88};8990// Call this function to ensure that require(id) returns the actual91// exports object created by the module.92function doNotMock(id) {93explicitMockMap[id] = false;9495var entry = exportsRegistry[id];96if (entry && entry.module && entry.actual) {97entry.module.exports = entry.actual;98}99100return exports;101}102103// Call this function to ensure that require(id) returns a mock exports104// object based on the actual exports object created by the module.105function doMock(id) {106explicitMockMap[id] = true;107108var entry = exportsRegistry[id];109if (entry && entry.module && entry.actual) {110// Because mocking can be expensive, create the mock exports object on111// demand, the first time doMock is called.112entry.mocked || (entry.mocked = getMock(entry.actual));113entry.module.exports = entry.mocked;114}115116return exports;117}118119var global = Function("return this")();120require('test/mock-timers').installMockTimers(global);121122// Exported names are different for backwards compatibility.123exports.dontMock = doNotMock;124exports.mock = doMock;125126127