Created
July 27, 2012 21:22
-
-
Save fivetanley/3190522 to your computer and use it in GitHub Desktop.
amdMock
This file contains hidden or 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
/*global requirejs:false requirejsVars: false*/ | |
define( function() { | |
'use strict'; | |
function randomContextName( contexts ) { | |
var randID = Math.random().toString( 36 ).substring( 9 ); | |
if ( contexts[ randID ] ) { | |
return randomContextName( contexts ); | |
} | |
return randID; | |
} | |
/** | |
* For each dependency, load the mock first, or use baseURL if not | |
* defined in mocks object. | |
* @function | |
* @name mockAMD | |
* @license MIT | |
* @author Stanley Stuart <@fivetanley> | |
* @param { Array } dependencies - the module dependencies you'd expect | |
* in a typical require call. e.g. [ 'foo', 'bar', 'baz/foo' ] | |
* @param { Object } mocks - any dependencies you want to mock. | |
* Example: | |
* { | |
* // 'foo' is the KEY!!! | |
* 'foo': { | |
* 'bar': 'baz' | |
* } | |
* } | |
* | |
* Would return an object { 'bar': 'baz' } for dependency named 'foo' | |
* | |
* The KEY is the name and is resolved via baseUrl. e.g. 'foo', would | |
* map to $baseUrl/foo.js | |
* | |
* e.g. 'foo/bar' would resolve to $baseURL/foo/bar.js | |
* | |
* @param { Function } callback - function to call after dependencies | |
* have been loaded and mocked | |
* | |
* @example - | |
* // a simple module that has a dependency | |
* // dep1.js | |
* define( [ 'dep2' ], function() { | |
* return { | |
* say: function() { | |
* return dep2; | |
* } | |
* } | |
* }); | |
* // a sample jasmine test | |
* define([ 'mockAMD' ], function (mockAMD) { | |
* // dep1 depends upon dep2 | |
* var mocks = { | |
* 'dep2': 'hello' | |
* }; | |
* | |
* mockAMD( [ 'dep1'], mocks, function( dep1 ) { | |
* // should pass | |
* describe( 'dep1#say', function() { | |
* it( 'says its dependency', function() { | |
* expect( dep1.say ).toEqual( 'hello' ); | |
* }); | |
* }); | |
* }); | |
* }); | |
* | |
* | |
*/ | |
return function( dependencies, mocks, callback ) { | |
var environment = require | |
, contexts = environment.s.contexts | |
, globalContext = contexts._ | |
, baseUrl = globalContext.config.baseUrl | |
, contextName = randomContextName( contexts ) | |
, context | |
, mock; | |
// create the context | |
context = require.config( { | |
context: contextName | |
, baseUrl: baseUrl | |
}); | |
// create the mocks in the temporary context | |
for ( mock in mocks ) { | |
if ( mocks.hasOwnProperty( mock ) ) { | |
contexts[ contextName ].defined[ mock ] = mocks[ mock ]; | |
} | |
} | |
context( dependencies, function( ) { | |
callback.apply( null, arguments ); | |
// clean up temporary context | |
contexts[ contextName ] = undefined; | |
}); | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is brilliant dude, I'm not just saying that. So simple. But accomplishes the task. Made a few modifications for my needs but ultimately, uses this idea. Thank you!