Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
rhpijnacker-philips committed Jul 6, 2020
0 parents commit d85133e
Show file tree
Hide file tree
Showing 11 changed files with 1,925 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"# intern-issue-1168"
5 changes: 5 additions & 0 deletions app/mocked.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
define({
true: function () {
return false;
},
});
34 changes: 34 additions & 0 deletions app/mocked.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
var registerSuite = intern.getInterface('object').registerSuite;
var assert = intern.getPlugin('chai').assert;

define(['./require', 'module', 'dojo/Deferred'], function (
require,
module,
Deferred
) {
var src;
var requireContext;

registerSuite(module.id, {
before: function () {
var deferred = new Deferred();
requireContext = require({
map: { '*': { 'app/src': 'app/mocked' } },
}, ['app/src'], function (mockedSrc) {
src = mockedSrc;
deferred.resolve();
});
return deferred.promise;
},

after: function () {
requireContext.restore();
},

tests: {
'mocked src': function () {
assert.isFalse(src.true());
},
},
});
});
88 changes: 88 additions & 0 deletions app/require.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Copyright Koninklijke Philips N.V. 2018
//
// All rights are reserved. Reproduction or transmission in whole or in part, in
// any form or by any means, electronic, mechanical or otherwise, is prohibited
// without the prior written consent of the copyright owner.
//

// The dojo AMD loader has an optional first 'config' parameter. This is used
// most often in unittest code to magically map moduleIds to a stub implementation.
// Unfortunately, this has a persistent side effect on the loader's internal
// administration. This version of 'require' provides a way to undo that.
//
// Usage:
// define([ ..., 'interntests/require'], function( ..., require) {
// var requireContext;
// before: function() {
// requireContext = require(
// { map: { '*': { ... } } }, // <--- this is where the magic happens
// [ ... ],
// function( ... ) { ... }
// );
// },
// after: function() {
// requireContext.restore();
// }
// });
//
// Note:
// the map should override on '*' level, so it gets correctly replaced while undoing.
//
// Mapping data-dojo-types
// -----------------------
// In cases where the stubbed modules are declared as a data-dojo-type in a template,
// the _WidgetsInTemplateMixin needs to be aware of the mapping when it instantiates
// the module instances. This is done by passing the 'require' function while instantiating
// the widget, like this:
//
// var instance = new MyWidget({ contextRequire: require });
//
// _WidgetsInTemplateMixin will look for such a contextRequire and use it if available.
// This ensures the map is applied at this point.
//
// Note:
// Unfortunately, when it creates the widgets in the template, it does not pass the
// contextRequire to their constructor. This means that mappings will not be applied to
// nested widgets, which may be unexpected and unintended.
// To fix this we would have to change the dojo parser.
//

define(['require', 'dojo/_base/lang'], function (require, lang) {
function isString(object) {
return {}.toString.call(object) === '[object String]';
}

function testRequire(
config,
deps,
callback,
referenceModule,
contextRequire
) {
if (Array.isArray(config) || isString(config)) {
// require is called without a config object, just forward to actual require
return require(config, deps, callback, referenceModule, contextRequire);
}
var globalRequireMap = lang.mixin({ '*': {} }, require.map);
var mergedMap = lang.mixin({}, globalRequireMap, config.map);
var mergedConfig = lang.mixin({}, config, { map: mergedMap });
// Clear the dependencies from the cache so they will get reloaded/redefined with the mapped configuration.
deps.forEach(require.undef);
// Call the actual require -- this will persistently update the global map internally
require(mergedConfig, deps, callback, referenceModule, contextRequire);
return {
restore: function restore() {
// Clear the loaded dependencies from the cache to avoid them being used with the mapped configuration.
deps.forEach(require.undef);
// Clear the constructor cache built-up by the dojo parser
delete testRequire._dojoParserCtorMap;
// Restore the updated global map to its original value
require({ map: globalRequireMap });
},
};
}

testRequire.undef = require.undef;

return testRequire;
});
8 changes: 8 additions & 0 deletions app/src.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
define({
true: function () {
return true;
},
trueToo: function () {
return true;
},
});
12 changes: 12 additions & 0 deletions app/src.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
var registerSuite = intern.getInterface('object').registerSuite;
var assert = intern.getPlugin('chai').assert;

define(['module', 'app/src'], function (module, src) {
registerSuite(module.id, {
tests: {
'regular src': function () {
assert.isTrue(src.true());
},
},
});
});
23 changes: 23 additions & 0 deletions app/src.test2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
var registerSuite = intern.getInterface('object').registerSuite;
var assert = intern.getPlugin('chai').assert;

define(['module', 'dojo/Deferred'], function (module, Deferred) {
var src;

registerSuite(module.id, {
before: function () {
var deferred = new Deferred();
require(['app/src'], function (delayedSrc) {
src = delayedSrc;
deferred.resolve();
});
return deferred.promise;
},

tests: {
'regular src 2': function () {
assert.isTrue(src.trueToo());
},
},
});
});
1 change: 1 addition & 0 deletions app/tests.all.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
define(['./src.test', './mocked.test', './src.test2'], () => {});
20 changes: 20 additions & 0 deletions intern.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"loader": {
"script": "dojo",
"options": {
"has": {
"dojo-undef-api": true
},
"packages": [
{ "name": "app", "location": "app" },
{ "name": "dojo", "location": "node_modules/dojo" }
]
}
},
"suites": "app/tests.all",
"environments": {
"browserName": "chrome",
"fixSessionCapabilities": "no-detect"
},
"coverage": ["app/src.js"]
}
Loading

0 comments on commit d85133e

Please sign in to comment.