Skip to content

Commit

Permalink
[INTERNAL] generateStandaloneAppBundle: Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
RandomByte committed Jan 21, 2019
1 parent 184fe52 commit a83b034
Showing 1 changed file with 139 additions and 4 deletions.
143 changes: 139 additions & 4 deletions test/lib/tasks/bundlers/generateStandaloneAppBundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,148 @@ const path = require("path");
const chai = require("chai");
chai.use(require("chai-fs"));
const assert = chai.assert;
const sinon = require("sinon");
const mock = require("mock-require");

let generateStandaloneAppBundle = require("../../../../lib/tasks/bundlers/generateStandaloneAppBundle");

test.beforeEach((t) => {
// Stubbing processors/bundlers/moduleBundler
t.context.moduleBundlerStub = sinon.stub();
t.context.moduleBundlerStub.resolves(["I am a resource"]);
mock("../../../../lib/processors/bundlers/moduleBundler", t.context.moduleBundlerStub);

// Re-require tested module
generateStandaloneAppBundle = mock.reRequire("../../../../lib/tasks/bundlers/generateStandaloneAppBundle");
});

test.afterEach.always((t) => {
mock.stopAll();
sinon.restore();
});

test.serial("execute module bundler and write results", async (t) => {
const dummyResource = {
getPath: function() {
return "ponyPath";
}
};
const dummyReaderWriter = {
byGlob: async function() {
return [dummyResource, dummyResource];
},
write: function() {}
};
sinon.stub(dummyReaderWriter, "write").resolves();
const params = {
workspace: dummyReaderWriter,
dependencies: dummyReaderWriter,
options: {
projectName: "some.project.name",
namespace: "some/project/namespace"
}
};
await generateStandaloneAppBundle(params);

t.deepEqual(t.context.moduleBundlerStub.callCount, 1, "moduleBundler should be called once");

const {resources, options} = t.context.moduleBundlerStub.getCall(0).args[0];
t.deepEqual(resources.length, 4, "moduleBundler got supplied with 4 resources");
t.deepEqual(options.bundleDefinition.sections[0].filters, [
"jquery.sap.global.js"
], "Correct filter in first bundle definition section");
t.deepEqual(options.bundleDefinition.sections[1].filters, [
"some/project/namespace/",
"!some/project/namespace/test/",
"!some/project/namespace/*.html",
"sap/ui/core/Core.js"
], "Correct filter in second bundle definition section");
});

test.serial("execute module bundler and write results without namespace", async (t) => {
const dummyResource = {
getPath: function() {
return "ponyPath";
}
};
const dummyReaderWriter = {
byGlob: async function() {
return [dummyResource, dummyResource];
},
write: function() {}
};
sinon.stub(dummyReaderWriter, "write").resolves();
const params = {
workspace: dummyReaderWriter,
dependencies: dummyReaderWriter,
options: {
projectName: "some.project.name"
}
};
await generateStandaloneAppBundle(params);

t.deepEqual(t.context.moduleBundlerStub.callCount, 1, "moduleBundler should be called once");

const {resources, options} = t.context.moduleBundlerStub.getCall(0).args[0];
t.deepEqual(resources.length, 4, "moduleBundler got supplied with 4 resources");
t.deepEqual(options.bundleDefinition.sections[0].filters, [
"jquery.sap.global.js"
], "Correct filter in first bundle definition section");
t.deepEqual(options.bundleDefinition.sections[1].filters, [
"/",
"!/test/",
"!/*.html",
"sap/ui/core/Core.js"
], "Correct filter in second bundle definition section");
});


test.serial("execute module bundler and write results in evo mode", async (t) => {
const dummyResource = {
getPath: function() {
return "/resources/ui5loader.js"; // Triggers evo mode
}
};
const dummyReaderWriter = {
byGlob: async function() {
return [dummyResource, dummyResource];
},
write: function() {}
};
sinon.stub(dummyReaderWriter, "write").resolves();
const params = {
workspace: dummyReaderWriter,
dependencies: dummyReaderWriter,
options: {
projectName: "some.project.name",
namespace: "some/project/namespace"
}
};
await generateStandaloneAppBundle(params);

t.deepEqual(t.context.moduleBundlerStub.callCount, 1, "moduleBundler should be called once");

const {resources, options} = t.context.moduleBundlerStub.getCall(0).args[0];
t.deepEqual(resources.length, 4, "moduleBundler got supplied with 4 resources");
t.deepEqual(options.bundleDefinition.sections[0].filters, [
"ui5loader-autoconfig.js"
], "Evo mode active - Correct filter in first bundle definition section");
t.deepEqual(options.bundleDefinition.sections[1].filters, [
"some/project/namespace/",
"!some/project/namespace/test/",
"!some/project/namespace/*.html",
"sap/ui/core/Core.js"
], "Correct filter in second bundle definition section");
});

/* =================*/
/* Integration tests */

const recursive = require("recursive-readdir");
const ui5Builder = require("../../../../");
const builder = ui5Builder.builder;
const applicationBPath = path.join(__dirname, "..", "..", "..", "fixtures", "application.b");

const recursive = require("recursive-readdir");

const findFiles = (folder) => {
return new Promise((resolve, reject) => {
recursive(folder, (err, files) => {
Expand All @@ -24,6 +158,7 @@ const findFiles = (folder) => {
};

test("integration: build application.b standalone", async (t) => {
// beforeEach mocks do not apply to this test as all modules have already been required via ui5Builder require above
const destPath = "./test/tmp/build/application.b/standalone";
const expectedPath = "./test/expected/build/application.b/standalone";
const excludedTasks = ["*"];
Expand All @@ -38,14 +173,14 @@ test("integration: build application.b standalone", async (t) => {
return findFiles(expectedPath);
}).then((expectedFiles) => {
// Check for all directories and files
assert.directoryDeepEqual(destPath, expectedPath);
assert.directoryDeepEqual(destPath, expectedPath, "Result directory structure correct");

// Check for all file contents
t.deepEqual(expectedFiles.length, 10, "10 files are expected");
expectedFiles.forEach((expectedFile) => {
const relativeFile = path.relative(expectedPath, expectedFile);
const destFile = path.join(destPath, relativeFile);
assert.fileEqual(destFile, expectedFile);
assert.fileEqual(destFile, expectedFile, "Correct file content");
});
}));
});
Expand Down

0 comments on commit a83b034

Please sign in to comment.