webpack/test/DependenciesBlockVariable.u...

85 lines
2.1 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-01-24 20:17:21 +08:00
const sandbox = sinon.sandbox.create();
const dependencyMock = {
constructor: {
name: "DependencyMock"
},
disconnect: sandbox.spy(),
updateHash: sandbox.spy()
};
const DependenciesBlockVariableInstance = new DependenciesBlockVariable("dependencies-name", "expression", [dependencyMock]);
2018-01-24 20:17:21 +08:00
afterEach(() => {
sandbox.restore();
});
2018-01-24 20:17:21 +08:00
describe("hasDependencies", () => {
it("returns `true` if has dependencies", () => {
expect(DependenciesBlockVariableInstance.hasDependencies()).toBe(true);
});
});
2018-01-24 20:17:21 +08:00
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", () => {
2018-01-24 20:17:21 +08:00
const hash = {
update: sandbox.spy()
};
2018-01-24 20:17:21 +08:00
DependenciesBlockVariableInstance.updateHash(hash);
2018-01-24 20:17:21 +08:00
it("should update hash dependencies with name", () => {
expect(hash.update.calledWith("dependencies-name")).toBe(true);
});
2018-01-24 20:17:21 +08:00
it("should update hash dependencies with expression", () => {
expect(hash.update.calledWith("expression")).toBe(true);
});
it("should update hash inside dependencies", () => {
expect(dependencyMock.updateHash.calledOnce).toBe(true);
});
});
describe("expressionSource", () => {
2018-01-24 20:17:21 +08:00
const applyMock = sandbox.spy();
it("aplies information inside dependency templates", () => {
2018-01-24 20:17:21 +08:00
const dependencyTemplates = {
get() {
return {
apply: applyMock
};
}
};
2017-02-03 21:37:56 +08:00
DependenciesBlockVariableInstance.expressionSource(
dependencyTemplates, {}, {}
);
2018-01-24 20:17:21 +08:00
expect(applyMock.calledOnce).toBe(true);
});
it("aplies information inside dependency templates", () => {
2018-01-24 20:17:21 +08:00
const dependencyTemplates = {
get() {
return false;
}
};
2018-01-24 20:17:21 +08:00
expect(() => {
2017-02-03 21:37:56 +08:00
DependenciesBlockVariableInstance.expressionSource(
dependencyTemplates, {}, {}
);
2018-01-24 20:17:21 +08:00
}).toThrow("No template for dependency: DependencyMock");
});
});
});