{"size": 4952, "ext": "js", "max_stars_repo_path": "test/lib/tasks/bundlers/generateStandaloneAppBundle.js", "max_stars_repo_name": "ProfilingPromisesBenchmarks/ui5-builder", "max_stars_repo_stars_event_min_datetime": "2018-03-29T13:05:13.000Z", "max_stars_repo_stars_event_max_datetime": "2022-02-10T10:39:54.000Z", "max_issues_repo_path": "test/lib/tasks/bundlers/generateStandaloneAppBundle.js", "max_issues_repo_name": "ProfilingPromisesBenchmarks/ui5-builder", "max_issues_repo_issues_event_min_datetime": "2018-03-30T14:17:47.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-31T12:13:26.000Z", "max_forks_repo_path": "test/lib/tasks/bundlers/generateStandaloneAppBundle.js", "max_forks_repo_name": "thuyboehm/ui5-builder", "max_forks_repo_forks_event_min_datetime": "2018-06-19T10:12:38.000Z", "max_forks_repo_forks_event_max_datetime": "2022-02-27T08:55:03.000Z"}
{"max_stars_count": 63, "max_issues_count": 652, "max_forks_count": 31, "avg_line_length": 32.1558441558, "max_line_length": 108, "alphanum_fraction": 0.718901454}
const test = require("ava");
const chai = require("chai");
chai.use(require("chai-fs"));
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, 2, "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/**/manifest.json",
		"some/project/namespace/changes/changes-bundle.json",
		"some/project/namespace/changes/flexibility-bundle.json",
		"!some/project/namespace/test/",
		"sap/ui/core/Core.js"
	], "Correct filter in second bundle definition section");
	t.deepEqual(options.bundleDefinition.defaultFileTypes, [
		".js",
		".control.xml",
		".fragment.html",
		".fragment.json",
		".fragment.xml",
		".view.html",
		".view.json",
		".view.xml",
		".properties"
	], "Correct default file types in bundle definition");
});

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, 2, "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, [
		"/",
		"/**/manifest.json",
		"/changes/changes-bundle.json",
		"/changes/flexibility-bundle.json",
		"!/test/",
		"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, 2, "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/**/manifest.json",
		"some/project/namespace/changes/changes-bundle.json",
		"some/project/namespace/changes/flexibility-bundle.json",
		"!some/project/namespace/test/",
		"sap/ui/core/Core.js"
	], "Correct filter in second bundle definition section");
});
