1
0
Fork 0
freesewing/tests/hooks.test.js

115 lines
2.2 KiB
JavaScript
Raw Normal View History

2018-08-12 17:02:49 +02:00
let expect = require("chai").expect;
let freesewing = require("./dist/index.js");
2018-08-12 12:37:30 +02:00
2018-08-12 17:02:49 +02:00
let pattern = new freesewing.Pattern();
let h = pattern.hooks;
2018-08-12 12:37:30 +02:00
it("Should contain all hooks", () => {
let test = [
"preDraft",
"postDraft",
"preSample",
"postSample",
"preRender",
"postRender",
"insertText",
"debug"
];
expect(h.all).to.eql(test);
});
it("Should contain no hooks upon creation", () => {
let test = {};
expect(h._hooks).to.eql(test);
});
it("Should return hooks correctly", () => {
h._hooks = {
hookA: "testA",
hookB: "testB"
};
expect(h.list("hookA")).to.equal("testA");
expect(h.list("hookB")).to.equal("testB");
expect(h.list("hookC")).to.equal(false);
});
it("Should not attach a non-existing hook", () => {
let obj = {};
expect(h.attach("no-such-hook", obj)).to.equal(undefined);
});
2018-08-12 17:02:49 +02:00
let count = 0;
let pre = function() {
2018-08-12 12:37:30 +02:00
count += 5;
};
it("Should attach the preDraft & postDraft hooks", () => {
count = 0;
let obj = {
draft: function() {},
pre: pre
2018-08-12 12:37:30 +02:00
};
h._hooks = {
preDraft: [
function(next) {
next();
}
],
postDraft: [
function(next) {
next();
}
]
};
expect(h.attach("preDraft", obj)).to.equal(undefined);
obj.draft();
expect(count).to.equal(5);
expect(h.attach("postDraft", obj)).to.equal(undefined);
obj.draft();
expect(count).to.equal(10);
2018-08-12 12:37:30 +02:00
});
it("Should attach the preSample & postSample hooks", () => {
count = 0;
let obj = {
sampleOption: function() {},
pre: pre
2018-08-12 12:37:30 +02:00
};
h._hooks = {
preSample: [
function(next) {
next();
}
],
postSample: [
function(next) {
next();
}
]
};
expect(h.attach("preSample", obj)).to.equal(undefined);
obj.sampleOption();
expect(count).to.equal(5);
2018-08-12 12:37:30 +02:00
expect(h.attach("postSample", obj)).to.equal(undefined);
obj.sampleOption();
expect(count).to.equal(10);
2018-08-12 12:37:30 +02:00
});
it("Should attach the debug hook", () => {
count = 0;
let obj = {
debug: function() {},
pre: pre
2018-08-12 12:37:30 +02:00
};
h._hooks = {
debug: [
function(next) {
next();
}
]
};
expect(h.attach("debug", obj)).to.equal(undefined);
obj.debug();
expect(count).to.equal(5);
});