2020-07-30 18:52:11 +02:00
|
|
|
const should = require("should");
|
|
|
|
const NR_TEST_UTILS = require("nr-test-utils");
|
|
|
|
|
2021-04-12 21:30:31 +02:00
|
|
|
const hooks = NR_TEST_UTILS.require("@node-red/util/lib/hooks");
|
2020-07-30 18:52:11 +02:00
|
|
|
|
2021-04-12 21:30:31 +02:00
|
|
|
describe("util/hooks", function() {
|
2020-07-30 18:52:11 +02:00
|
|
|
afterEach(function() {
|
|
|
|
hooks.clear();
|
|
|
|
})
|
|
|
|
it("allows a hook to be registered", function(done) {
|
|
|
|
let calledWith = null;
|
2020-09-29 17:28:52 +02:00
|
|
|
hooks.has("onSend").should.be.false();
|
|
|
|
hooks.add("onSend", function(payload) { calledWith = payload } )
|
|
|
|
hooks.has("onSend").should.be.true();
|
2020-07-30 18:52:11 +02:00
|
|
|
let data = { a: 1 };
|
2020-09-29 17:28:52 +02:00
|
|
|
hooks.trigger("onSend",data,err => {
|
2020-07-30 18:52:11 +02:00
|
|
|
calledWith.should.equal(data);
|
|
|
|
done(err);
|
|
|
|
})
|
|
|
|
})
|
2020-09-29 17:28:52 +02:00
|
|
|
it("rejects invalid hook id", function(done) {
|
|
|
|
try {
|
|
|
|
hooks.add("foo", function(payload) {})
|
|
|
|
done(new Error("Invalid hook accepted"))
|
|
|
|
} catch(err) {
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
})
|
2020-07-30 18:52:11 +02:00
|
|
|
it("calls hooks in the order they were registered", function(done) {
|
2020-09-29 17:28:52 +02:00
|
|
|
hooks.add("onSend", function(payload) { payload.order.push("A") } )
|
|
|
|
hooks.add("onSend", function(payload) { payload.order.push("B") } )
|
2020-07-30 18:52:11 +02:00
|
|
|
let data = { order:[] };
|
2020-09-29 17:28:52 +02:00
|
|
|
hooks.trigger("onSend",data,err => {
|
2020-07-30 18:52:11 +02:00
|
|
|
data.order.should.eql(["A","B"])
|
|
|
|
done(err);
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it("does not allow multiple hooks with same id.label", function() {
|
2020-09-29 17:28:52 +02:00
|
|
|
hooks.has("onSend.one").should.be.false();
|
|
|
|
hooks.has("onSend").should.be.false();
|
|
|
|
hooks.add("onSend.one", function(payload) { payload.order.push("A") } );
|
|
|
|
hooks.has("onSend.one").should.be.true();
|
|
|
|
hooks.has("onSend").should.be.true();
|
2020-07-30 18:52:11 +02:00
|
|
|
(function() {
|
2020-09-29 17:28:52 +02:00
|
|
|
hooks.add("onSend.one", function(payload) { payload.order.push("B") } )
|
2020-07-30 18:52:11 +02:00
|
|
|
}).should.throw();
|
|
|
|
})
|
|
|
|
|
|
|
|
it("removes labelled hook", function(done) {
|
2020-09-29 17:28:52 +02:00
|
|
|
hooks.has("onSend.A").should.be.false();
|
|
|
|
hooks.has("onSend.B").should.be.false();
|
|
|
|
hooks.has("onSend").should.be.false();
|
|
|
|
|
|
|
|
hooks.add("onSend.A", function(payload) { payload.order.push("A") } )
|
|
|
|
|
|
|
|
hooks.has("onSend.A").should.be.true();
|
|
|
|
hooks.has("onSend.B").should.be.false();
|
|
|
|
hooks.has("onSend").should.be.true();
|
|
|
|
|
|
|
|
hooks.add("onSend.B", function(payload) { payload.order.push("B") } )
|
|
|
|
|
|
|
|
hooks.has("onSend.A").should.be.true();
|
|
|
|
hooks.has("onSend.B").should.be.true();
|
|
|
|
hooks.has("onSend").should.be.true();
|
|
|
|
|
|
|
|
hooks.remove("onSend.A");
|
|
|
|
|
|
|
|
hooks.has("onSend.A").should.be.false();
|
|
|
|
hooks.has("onSend.B").should.be.true();
|
|
|
|
hooks.has("onSend").should.be.true();
|
2020-07-30 18:52:11 +02:00
|
|
|
|
|
|
|
|
|
|
|
let data = { order:[] };
|
2020-09-29 17:28:52 +02:00
|
|
|
hooks.trigger("onSend",data,err => {
|
2020-07-30 18:52:11 +02:00
|
|
|
try {
|
|
|
|
data.order.should.eql(["B"])
|
|
|
|
|
2020-09-29 17:28:52 +02:00
|
|
|
hooks.remove("onSend.B");
|
2020-07-30 18:52:11 +02:00
|
|
|
|
2020-09-29 17:28:52 +02:00
|
|
|
hooks.has("onSend.A").should.be.false();
|
|
|
|
hooks.has("onSend.B").should.be.false();
|
|
|
|
hooks.has("onSend").should.be.false();
|
2021-04-12 21:30:31 +02:00
|
|
|
|
2020-07-30 18:52:11 +02:00
|
|
|
done(err);
|
|
|
|
} catch(err2) {
|
|
|
|
done(err2);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it("cannot remove unlabelled hook", function() {
|
2020-09-29 17:28:52 +02:00
|
|
|
hooks.add("onSend", function(payload) { payload.order.push("A") } );
|
2020-07-30 18:52:11 +02:00
|
|
|
(function() {
|
2020-09-29 17:28:52 +02:00
|
|
|
hooks.remove("onSend")
|
2020-07-30 18:52:11 +02:00
|
|
|
}).should.throw();
|
|
|
|
})
|
|
|
|
it("removes all hooks with same label", function(done) {
|
2020-09-29 17:28:52 +02:00
|
|
|
hooks.add("onSend.A", function(payload) { payload.order.push("A") } )
|
|
|
|
hooks.add("onSend.B", function(payload) { payload.order.push("B") } )
|
|
|
|
hooks.add("preRoute.A", function(payload) { payload.order.push("C") } )
|
|
|
|
hooks.add("preRoute.B", function(payload) { payload.order.push("D") } )
|
2020-07-30 18:52:11 +02:00
|
|
|
|
|
|
|
let data = { order:[] };
|
2020-09-29 17:28:52 +02:00
|
|
|
hooks.trigger("onSend",data,err => {
|
2020-07-30 18:52:11 +02:00
|
|
|
data.order.should.eql(["A","B"])
|
2020-09-29 17:28:52 +02:00
|
|
|
hooks.trigger("preRoute", data, err => {
|
2020-07-30 18:52:11 +02:00
|
|
|
data.order.should.eql(["A","B","C","D"])
|
|
|
|
|
|
|
|
data.order = [];
|
|
|
|
|
|
|
|
hooks.remove("*.A");
|
|
|
|
|
2020-09-29 17:28:52 +02:00
|
|
|
hooks.trigger("onSend",data,err => {
|
2020-07-30 18:52:11 +02:00
|
|
|
data.order.should.eql(["B"])
|
2020-09-29 17:28:52 +02:00
|
|
|
hooks.trigger("preRoute", data, err => {
|
2020-07-30 18:52:11 +02:00
|
|
|
data.order.should.eql(["B","D"])
|
|
|
|
})
|
|
|
|
done(err);
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
it("halts execution on return false", function(done) {
|
2020-09-29 17:28:52 +02:00
|
|
|
hooks.add("onSend.A", function(payload) { payload.order.push("A"); return false } )
|
|
|
|
hooks.add("onSend.B", function(payload) { payload.order.push("B") } )
|
2020-07-30 18:52:11 +02:00
|
|
|
|
|
|
|
let data = { order:[] };
|
2020-09-29 17:28:52 +02:00
|
|
|
hooks.trigger("onSend",data,err => {
|
2020-07-30 18:52:11 +02:00
|
|
|
data.order.should.eql(["A"])
|
|
|
|
err.should.be.false();
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
})
|
|
|
|
it("halts execution on thrown error", function(done) {
|
2020-09-29 17:28:52 +02:00
|
|
|
hooks.add("onSend.A", function(payload) { payload.order.push("A"); throw new Error("error") } )
|
|
|
|
hooks.add("onSend.B", function(payload) { payload.order.push("B") } )
|
2020-07-30 18:52:11 +02:00
|
|
|
|
|
|
|
let data = { order:[] };
|
2020-09-29 17:28:52 +02:00
|
|
|
hooks.trigger("onSend",data,err => {
|
2020-07-30 18:52:11 +02:00
|
|
|
data.order.should.eql(["A"])
|
|
|
|
should.exist(err);
|
|
|
|
err.should.not.be.false()
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it("handler can use callback function", function(done) {
|
2020-09-29 17:28:52 +02:00
|
|
|
hooks.add("onSend.A", function(payload, done) {
|
2020-07-30 18:52:11 +02:00
|
|
|
setTimeout(function() {
|
|
|
|
payload.order.push("A")
|
|
|
|
done()
|
|
|
|
},30)
|
|
|
|
})
|
2020-09-29 17:28:52 +02:00
|
|
|
hooks.add("onSend.B", function(payload) { payload.order.push("B") } )
|
2020-07-30 18:52:11 +02:00
|
|
|
|
|
|
|
let data = { order:[] };
|
2020-09-29 17:28:52 +02:00
|
|
|
hooks.trigger("onSend",data,err => {
|
2020-07-30 18:52:11 +02:00
|
|
|
data.order.should.eql(["A","B"])
|
|
|
|
done(err);
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it("handler can use callback function - halt execution", function(done) {
|
2020-09-29 17:28:52 +02:00
|
|
|
hooks.add("onSend.A", function(payload, done) {
|
2020-07-30 18:52:11 +02:00
|
|
|
setTimeout(function() {
|
|
|
|
payload.order.push("A")
|
|
|
|
done(false)
|
|
|
|
},30)
|
|
|
|
})
|
2020-09-29 17:28:52 +02:00
|
|
|
hooks.add("onSend.B", function(payload) { payload.order.push("B") } )
|
2020-07-30 18:52:11 +02:00
|
|
|
|
|
|
|
let data = { order:[] };
|
2020-09-29 17:28:52 +02:00
|
|
|
hooks.trigger("onSend",data,err => {
|
2020-07-30 18:52:11 +02:00
|
|
|
data.order.should.eql(["A"])
|
|
|
|
err.should.be.false()
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
})
|
|
|
|
it("handler can use callback function - halt on error", function(done) {
|
2020-09-29 17:28:52 +02:00
|
|
|
hooks.add("onSend.A", function(payload, done) {
|
2020-07-30 18:52:11 +02:00
|
|
|
setTimeout(function() {
|
|
|
|
done(new Error("test error"))
|
|
|
|
},30)
|
|
|
|
})
|
2020-09-29 17:28:52 +02:00
|
|
|
hooks.add("onSend.B", function(payload) { payload.order.push("B") } )
|
2020-07-30 18:52:11 +02:00
|
|
|
|
|
|
|
let data = { order:[] };
|
2020-09-29 17:28:52 +02:00
|
|
|
hooks.trigger("onSend",data,err => {
|
2020-07-30 18:52:11 +02:00
|
|
|
data.order.should.eql([])
|
|
|
|
should.exist(err);
|
|
|
|
err.should.not.be.false()
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it("handler be an async function", function(done) {
|
2020-09-29 17:28:52 +02:00
|
|
|
hooks.add("onSend.A", async function(payload) {
|
2020-07-30 18:52:11 +02:00
|
|
|
return new Promise(resolve => {
|
|
|
|
setTimeout(function() {
|
|
|
|
payload.order.push("A")
|
|
|
|
resolve()
|
|
|
|
},30)
|
|
|
|
});
|
|
|
|
})
|
2020-09-29 17:28:52 +02:00
|
|
|
hooks.add("onSend.B", function(payload) { payload.order.push("B") } )
|
2020-07-30 18:52:11 +02:00
|
|
|
|
|
|
|
let data = { order:[] };
|
2020-09-29 17:28:52 +02:00
|
|
|
hooks.trigger("onSend",data,err => {
|
2020-07-30 18:52:11 +02:00
|
|
|
data.order.should.eql(["A","B"])
|
|
|
|
done(err);
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it("handler be an async function - halt execution", function(done) {
|
2020-09-29 17:28:52 +02:00
|
|
|
hooks.add("onSend.A", async function(payload) {
|
2020-07-30 18:52:11 +02:00
|
|
|
return new Promise(resolve => {
|
|
|
|
setTimeout(function() {
|
|
|
|
payload.order.push("A")
|
|
|
|
resolve(false)
|
|
|
|
},30)
|
|
|
|
});
|
|
|
|
})
|
2020-09-29 17:28:52 +02:00
|
|
|
hooks.add("onSend.B", function(payload) { payload.order.push("B") } )
|
2020-07-30 18:52:11 +02:00
|
|
|
|
|
|
|
let data = { order:[] };
|
2020-09-29 17:28:52 +02:00
|
|
|
hooks.trigger("onSend",data,err => {
|
2020-07-30 18:52:11 +02:00
|
|
|
data.order.should.eql(["A"])
|
|
|
|
done(err);
|
|
|
|
})
|
|
|
|
})
|
|
|
|
it("handler be an async function - halt on error", function(done) {
|
2020-09-29 17:28:52 +02:00
|
|
|
hooks.add("onSend.A", async function(payload) {
|
2020-07-30 18:52:11 +02:00
|
|
|
return new Promise((resolve,reject) => {
|
|
|
|
setTimeout(function() {
|
|
|
|
reject(new Error("test error"))
|
|
|
|
},30)
|
|
|
|
});
|
|
|
|
})
|
2020-09-29 17:28:52 +02:00
|
|
|
hooks.add("onSend.B", function(payload) { payload.order.push("B") } )
|
2020-07-30 18:52:11 +02:00
|
|
|
|
|
|
|
let data = { order:[] };
|
2020-09-29 17:28:52 +02:00
|
|
|
hooks.trigger("onSend",data,err => {
|
2020-07-30 18:52:11 +02:00
|
|
|
data.order.should.eql([])
|
|
|
|
should.exist(err);
|
|
|
|
err.should.not.be.false()
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
})
|
2021-04-15 16:11:27 +02:00
|
|
|
|
|
|
|
|
|
|
|
it("handler can use callback function - promise API", function(done) {
|
|
|
|
hooks.add("onSend.A", function(payload, done) {
|
|
|
|
setTimeout(function() {
|
|
|
|
payload.order.push("A")
|
|
|
|
done()
|
|
|
|
},30)
|
|
|
|
})
|
|
|
|
hooks.add("onSend.B", function(payload) { payload.order.push("B") } )
|
|
|
|
|
|
|
|
let data = { order:[] };
|
|
|
|
hooks.trigger("onSend",data).then(() => {
|
|
|
|
data.order.should.eql(["A","B"])
|
|
|
|
done()
|
|
|
|
}).catch(done)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("handler can halt execution - promise API", function(done) {
|
|
|
|
hooks.add("onSend.A", function(payload, done) {
|
|
|
|
setTimeout(function() {
|
|
|
|
payload.order.push("A")
|
|
|
|
done(false)
|
|
|
|
},30)
|
|
|
|
})
|
|
|
|
hooks.add("onSend.B", function(payload) { payload.order.push("B") } )
|
|
|
|
|
|
|
|
let data = { order:[] };
|
|
|
|
hooks.trigger("onSend",data).then(() => {
|
|
|
|
data.order.should.eql(["A"])
|
|
|
|
done()
|
|
|
|
}).catch(done)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("handler can halt execution on error - promise API", function(done) {
|
|
|
|
hooks.add("onSend.A", function(payload, done) {
|
|
|
|
throw new Error("error");
|
|
|
|
})
|
|
|
|
hooks.add("onSend.B", function(payload) { payload.order.push("B") } )
|
|
|
|
|
|
|
|
let data = { order:[] };
|
|
|
|
hooks.trigger("onSend",data).then(() => {
|
|
|
|
done("hooks.trigger resolved unexpectedly")
|
|
|
|
}).catch(err => {
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
})
|
2020-07-30 18:52:11 +02:00
|
|
|
});
|