node-red-contrib-password-g.../test/PasswordGeneratorNode_spec.ts

85 lines
2.8 KiB
TypeScript
Raw Normal View History

2021-07-09 03:49:18 +02:00
import "mocha";
import { expect } from "chai";
import helper = require("node-red-node-test-helper");
import valueChangeNode = require("../lib/PasswordGeneratorNode");
describe("PasswordGeneratorNode", () => {
before(() => {
helper.init(require.resolve('node-red'));
});
beforeEach((done) => {
helper.startServer(done);
});
afterEach((done) => {
helper.unload().then(() => helper.stopServer(done));
});
const nodeId = "node-id";
const outNodeId = "out-node-id";
2021-07-11 06:16:21 +02:00
function createFlow(args?: { length?: number, setTo?: string }) {
2021-07-10 10:33:07 +02:00
return [
{
id: nodeId,
type: "password-generator",
2021-07-11 06:16:21 +02:00
length: args?.length || 10,
2021-07-10 10:33:07 +02:00
setTo: args?.setTo,
name: "generator-name",
wires: [[outNodeId]]
},
{
id: outNodeId,
type: "helper",
}
];
}
2021-07-09 03:49:18 +02:00
it("should be loaded", (done) => {
2021-07-10 10:33:07 +02:00
const flow = createFlow();
helper.load([valueChangeNode], flow, () => {
2021-07-09 03:49:18 +02:00
const node = helper.getNode(nodeId);
expect(node.name).to.equal("generator-name");
done();
}).catch(done);
});
it("should set value to the property specified in setTo", (done) => {
2021-07-10 10:33:07 +02:00
const flow = createFlow({ setTo: "payload.value" });
helper.load([valueChangeNode], flow, () => {
2021-07-09 03:49:18 +02:00
const node = helper.getNode(nodeId);
const outNode = helper.getNode(outNodeId);
outNode.on("input", (msg: any) => {
expect(msg.payload.value).not.to.be.undefined;
done();
});
node.receive({ payload: 1 });
}).catch(done);
});
2021-07-10 10:33:07 +02:00
it("should set value to msg.payload when neither setTo nor msg.to is set ", (done) => {
const flow = createFlow();
helper.load([valueChangeNode], flow, () => {
const node = helper.getNode(nodeId);
const outNode = helper.getNode(outNodeId);
outNode.on("input", (msg: any) => {
expect(msg.payload).not.to.be.undefined;
done();
});
node.receive({ payload: 1 });
}).catch(done);
});
it("should use msg.to if it is set even if setTo is set", (done) => {
const flow = createFlow({ setTo: "payload.value" });
helper.load([valueChangeNode], flow, () => {
const node = helper.getNode(nodeId);
const outNode = helper.getNode(outNodeId);
outNode.on("input", (msg: any) => {
expect(msg.hoge.hoge).not.to.be.undefined;
done();
});
node.receive({ payload: 1, to: "hoge.hoge" } as any);
}).catch(done);
});
2021-07-09 03:49:18 +02:00
});