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) => {
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();
});

View File

@ -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);
});
});

View File

@ -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}"`)