From 3eeb04e746798b33b182e7235d692efeff5cf8e5 Mon Sep 17 00:00:00 2001 From: Joel Chen Date: Sat, 27 Mar 2021 14:06:04 -0700 Subject: [PATCH] fix subapp v1 loadSubAppBundles --- packages/subapp-web/package.json | 5 +- packages/subapp-web/src/subapp-web.js | 15 +++-- .../subapp-web/test/spec/subapp-web.spec.js | 59 +++++++++++++++++++ 3 files changed, 69 insertions(+), 10 deletions(-) create mode 100644 packages/subapp-web/test/spec/subapp-web.spec.js diff --git a/packages/subapp-web/package.json b/packages/subapp-web/package.json index 0bf8593b6..b1f79d713 100644 --- a/packages/subapp-web/package.json +++ b/packages/subapp-web/package.json @@ -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" @@ -84,7 +84,8 @@ "dist", "test", "browser", - "node-dist" + "node-dist", + ".babelrc.js" ], "check-coverage": true, "statements": 0, diff --git a/packages/subapp-web/src/subapp-web.js b/packages/subapp-web/src/subapp-web.js index 6865e8228..95227e7fd 100644 --- a/packages/subapp-web/src/subapp-web.js +++ b/packages/subapp-web/src/subapp-web.js @@ -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; } @@ -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(); @@ -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(); }, diff --git a/packages/subapp-web/test/spec/subapp-web.spec.js b/packages/subapp-web/test/spec/subapp-web.spec.js new file mode 100644 index 000000000..d854a6958 --- /dev/null +++ b/packages/subapp-web/test/spec/subapp-web.spec.js @@ -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(` + + + +

Hello world

+ +`); + + 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 }); + } + ); + }); +});