Skip to content

Commit

Permalink
fix subapp v1 loadSubAppBundles
Browse files Browse the repository at this point in the history
  • Loading branch information
jchip committed Mar 27, 2021
1 parent e39ed8c commit 3eeb04e
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 10 deletions.
5 changes: 3 additions & 2 deletions packages/subapp-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"chai-as-promised": "^7.1.1",
"electrode-archetype-njs-module-dev": "^3.0.3",
"electrode-react-webapp": "^3.8.9",
"jsdom": "^15.2.1",
"jsdom": "^16.5.1",
"mock-require": "^1.3.0",
"run-verify": "^1.2.2",
"subapp-pkg-util": "../subapp-pkg-util"
Expand Down Expand Up @@ -84,7 +84,8 @@
"dist",
"test",
"browser",
"node-dist"
"node-dist",
".babelrc.js"
],
"check-coverage": true,
"statements": 0,
Expand Down
15 changes: 7 additions & 8 deletions packages/subapp-web/src/subapp-web.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,7 @@
getBundle(name, namespace) {
namespace = ensureNamespace(namespace);

if (
!name ||
!runtimeInfo.bundles[namespace] ||
!runtimeInfo.bundles[namespace][name.toLowerCase()]
) {
if (!name || !runtimeInfo.bundles[namespace]) {
return;
}

Expand Down Expand Up @@ -334,7 +330,10 @@
done = done || (() => {});
namespace = ensureNamespace(namespace);

const toLoad = [].concat(names).filter(x => xv1.getBundle(x, namespace) === undefined);
const toLoad = [].concat(names).filter(x => {
const s = xv1.getBundle(x, namespace);
return s === undefined;
});

if (toLoad.length === 0) {
return done();
Expand Down Expand Up @@ -372,13 +371,13 @@
const afterLoad = () => {
loaded.push(assets);
if (loaded.length === assetsToLoad.length) {
console.log("all assets loaded", assetsToLoad);
console.debug("all assets loaded", assetsToLoad);
done();
}
};
loadjs(new_assets, id, {
success: () => {
console.log(`loaded asset for ${name} (id: ${id}) - ${assets}`);
console.debug(`loaded asset for ${name} (id: ${id}) - ${assets}`);
xv1.setBundle(id, 1, namespace);
afterLoad();
},
Expand Down
59 changes: 59 additions & 0 deletions packages/subapp-web/test/spec/subapp-web.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
"use strict";

/* eslint-disable max-len */

const { asyncVerify } = require("run-verify");
const { describe, it, before, after } = require("mocha");
const { expect } = require("chai");
const { JSDOM } = require("jsdom");
const { delay } = require("xaa");

describe("subapp-web xarc client v1", function () {
let xarcV1;
const dom = new JSDOM(`<!DOCTYPE html><html><head>
<script id="bundleAssets" type="application/json">
{"jsChunksById":{"bottom":"/js/bottom.bundle.dev.js","deal":"/js/deal.bundle.dev.js","extras":"/js/extras.bundle.dev.js","footer":"/js/footer.bundle.dev.js","header":"/js/header.bundle.dev.js","mainbody":"/js/mainbody.bundle.dev.js","runtime":"/js/runtime.bundle.dev.js","suspensedemo":"/js/suspensedemo.bundle.dev.js","vendors.~199adadd":"/js/vendors.~199adadd.bundle.dev.js","vendors.~73011e79":"/js/vendors.~73011e79.bundle.dev.js","vendors.~a8cff946":"/js/vendors.~a8cff946.bundle.dev.js"},"md":{},"entryPoints":{"mainbody":["runtime","vendors.~73011e79","vendors.~a8cff946","vendors.~199adadd","mainbody"],"bottom":["runtime","vendors.~73011e79","vendors.~a8cff946","vendors.~199adadd","bottom"],"header":["runtime","vendors.~73011e79","header"],"footer":["runtime","vendors.~73011e79","vendors.~a8cff946","footer"],"extras":["runtime","vendors.~73011e79","vendors.~a8cff946","extras"],"suspensedemo":["runtime","vendors.~73011e79","suspensedemo"],"deal":["runtime","vendors.~73011e79","deal"]},"basePath":""}
</script>
</head>
<body>
<p>Hello world</p>
</body>
</html>`);

const jsLoaded = {};

before(() => {
const xarc = "../../src/subapp-web.js";
delete require.cache[require.resolve(xarc)];
global.window = dom.window;
global.document = dom.window.document;
global.loadjs = (asset, id, options) => {
if (jsLoaded[id]) {
throw new Error(`loadjs ${id} already loaded`);
}
jsLoaded[id] = true;
setTimeout(() => options.success(), 20);
};
require(xarc);
xarcV1 = global.window.xarcV1;
});

after(() => {
delete global.window;
delete global.document;
delete global.loadjs;
});

it("should handle loadSubAppBundles multiple times", () => {
return asyncVerify(
() => {
xarcV1.loadSubAppBundles("deal", () => {});
xarcV1.loadSubAppBundles("deal", () => {});
},
() => delay(50),
() => {
expect(jsLoaded).to.deep.equal({ runtime: true, "vendors.~73011e79": true, deal: true });
}
);
});
});

0 comments on commit 3eeb04e

Please sign in to comment.