Skip to content

Commit 2856001

Browse files
committed
Templates: add tests, import fixes
1 parent 90aa8a6 commit 2856001

11 files changed

Lines changed: 152 additions & 18 deletions

File tree

‎utils/add-node.cjs‎

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const mustacheData = {
4141
};
4242

4343
// node files generator
44-
async function generateFiles(fromDir, toDir) {
44+
async function generateFiles(fromDir, toDir, toDirTests) {
4545
// always called for a nonexistent toDir
4646
await mkdir(toDir);
4747
console.log(green(`Created directory: ${bold(toDir)}`));
@@ -57,7 +57,7 @@ async function generateFiles(fromDir, toDir) {
5757

5858
if (fromDirent.isDirectory()) {
5959
// directory -> recursive call
60-
await generateFiles(fromFilePath, toFilePath);
60+
await generateFiles(fromFilePath, toFilePath, toDirTests);
6161
} else {
6262
// file -> generate
6363
const ext = path.extname(toFilePath);
@@ -67,10 +67,18 @@ async function generateFiles(fromDir, toDir) {
6767
console.log(green(`Copied file: ${bold(toFilePath)}`));
6868
} else {
6969
// generate from mustache template
70-
toFilePath = path.join(
71-
path.dirname(toFilePath),
72-
path.basename(toFilePath, ext)
73-
);
70+
const basename = path.basename(toFilePath, ext);
71+
if (basename.endsWith('.test.ts')) {
72+
toFilePath = path.join(
73+
toDirTests,
74+
basename
75+
);
76+
} else {
77+
toFilePath = path.join(
78+
path.dirname(toFilePath),
79+
basename
80+
);
81+
}
7482
const tpl = await readFile(fromFilePath, "utf8");
7583
const renderedStr = mustache.render(tpl, mustacheData, {}, [
7684
"<%",
@@ -104,6 +112,12 @@ async function main() {
104112
"nodes",
105113
nodeTypeInKebabCase
106114
);
115+
const testsDir = path.join(
116+
__dirname,
117+
"..",
118+
"src",
119+
"__tests__"
120+
);
107121

108122
// check if paths ok
109123
if (!fs.existsSync(templateDir)) {
@@ -114,7 +128,9 @@ async function main() {
114128
console.log(red(`Node ${bold(nodeTypeInKebabCase)} already exists`));
115129
return;
116130
}
117-
131+
if (!fs.existsSync(testsDir)) {
132+
await mkdir(testsDir);
133+
}
118134
// we can do that now
119135
console.log(
120136
green(
@@ -125,7 +141,7 @@ async function main() {
125141
);
126142

127143
try {
128-
await generateFiles(templateDir, newNodeDir);
144+
await generateFiles(templateDir, newNodeDir, testsDir);
129145
await addNodeToPackageJson();
130146
} catch (e) {
131147
console.log(red(`Error: ${bold(e)}`));

‎utils/templates/blank-0/modules/types.ts.mustache‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Node, NodeDef } from "node-red";
2-
import { <%NodeTypePascalCase%>Options } from "../shared/types";
2+
import { <%NodeTypePascalCase%>Options } from "../shared/types.js";
33

44
export interface <%NodeTypePascalCase%>NodeDef extends NodeDef, <%NodeTypePascalCase%>Options {}
55

‎utils/templates/blank-0/node-type.html/index.ts.mustache‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { EditorRED } from "node-red";
2-
import { <%NodeTypePascalCase%>EditorNodeProperties } from "./modules/types";
2+
import { <%NodeTypePascalCase%>EditorNodeProperties } from "./modules/types.js";
33

44
declare const RED: EditorRED;
55

‎utils/templates/blank-0/node-type.html/modules/types.ts.mustache‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { EditorNodeProperties } from "node-red";
2-
import { <%NodeTypePascalCase%>Options } from "../../shared/types";
2+
import { <%NodeTypePascalCase%>Options } from "../../shared/types.js";
33

44
export interface <%NodeTypePascalCase%>EditorNodeProperties
55
extends EditorNodeProperties,
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import testHelper, { TestFlowsItem } from 'node-red-node-test-helper';
2+
import <%NodeTypeCamelCase%>Node from '../nodes/<%NodeTypeKebabCase%>/<%NodeTypeKebabCase%>.js';
3+
import { <%NodeTypePascalCase%>NodeDef } from '../nodes/<%NodeTypeKebabCase%>/modules/types.js';
4+
5+
type FlowsItem = TestFlowsItem<<%NodeTypePascalCase%>NodeDef>;
6+
type Flows = Array<FlowsItem>;
7+
8+
describe('<%NodeTypeKebabCase%> node', () => {
9+
beforeEach((done) => {
10+
testHelper.startServer(done);
11+
});
12+
13+
afterEach((done) => {
14+
testHelper.unload().then(() => {
15+
testHelper.stopServer(done);
16+
});
17+
});
18+
19+
it('should be loaded', (done) => {
20+
const flows: Flows = [
21+
{ id: 'n1', type: '<%NodeTypeKebabCase%>', name: '<%NodeTypeKebabCase%>' },
22+
];
23+
testHelper.load(<%NodeTypeCamelCase%>Node, flows, () => {
24+
const n1 = testHelper.getNode('n1');
25+
expect(n1).toBeTruthy();
26+
expect(n1.name).toEqual('<%NodeTypeKebabCase%>');
27+
done();
28+
});
29+
});
30+
31+
/*
32+
describe('in X mode', () => {
33+
let flows: Flows;
34+
beforeEach(() => {
35+
flows = [
36+
{
37+
id: 'n1',
38+
type: '<%NodeTypeKebabCase%>',
39+
name: '<%NodeTypeKebabCase%>',
40+
mode: 'X', // some node option
41+
wires: [['n2']],
42+
},
43+
{ id: 'n2', type: 'helper' },
44+
];
45+
});
46+
it("should ...", (done) => {
47+
testHelper.load(<%NodeTypeCamelCase%>Node, flows, () => {
48+
const n2 = testHelper.getNode('n2');
49+
const n1 = testHelper.getNode('n1');
50+
n2.on('input', (msg: unknown) => {
51+
expect(msg.payload).toBe('test');
52+
done();
53+
});
54+
n1.receive({ payload: 'test' });
55+
});
56+
});
57+
});
58+
*/
59+
});

‎utils/templates/blank-0/node-type.ts.mustache‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { NodeInitializer } from "node-red";
2-
import { <%NodeTypePascalCase%>Node, <%NodeTypePascalCase%>NodeDef } from "./modules/types";
2+
import { <%NodeTypePascalCase%>Node, <%NodeTypePascalCase%>NodeDef } from "./modules/types.js";
33

44
const nodeInit: NodeInitializer = (RED): void => {
55
function <%NodeTypePascalCase%>NodeConstructor(
@@ -23,4 +23,4 @@ const nodeInit: NodeInitializer = (RED): void => {
2323
RED.nodes.registerType("<%NodeTypeKebabCase%>", <%NodeTypePascalCase%>NodeConstructor);
2424
};
2525

26-
export = nodeInit;
26+
export default nodeInit;

‎utils/templates/blank/modules/types.ts.mustache‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Node, NodeDef } from "node-red";
2-
import { <%NodeTypePascalCase%>Options } from "../shared/types";
2+
import { <%NodeTypePascalCase%>Options } from "../shared/types.js";
33

44
export interface <%NodeTypePascalCase%>NodeDef extends NodeDef, <%NodeTypePascalCase%>Options {}
55

‎utils/templates/blank/node-type.html/index.ts.mustache‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { EditorRED } from "node-red";
2-
import { <%NodeTypePascalCase%>EditorNodeProperties } from "./modules/types";
2+
import { <%NodeTypePascalCase%>EditorNodeProperties } from "./modules/types.js";
33

44
declare const RED: EditorRED;
55

‎utils/templates/blank/node-type.html/modules/types.ts.mustache‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { EditorNodeProperties } from "node-red";
2-
import { <%NodeTypePascalCase%>Options } from "../../shared/types";
2+
import { <%NodeTypePascalCase%>Options } from "../../shared/types.js";
33

44
export interface <%NodeTypePascalCase%>EditorNodeProperties
55
extends EditorNodeProperties,
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import testHelper, { TestFlowsItem } from 'node-red-node-test-helper';
2+
import <%NodeTypeCamelCase%>Node from '../nodes/<%NodeTypeKebabCase%>/<%NodeTypeKebabCase%>.js';
3+
import { <%NodeTypePascalCase%>NodeDef } from '../nodes/<%NodeTypeKebabCase%>/modules/types.js';
4+
5+
type FlowsItem = TestFlowsItem<<%NodeTypePascalCase%>NodeDef>;
6+
type Flows = Array<FlowsItem>;
7+
8+
describe('<%NodeTypeKebabCase%> node', () => {
9+
beforeEach((done) => {
10+
testHelper.startServer(done);
11+
});
12+
13+
afterEach((done) => {
14+
testHelper.unload().then(() => {
15+
testHelper.stopServer(done);
16+
});
17+
});
18+
19+
it('should be loaded', (done) => {
20+
const flows: Flows = [
21+
{ id: 'n1', type: '<%NodeTypeKebabCase%>', name: '<%NodeTypeKebabCase%>' },
22+
];
23+
testHelper.load(<%NodeTypeCamelCase%>Node, flows, () => {
24+
const n1 = testHelper.getNode('n1');
25+
expect(n1).toBeTruthy();
26+
expect(n1.name).toEqual('<%NodeTypeKebabCase%>');
27+
done();
28+
});
29+
});
30+
31+
/*
32+
describe('in X mode', () => {
33+
let flows: Flows;
34+
beforeEach(() => {
35+
flows = [
36+
{
37+
id: 'n1',
38+
type: '<%NodeTypeKebabCase%>',
39+
name: '<%NodeTypeKebabCase%>',
40+
mode: 'X', // some node option
41+
wires: [['n2']],
42+
},
43+
{ id: 'n2', type: 'helper' },
44+
];
45+
});
46+
it("should ...", (done) => {
47+
testHelper.load(<%NodeTypeCamelCase%>Node, flows, () => {
48+
const n2 = testHelper.getNode('n2');
49+
const n1 = testHelper.getNode('n1');
50+
n2.on('input', (msg: unknown) => {
51+
expect(msg.payload).toBe('test');
52+
done();
53+
});
54+
n1.receive({ payload: 'test' });
55+
});
56+
});
57+
});
58+
*/
59+
});

0 commit comments

Comments
 (0)