Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some subapp web tests #1543

Merged
merged 2 commits into from
Feb 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/subapp-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,12 @@
"@babel/plugin-transform-runtime": "^7.8.3",
"@babel/preset-env": "^7.3.1",
"@babel/preset-react": "^7.0.0",
"@babel/register": "^7.7.7",
"babel-preset-minify": "^0.5.1",
"chai-as-promised": "^7.1.1",
"electrode-archetype-njs-module-dev": "^3.0.0",
"jsdom": "^15.2.1",
"mock-require": "^1.3.0",
"run-verify": "^1.2.2",
"subapp-pkg-util": "../subapp-pkg-util"
},
Expand Down
5 changes: 3 additions & 2 deletions packages/subapp-web/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,13 @@ export function getSubAppComponent({ name, timeout = 15000, onReady, onError, fa
//
}

export function waitForSubApp(name) {
export function waitForSubApp(name, timeout = 15000) {
return new Promise((resolve, reject) => {
dynamicLoadSubApp({
name,
onLoad: () => resolve(),
onError: () => reject()
onError: () => reject(),
timeout
});
});
}
Expand Down
1 change: 1 addition & 0 deletions packages/subapp-web/test/mocha.opts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
--require node_modules/electrode-archetype-njs-module-dev/config/test/setup.js
--require @babel/register
--recursive
260 changes: 257 additions & 3 deletions packages/subapp-web/test/spec/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,263 @@
"use strict";

const subAppWeb = require("../..");
const { JSDOM } = require("jsdom");
const mockRequire = require("mock-require");
const sinon = require("sinon");
const chai = require("chai");
const chaiAsPromised = require("chai-as-promised");
chai.use(chaiAsPromised);

const expect = chai.expect;

let clock;

describe("subapp-web", function() {
it("test", () => {
expect(subAppWeb).to.exist;
beforeEach(() => {
const dom = new JSDOM("");
clock = sinon.useFakeTimers();
global.window = dom.window;
global.document = dom.window.document;
delete require.cache[require.resolve("../../src/index")];
delete require.cache[require.resolve("../../src/xarc")];
delete require.cache[require.resolve("../../src/subapp-web")];
});

afterEach(() => {
try {
mockRequire.stop("../../src/xarc");
} catch (e) {} // eslint-disable-line
try {
clock.restore();
} catch (e) {} // eslint-disable-line
delete global.window;
delete global.document;
});

it("dynamicLoadSubApp should return inline if no id or onLoad are specified and both subapp and bundle are available", () => {
require("../../src/subapp-web");
const xarc = require("../../src/xarc").default;
const index = require("../../src/index");
require("../../src/subapp-web");
xarc.setSubApp("phantom-subapp", {start: () => "test string"});
xarc.setBundle("phantom-subapp", {});
const ret = index.dynamicLoadSubApp({name: "phantom-subapp"});
expect(ret).to.equal("test string");
});

it("dynamicLoadSubApp should run onLoad if it is specified but not id and both subapp and bundle are available", async () => {
let called = false;
mockRequire("../../src/xarc", {
getBundle: () => true,
getSubApp: () => true,
startSubApp: () => Promise.resolve()
});
const index = require("../../src/index");
index.dynamicLoadSubApp({name: "phantom-subapp", onLoad: () => {
called = true;
}});
expect(called).to.equal(false);
await clock.runAll();
expect(called).to.equal(true);
});

it("dynamicLoadSubApp should call loadSubAppBundles if the bundle is not available", async () => {
let called = false;
global.window = {};
mockRequire("../../src/xarc", {
getBundle: () => undefined,
loadSubAppBundles: () => {
called = true;
}
});
const index = require("../../src/index");
index.dynamicLoadSubApp({name: "phantom-subapp"});
expect(called).to.equal(true);
});

it("dynamicLoadSubApp should run subapp.start if id is specified", async () => {
let called = false;
global.window = {};
global.document = {
getElementById: () => true
};
mockRequire("../../src/xarc", {
getBundle: () => true,
getSubApp: () => ({
start: () => {
called = true;
}
}),
startSubApp: () => Promise.resolve()
});
const index = require("../../src/index");
index.dynamicLoadSubApp({name: "phantom-subapp", id: "test-id"});
expect(called).to.equal(false);
await clock.runAll();
expect(called).to.equal(true);
});

it("waitForSubApp should resolve promise if sub app is available", async () => {
mockRequire("../../src/xarc", {
getBundle: () => true,
getSubApp: () => true,
startSubApp: () => Promise.resolve()
});
const index = require("../../src/index");
const ret = index.waitForSubApp("phantom-subapp");
await clock.runAll();
await ret;
});

it("waitForSubApp should reject promise if startSubApp fails to complete", async () => {
mockRequire("../../src/xarc", {
getBundle: () => true,
getSubApp: () => false
});
const index = require("../../src/index");
const ret = index.waitForSubApp("phantom-subapp", 51);
await clock.runAll();
return expect(ret).to.be.rejected;
});

it("getBrowserHistory should create and return xarc.rt.history", async () => {
const xarc = {
rt: {}
};
mockRequire("../../src/xarc", xarc);
const index = require("../../src/index");
const history = index.getBrowserHistory();
expect(history).to.exist;
expect(xarc.rt.history).to.exist;
expect(history).to.equal(xarc.rt.history);
});

it("loadSubApp should run load subapp", async () => {
require("../../src/subapp-web");
const xarc = require("../../src/xarc").default;
const index = require("../../src/index");
const subAppInfo = {
name: "testsubapp",
Component: () => "asdf"
};
index.loadSubApp(subAppInfo);
const subApp = xarc.getSubApp("testsubapp");
expect(subApp._started).to.exist;
});

it("loadSubApp should call preStart, preRender, signalReady", async () => {
let preStartCalled = false;
let preRenderCalled = false;
let signalReadyCalled = false;
let startCalled = false;
require("../../src/subapp-web");
const xarc = require("../../src/xarc").default;
const index = require("../../src/index");
index.setupFramework(class {
renderStart() {}
});
const subAppInfo = {
name: "testsubapp",
Component: () => "asdf"
};
index.loadSubApp(subAppInfo);
xarc.startSubAppOnLoad({
name: "testsubapp"
});
const subApp = xarc.getSubApp("testsubapp");
subApp.preStart = () => {
preStartCalled = true;
return {};
};
subApp.preRender = () => { preRenderCalled = true; };
subApp.signalReady = () => { signalReadyCalled = true; };
subApp.start = () => { startCalled = true; };
expect(preStartCalled).to.equal(false);
expect(preRenderCalled).to.equal(false);
expect(signalReadyCalled).to.equal(false);
expect(startCalled).to.equal(false);
await clock.runAll();
clock.restore();
return new Promise((accept) => {
setTimeout(() => {
expect(preStartCalled).to.equal(true);
expect(preRenderCalled).to.equal(true);
expect(signalReadyCalled).to.equal(true);
expect(startCalled).to.equal(true);
accept();
}, 100);
});
});

it("loadSubApp should create a getInstance method", async () => {
require("../../src/subapp-web");
const xarc = require("../../src/xarc").default;
const index = require("../../src/index");
const subAppInfo = {
name: "testsubapp",
Component: () => "testcontent"
};
index.loadSubApp(subAppInfo);
const subApp = xarc.getSubApp("testsubapp");
const instance = subApp.getInstance({id: "testid"});
expect(instance).to.exist;
});

it("loadSubApp should create a start method", async () => {
require("../../src/subapp-web");
const xarc = require("../../src/xarc").default;
const index = require("../../src/index");
const subAppInfo = {
name: "testsubapp",
Component: () => "testcontent"
};
index.loadSubApp(subAppInfo);
const subApp = xarc.getSubApp("testsubapp");
expect(subApp.start).to.not.throw;
});

it("loadSubApp should create a inline method", async () => {
require("../../src/subapp-web");
const xarc = require("../../src/xarc").default;
const index = require("../../src/index");
const subAppInfo = {
name: "testsubapp",
Component: () => "testcontent"
};
index.loadSubApp(subAppInfo);
const subApp = xarc.getSubApp("testsubapp");
expect(subApp.inline).to.not.throw;
});

it("hotReloadSubApp should run subapp.start", async () => {
let called = false;
require("../../src/subapp-web");
const xarc = require("../../src/xarc").default;
const index = require("../../src/index");
const subAppInfo = {
name: "testsubapp",
Component: () => "asdf"
};
index.loadSubApp(subAppInfo);
const subApp = xarc.getSubApp(subAppInfo.name);
subApp.start = () => {
called = true;
};
subApp._started = [true];
index.hotReloadSubApp(subAppInfo);
expect(called).to.equal(false);
await clock.runAll();
expect(called).to.equal(true);
});

it("hotReloadSubApp should return true if subapp is loaded", async () => {
require("../../src/subapp-web");
const index = require("../../src/index");
const subAppInfo = {
name: "testsubapp",
Component: () => "asdf"
};
expect(index.isLoaded(subAppInfo.name)).to.equal(false);
index.loadSubApp(subAppInfo);
expect(index.isLoaded(subAppInfo.name)).to.equal(true);
});
});
90 changes: 90 additions & 0 deletions packages/subapp-web/test/spec/start.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,94 @@ describe("start", function() {
it("should return subapp start HTML", () => {
expect(startToken().process({ user: {} }, { props: {} })).contains("subapp start");
});

it("should call saveSSRInfo on independent group", async () => {
let called = false;
const xarcSubappSSR = {
_: {
queue: [{
awaitData: () => Promise.resolve(),
ready: {
promise: Promise.resolve()
},
saveSSRInfo: () => { called = true; }
}]
}
};
startToken().process({ user: { xarcSubappSSR } }, { props: {} });
return new Promise((accept) => {
setTimeout(() => {
expect(called).to.equal(true);
accept();
}, 100);
});
});

it("should call saveSSRInfo on normal group", async () => {
let called = false;
const xarcSubappSSR = {
"2": {
queue: [{
awaitData: () => Promise.resolve(),
ready: {
promise: Promise.resolve()
},
saveSSRInfo: () => { called = true; }
}]
}
};
startToken().process({ user: { xarcSubappSSR } }, { props: {} });
return new Promise((accept) => {
setTimeout(() => {
expect(called).to.equal(true);
accept();
}, 100);
});
});

it("should call renderSSR on normal group", async () => {
let called = false;
const xarcSubappSSR = {
"2": {
queue: [{
awaitData: () => Promise.resolve(),
ready: {
promise: Promise.resolve()
},
renderSSR: () => { called = true; }
}]
}
};
startToken().process({ user: { xarcSubappSSR } }, { props: {} });
return new Promise((accept) => {
setTimeout(() => {
expect(called).to.equal(true);
accept();
}, 100);
});
});

it("should call realizeReduxStore on normal group", async () => {
let called = false;
const xarcSubappSSR = {
"2": {
queue: [{
awaitData: () => Promise.resolve(),
ready: {
promise: Promise.resolve()
},
lib: {
realizeReduxStore: () => { called = true; }
}
}]
}
};
startToken().process({ user: { xarcSubappSSR } }, { props: {} });
return new Promise((accept) => {
setTimeout(() => {
expect(called).to.equal(true);
accept();
}, 100);
});
});
});