Add tests

This commit is contained in:
yuto-yuto 2021-07-10 10:33:07 +02:00
parent 9211a8308a
commit cd168a303d
3 changed files with 48 additions and 20 deletions

View File

@ -16,9 +16,8 @@ export = (RED: nodered.NodeAPI): void => {
this.on("input", async (msg: any, send, done) => { this.on("input", async (msg: any, send, done) => {
const password = await generatePassword(config.size); const password = await generatePassword(config.size);
const valueSetPath = msg.to || config.setTo || "payload"; const valueSetPath = msg.to || config.setTo || "payload";
yutolity.setValue(msg, valueSetPath, password); msg = yutolity.setValue(msg, valueSetPath, password);
send(msg); send(msg);
done(); done();
}); });

View File

@ -20,30 +20,33 @@ describe("PasswordGeneratorNode", () => {
const nodeId = "node-id"; const nodeId = "node-id";
const outNodeId = "out-node-id"; const outNodeId = "out-node-id";
const flows = [ function createFlow(args?: { size?: number, setTo?: string }) {
{ return [
id: nodeId, {
type: "password-generator", id: nodeId,
size: 10, type: "password-generator",
setTo: "payload.value", size: args?.size || 10,
name: "generator-name", setTo: args?.setTo,
wires: [[outNodeId]] name: "generator-name",
}, wires: [[outNodeId]]
{ },
id: outNodeId, {
type: "helper", id: outNodeId,
} type: "helper",
]; }
];
}
it("should be loaded", (done) => { it("should be loaded", (done) => {
helper.load([valueChangeNode], flows, () => { const flow = createFlow();
helper.load([valueChangeNode], flow, () => {
const node = helper.getNode(nodeId); const node = helper.getNode(nodeId);
expect(node.name).to.equal("generator-name"); expect(node.name).to.equal("generator-name");
done(); done();
}).catch(done); }).catch(done);
}); });
it("should set value to the property specified in setTo", (done) => { it("should set value to the property specified in setTo", (done) => {
helper.load([valueChangeNode], flows, () => { const flow = createFlow({ setTo: "payload.value" });
helper.load([valueChangeNode], flow, () => {
const node = helper.getNode(nodeId); const node = helper.getNode(nodeId);
const outNode = helper.getNode(outNodeId); const outNode = helper.getNode(outNodeId);
outNode.on("input", (msg: any) => { outNode.on("input", (msg: any) => {
@ -54,4 +57,30 @@ describe("PasswordGeneratorNode", () => {
}).catch(done); }).catch(done);
}); });
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);
});
}); });

View File

@ -2,7 +2,7 @@ import "mocha";
import { expect } from "chai"; import { expect } from "chai";
import { generatePassword } from "../lib/PasswordGenerator"; import { generatePassword } from "../lib/PasswordGenerator";
describe.only("PasswordGenerator", () => { describe("PasswordGenerator", () => {
it("should return true for the first time", async () => { it("should return true for the first time", async () => {
const result = await generatePassword(10); const result = await generatePassword(10);
console.log(`before: "${result}"`) console.log(`before: "${result}"`)