From cd168a303de73a1e8505ac52fbda5a53c1fab2bf Mon Sep 17 00:00:00 2001 From: yuto-yuto Date: Sat, 10 Jul 2021 10:33:07 +0200 Subject: [PATCH] Add tests --- lib/PasswordGeneratorNode.ts | 3 +- test/PasswordGeneratorNode_spec.ts | 63 ++++++++++++++++++++++-------- test/PasswordGenerator_spec.ts | 2 +- 3 files changed, 48 insertions(+), 20 deletions(-) diff --git a/lib/PasswordGeneratorNode.ts b/lib/PasswordGeneratorNode.ts index 5834e1a..cc81d6b 100644 --- a/lib/PasswordGeneratorNode.ts +++ b/lib/PasswordGeneratorNode.ts @@ -16,9 +16,8 @@ export = (RED: nodered.NodeAPI): void => { this.on("input", async (msg: any, send, done) => { const password = await generatePassword(config.size); - const valueSetPath = msg.to || config.setTo || "payload"; - yutolity.setValue(msg, valueSetPath, password); + msg = yutolity.setValue(msg, valueSetPath, password); send(msg); done(); }); diff --git a/test/PasswordGeneratorNode_spec.ts b/test/PasswordGeneratorNode_spec.ts index d51c120..3492ee6 100644 --- a/test/PasswordGeneratorNode_spec.ts +++ b/test/PasswordGeneratorNode_spec.ts @@ -20,30 +20,33 @@ describe("PasswordGeneratorNode", () => { const nodeId = "node-id"; const outNodeId = "out-node-id"; - const flows = [ - { - id: nodeId, - type: "password-generator", - size: 10, - setTo: "payload.value", - name: "generator-name", - wires: [[outNodeId]] - }, - { - id: outNodeId, - type: "helper", - } - ]; - + function createFlow(args?: { size?: number, setTo?: string }) { + return [ + { + id: nodeId, + type: "password-generator", + size: args?.size || 10, + setTo: args?.setTo, + name: "generator-name", + wires: [[outNodeId]] + }, + { + id: outNodeId, + type: "helper", + } + ]; + } it("should be loaded", (done) => { - helper.load([valueChangeNode], flows, () => { + const flow = createFlow(); + helper.load([valueChangeNode], flow, () => { 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) => { - helper.load([valueChangeNode], flows, () => { + 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) => { @@ -54,4 +57,30 @@ describe("PasswordGeneratorNode", () => { }).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); + }); }); \ No newline at end of file diff --git a/test/PasswordGenerator_spec.ts b/test/PasswordGenerator_spec.ts index 1241f3e..6eccc7c 100644 --- a/test/PasswordGenerator_spec.ts +++ b/test/PasswordGenerator_spec.ts @@ -2,7 +2,7 @@ import "mocha"; import { expect } from "chai"; import { generatePassword } from "../lib/PasswordGenerator"; -describe.only("PasswordGenerator", () => { +describe("PasswordGenerator", () => { it("should return true for the first time", async () => { const result = await generatePassword(10); console.log(`before: "${result}"`)