n8n/packages/nodes-base/test/nodes/Set/SetNode.test.ts
agobrech 5b9c650e55
test: Add unit testing to nodes (no-changelog) (#4890)
* 🧪 Add base for building unit testing within nodes

* Improve helper functions

* 🧪 If node test

* 🧪 Airtable node test

* 🧪 If node test improvements

* 🧪 Airtable node test improvements

* ♻️ cleanup node unit tests

* ♻️ refactor getting node result data to use helper method

*  removed unused variables

* ♻️ Helper to read json files

---------

Co-authored-by: Marcus <marcus@n8n.io>
Co-authored-by: Michael Kret <michael.k@radency.com>
2023-01-30 12:20:33 +01:00

201 lines
3.7 KiB
TypeScript

import { INodeType } from 'n8n-workflow';
import * as Helpers from '../Helpers';
import { Start } from '../../../nodes/Start/Start.node';
import { Set } from '../../../nodes/Set/Set.node';
import { executeWorkflow } from '../ExecuteWorkflow';
import { WorkflowTestData } from '../types';
describe('Execute Set Node', () => {
const tests: Array<WorkflowTestData> = [
{
description: 'should set value',
input: {
workflowData: {
nodes: [
{
id: 'uuid-1',
parameters: {},
name: 'Start',
type: 'n8n-nodes-base.start',
typeVersion: 1,
position: [100, 300],
},
{
id: 'uuid-2',
parameters: {
values: {
number: [
{
name: 'value1',
value: 1,
},
],
},
},
name: 'Set',
type: 'n8n-nodes-base.set',
typeVersion: 1,
position: [280, 300],
},
],
connections: {
Start: {
main: [
[
{
node: 'Set',
type: 'main',
index: 0,
},
],
],
},
},
},
},
output: {
nodeExecutionOrder: ['Start', 'Set'],
nodeData: {
Set: [
[
{
value1: 1,
},
],
],
},
},
},
{
description: 'should set multiple values',
input: {
workflowData: {
nodes: [
{
id: 'uuid-1',
parameters: {},
name: 'Start',
type: 'n8n-nodes-base.start',
typeVersion: 1,
position: [100, 300],
},
{
id: 'uuid-2',
parameters: {
values: {
number: [
{
name: 'value1',
value: 1,
},
],
boolean: [
{
name: 'value2',
value: true,
},
],
},
},
name: 'Set',
type: 'n8n-nodes-base.set',
typeVersion: 1,
position: [280, 300],
},
{
id: 'uuid-3',
parameters: {
values: {
number: [
{
name: 'value1',
value: 2,
},
],
boolean: [
{
name: 'value2',
value: false,
},
],
},
},
name: 'Set1',
type: 'n8n-nodes-base.set',
typeVersion: 1,
position: [280, 300],
},
],
connections: {
Start: {
main: [
[
{
node: 'Set',
type: 'main',
index: 0,
},
],
],
},
Set: {
main: [
[
{
node: 'Set1',
type: 'main',
index: 0,
},
],
],
},
},
},
},
output: {
nodeExecutionOrder: ['Start', 'Set'],
nodeData: {
Set: [
[
{
value1: 1,
value2: true,
},
],
],
Set1: [
[
{
value1: 2,
value2: false,
},
],
],
},
},
},
];
const nodes: INodeType[] = [new Start(), new Set()];
const nodeTypes = Helpers.setup(nodes);
for (const testData of tests) {
test(testData.description, async () => {
// execute workflow
const { result } = await executeWorkflow(testData, nodeTypes);
// check if result node data matches expected test data
const resultNodeData = Helpers.getResultNodeData(result, testData);
resultNodeData.forEach(({ nodeName, resultData }) =>
expect(resultData).toEqual(testData.output.nodeData[nodeName]),
);
// Check if other data has correct value
expect(result.finished).toEqual(true);
expect(result.data.executionData!.contextData).toEqual({});
expect(result.data.executionData!.nodeExecutionStack).toEqual([]);
});
}
});