Skip to content

Commit

Permalink
no longer tests for modules in the cache by checking for truthiness (…
Browse files Browse the repository at this point in the history
…leftover from earlier assumption that all modules would have exports objects). with tests
  • Loading branch information
John Hann committed Feb 28, 2012
1 parent a11826c commit efd3726
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/curl.js
Expand Up @@ -820,9 +820,10 @@
}
}

// find resource definition
// find resource definition. ALWAYS check via (id in cache) b/c
// falsey values could be in there.
def = cache[mainId];
if (!def) {
if (!(mainId in cache)) {
def = cache[mainId] = core.createResourceDef(pathInfo.config, mainId, isPreload, !!parts.pluginId ? pathInfo.path : undef);
def.url = core.checkToAddJsExt(pathInfo.url);
core.fetchResDef(def);
Expand Down Expand Up @@ -865,7 +866,7 @@
normalizedDef = cache[fullId];

// if this is our first time fetching this (normalized) def
if (!normalizedDef) {
if (!(fullId in cache)) {

// because we're using resId, plugins, such as wire!,
// can use paths relative to the resource
Expand Down Expand Up @@ -983,7 +984,7 @@
// named define(), it is in the cache if we are loading a dependency
// (could also be a secondary define() appearing in a built file, etc.)
var def = cache[id];
if (!def) {
if (!(id in cache)) {
// id is an absolute id in this case, so we can get the config.
// there's no way to allow a named define to fetch dependencies
// in the preload phase since we can't cascade the parent def.
Expand Down
104 changes: 104 additions & 0 deletions test/module-returns-undefined.html
@@ -0,0 +1,104 @@
<!DOCTYPE HTML>
<html>
<head>
<title>undefined module loads once</title>

<script>
curl = {
baseUrl: '',
paths: {
curl: '../src/curl'
}
};

</script>
<script src="../src/curl.js" type="text/javascript"></script>

<script type="text/javascript">

curl(['curl/_privileged', 'domReady!'], function (priv) {

// override curl's insertScript function with an injected spy

function MethodSpy (method, context) {
var count, orig, spy;

count = 0;
orig = context[method];

context[method] = function () {
count++;
return orig.apply(context, arguments);
};

return {
calledNever: function () { return count == 0; },
calledOnce: function () { return count == 1; },
calledTwice: function () { return count == 2; },
calledMany: function (howMany) { return count == howMany; }
};
}

var core = priv.core,
origLoadScript = core.loadScript,
Promise = priv.Promise,
module1 = 'stuff/undefined-module';

function runTest (moduleId, howMany) {

var spy = new MethodSpy('loadScript', core),
promise = new Promise(),
count = 2;

function countdown () {
if (--count == 0) {
promise.resolve();
}
}

curl([moduleId], function (whatevs) {}).then(
countdown,
function (ex) {
write('FAILED: on ' + moduleId + ' exception:' + ex);
}
);

curl([moduleId], function (whatevs) {}).then(
countdown,
function (ex) {
write('FAILED: on ' + moduleId + ' exception:' + ex);
}
);

if (spy.calledMany(howMany || 0)) {
write('SUCCESS: loaded ' + moduleId + ' exactly once');
}
else {
write('FAILED: loaded ' + moduleId + ' more than once');
}

priv.core.loadScript = origLoadScript;

return promise;

}
window.runTest = runTest;
window.module1 = module1;

runTest(module1, 1);

});

function write (msg) {
document.body.appendChild(document.createElement('div')).innerHTML = msg;
}

</script>

</head>
<body>

<button onclick="runTest(window.module1);">press this button to load the local module again</button>
<button onclick="curl('curl/_privileged',console.log);">dump cache to console</button>
</body>
</html>
3 changes: 3 additions & 0 deletions test/stuff/undefined-module.js
@@ -0,0 +1,3 @@
define(function () {
// returns nothing
});

0 comments on commit efd3726

Please sign in to comment.