Skip to content

Commit

Permalink
add ResourceCollector unit tests
Browse files Browse the repository at this point in the history
* groupResourcesByComponents
* visitResource
  • Loading branch information
tobiasso85 committed Aug 7, 2020
1 parent 6dd31e3 commit 18fb726
Showing 1 changed file with 45 additions and 5 deletions.
50 changes: 45 additions & 5 deletions test/lib/lbt/resources/ResourceCollector.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,40 @@
const test = require("ava");
const sinon = require("sinon");
const mock = require("mock-require");

const ResourceCollector = require("../../../../lib/lbt/resources/ResourceCollector");
let ResourceCollector = require("../../../../lib/lbt/resources/ResourceCollector");

test("add: empty constructor dummy params", (t) => {
test.beforeEach((t) => {
// Spying logger of processors/bootstrapHtmlTransformer
const log = require("@ui5/logger");
const loggerInstance = log.getLogger("lbt:resources:ResourceCollector");
mock("@ui5/logger", {
getLogger: () => loggerInstance
});
mock.reRequire("@ui5/logger");
t.context.logWarnSpy = sinon.spy(loggerInstance, "warn");

// Re-require tested module
ResourceCollector = mock.reRequire("../../../../lib/lbt/resources/ResourceCollector");
});

test.afterEach.always((t) => {
mock.stop("@ui5/logger");
t.context.logWarnSpy.restore();
});


test.serial("add: empty constructor dummy params", (t) => {
const resourceCollector = new ResourceCollector({}, {});
t.is(resourceCollector.resources.size, 0, "empty");
});

test("add: empty constructor", (t) => {
test.serial("add: empty constructor", (t) => {
const resourceCollector = new ResourceCollector();
t.is(resourceCollector.resources.size, 0, "empty");
});

test("setExternalResources: empty filters", (t) => {
test.serial("setExternalResources: empty filters", (t) => {
const resourceCollector = new ResourceCollector();
resourceCollector.setExternalResources({
"testcomp": []
Expand All @@ -21,7 +43,7 @@ test("setExternalResources: empty filters", (t) => {
t.is(orphanFilters.size, 1, "1 filter");
});

test("createOrphanFilters: filters", (t) => {
test.serial("createOrphanFilters: filters", (t) => {
const resourceCollector = new ResourceCollector();
resourceCollector.setExternalResources({
"testcomp": ["test"],
Expand All @@ -33,3 +55,21 @@ test("createOrphanFilters: filters", (t) => {
const orphanFilters = resourceCollector.createOrphanFilters();
t.is(orphanFilters.size, 4, "4 filters");
});

test.serial("visitResource: path", (t) => {
const resourceCollector = new ResourceCollector();
resourceCollector.visitResource("mypath", 13);
t.is(t.context.logWarnSpy.callCount, 1);
t.is(t.context.logWarnSpy.getCall(0).args[0], "non-runtime resource mypath ignored");
});

test.serial("groupResourcesByComponents: debugBundles", (t) => {
const resourceCollector = new ResourceCollector();
resourceCollector.setExternalResources({
"testcomp": ["my/file.js"]
});
resourceCollector.visitResource("/resources/testcomp/Component.js", 13);
resourceCollector.visitResource("/resources/my/file.js", 13);
resourceCollector.groupResourcesByComponents({debugBundles: ".*-dbg.js"});
t.is(resourceCollector.resources.size, 0, "all resources were deleted");
});

0 comments on commit 18fb726

Please sign in to comment.