webpack/test/DependenciesBlockVariable.u...

93 lines
2.2 KiB
JavaScript
Raw Normal View History

"use strict";
const sinon = require("sinon");
2017-02-28 22:28:44 +08:00
const DependenciesBlockVariable = require("../lib/DependenciesBlockVariable");
2017-02-03 21:37:56 +08:00
describe("DependenciesBlockVariable", () => {
2018-02-25 09:00:20 +08:00
let DependenciesBlockVariableInstance, dependencyMock, sandbox;
before(() => {
sandbox = sinon.sandbox.create();
dependencyMock = {
constructor: {
name: "DependencyMock"
},
disconnect: sandbox.spy(),
updateHash: sandbox.spy()
};
2017-02-03 21:37:56 +08:00
DependenciesBlockVariableInstance = new DependenciesBlockVariable(
"dependencies-name",
2018-02-25 09:00:20 +08:00
"expression",
[dependencyMock]
);
});
afterEach(() => sandbox.restore());
describe("hasDependencies", () =>
it("returns `true` if has dependencies", () =>
2018-01-24 20:17:21 +08:00
expect(DependenciesBlockVariableInstance.hasDependencies()).toBe(true);
describe("disconnect", () =>
it("trigger dependencies disconnection", () => {
2017-02-03 21:37:56 +08:00
DependenciesBlockVariableInstance.disconnect();
2018-01-24 20:17:21 +08:00
expect(dependencyMock.disconnect.calledOnce).toBe(true);
}));
describe("updateHash", () => {
let hash;
before(() => {
hash = {
update: sandbox.spy()
};
2017-02-03 21:37:56 +08:00
DependenciesBlockVariableInstance.updateHash(hash);
});
it("should update hash dependencies with name", () =>
2018-01-24 20:17:21 +08:00
expect(hash.update.calledWith("dependencies-name")).toBe(true);
it("should update hash dependencies with expression", () =>
2018-01-24 20:17:21 +08:00
expect(hash.update.calledWith("expression")).toBe(true);
it("should update hash inside dependencies", () =>
2018-01-24 20:17:21 +08:00
expect(dependencyMock.updateHash.calledOnce).toBe(true);
});
describe("expressionSource", () => {
2018-02-25 09:00:20 +08:00
let dependencyTemplates, applyMock;
2018-02-25 09:00:20 +08:00
before(() => (applyMock = sandbox.spy()));
2018-02-26 10:23:03 +08:00
it("applies information inside dependency templates", () => {
dependencyTemplates = {
get: function() {
return {
apply: applyMock
};
}
};
2017-02-03 21:37:56 +08:00
DependenciesBlockVariableInstance.expressionSource(
2018-02-25 18:46:17 +08:00
dependencyTemplates,
{},
{}
);
2018-01-24 20:17:21 +08:00
expect(applyMock.calledOnce).toBe(true);
});
2018-02-26 10:23:03 +08:00
it("applies information inside dependency templates", () => {
dependencyTemplates = {
get: function() {
return false;
}
};
2018-01-24 20:17:21 +08:00
expect(() => {
2017-02-03 21:37:56 +08:00
DependenciesBlockVariableInstance.expressionSource(
2018-02-25 18:46:17 +08:00
dependencyTemplates,
{},
{}
);
2018-01-24 20:17:21 +08:00
}).toThrow("No template for dependency: DependencyMock");
});
});
});