Created
April 30, 2015 17:34
-
-
Save dacrazycoder/3ccf4f0f47bda6fcd0d2 to your computer and use it in GitHub Desktop.
Mocking AMD calls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define([ | |
'intern', | |
'dojo/Deferred', | |
"dojo/aspect", | |
'./MockManager', | |
], function ( | |
intern, | |
Deferred, | |
aspect, | |
MockManager | |
) { | |
/** | |
* Extends the 'Define' to allow it's dependencies to be captures on the object returned | |
*/ | |
if (!window._origDefine) { | |
// Preserve the old define | |
window._origDefine = define; | |
// Add some wrappers to tickle injected values | |
window._mockDefine = function(modules, func, testdata) { | |
var funcWrapper = function() { | |
var args = arguments; | |
var result = func.apply(func, args); | |
if (result) { | |
result._modules = {}; | |
// Map the dependencies | |
for (var i=0,l=modules.length;i<l;i++) { | |
var moduleId = modules[i], | |
moduleValue = args[i]; | |
result._modules[moduleId] = moduleValue; | |
} | |
} | |
// Return the object with its module dependencies mapped | |
return result; | |
} | |
return window._origDefine(modules, funcWrapper); | |
}; | |
} | |
/** | |
* @description Mocks AMD dependencies for unit tests | |
* @param {string} moduleId - Defines the moduleId that will have its dependencies remapped | |
* @param {object} dependencyMap - loader map of each current dependencyId to mockObjId for remapping | |
* | |
* @example | |
* return MockAMD.mock('stx/gui/MessagesToolbar', { | |
* 'stx/gui/MessagesModel': 'tests/gui/messages/toolbar/mocks/MessagesModel' | |
* }).then(function(mocked) { | |
* MessagesToolbar = mocked; | |
* }); | |
* | |
* | |
*/ | |
return function MockAMD(moduleId, dependencyMap) { | |
var dfd = new Deferred(); | |
try { | |
// retrieve the original module values so they can be | |
// restored after the mocked copy has loaded | |
var originalModule; | |
var originalDependencies = {}; | |
var NOT_LOADED = {}; | |
try { | |
originalModule = require(moduleId); | |
require.undef(moduleId); | |
} catch (error) { | |
originalModule = NOT_LOADED; | |
} | |
// Unmap existing dependencies | |
for (var dependencyId in dependencyMap) { | |
try { | |
originalDependencies[dependencyId] = require(dependencyId); | |
require.undef(dependencyId); | |
} catch (error) { | |
originalDependencies[dependencyId] = NOT_LOADED; | |
} | |
} | |
// remap the module's dependencies with the provided map | |
var map = intern.config.loader.map || {}; | |
map[moduleId] = dependencyMap; | |
require({ | |
map: map | |
}); | |
// Swap the defines | |
window.define = window._mockDefine; | |
// reload the module using the mocked dependencies | |
require([moduleId], function (mockedModule) { | |
// restore the original condition of the loader by | |
// replacing all the modules that were unloaded | |
require.undef(moduleId); | |
if (originalModule !== NOT_LOADED) { | |
define(moduleId, [], function () { | |
return originalModule; | |
}); | |
} | |
for (var dependencyId in dependencyMap) { | |
map[moduleId][dependencyId] = dependencyId; | |
require.undef(dependencyId); | |
(function (originalDependency) { | |
if (originalDependency !== NOT_LOADED) { | |
define(dependencyId, [], function () { | |
return originalDependency; | |
}); | |
} | |
})(originalDependencies[dependencyId]); | |
} | |
require({ | |
map: map | |
}); | |
// restore the define.. | |
window.define = window._origDefine; | |
// provide the mocked copy to the caller | |
dfd.resolve(mockedModule); | |
}); | |
}catch(e) { | |
console.log(e); | |
} | |
return dfd.promise; | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment