mirror of
https://github.com/n8n-io/n8n.git
synced 2025-02-20 18:49:27 -08:00
test: Add tests for $getWorkflowStaticData (#12203)
This commit is contained in:
parent
1bfd9c0e91
commit
889b15d33e
|
@ -396,6 +396,177 @@ describe('JsTaskRunner', () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("$getWorkflowStaticData('global')", () => {
|
||||||
|
it('should have the global workflow static data available in runOnceForAllItems', async () => {
|
||||||
|
const outcome = await execTaskWithParams({
|
||||||
|
task: newTaskWithSettings({
|
||||||
|
code: 'return { val: $getWorkflowStaticData("global") }',
|
||||||
|
nodeMode: 'runOnceForAllItems',
|
||||||
|
}),
|
||||||
|
taskData: newDataRequestResponse(inputItems.map(wrapIntoJson), {
|
||||||
|
staticData: {
|
||||||
|
global: { key: 'value' },
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(outcome.result).toEqual([wrapIntoJson({ val: { key: 'value' } })]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should have the global workflow static data available in runOnceForEachItem', async () => {
|
||||||
|
const outcome = await execTaskWithParams({
|
||||||
|
task: newTaskWithSettings({
|
||||||
|
code: 'return { val: $getWorkflowStaticData("global") }',
|
||||||
|
nodeMode: 'runOnceForEachItem',
|
||||||
|
}),
|
||||||
|
taskData: newDataRequestResponse(inputItems.map(wrapIntoJson), {
|
||||||
|
staticData: {
|
||||||
|
global: { key: 'value' },
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(outcome.result).toEqual([
|
||||||
|
withPairedItem(0, wrapIntoJson({ val: { key: 'value' } })),
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test.each<[CodeExecutionMode]>([['runOnceForAllItems'], ['runOnceForEachItem']])(
|
||||||
|
"does not return static data if it hasn't been modified in %s",
|
||||||
|
async (mode) => {
|
||||||
|
const outcome = await execTaskWithParams({
|
||||||
|
task: newTaskWithSettings({
|
||||||
|
code: `
|
||||||
|
const staticData = $getWorkflowStaticData("global");
|
||||||
|
return { val: staticData };
|
||||||
|
`,
|
||||||
|
nodeMode: mode,
|
||||||
|
}),
|
||||||
|
taskData: newDataRequestResponse(inputItems.map(wrapIntoJson), {
|
||||||
|
staticData: {
|
||||||
|
global: { key: 'value' },
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(outcome.staticData).toBeUndefined();
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
test.each<[CodeExecutionMode]>([['runOnceForAllItems'], ['runOnceForEachItem']])(
|
||||||
|
'returns the updated static data in %s',
|
||||||
|
async (mode) => {
|
||||||
|
const outcome = await execTaskWithParams({
|
||||||
|
task: newTaskWithSettings({
|
||||||
|
code: `
|
||||||
|
const staticData = $getWorkflowStaticData("global");
|
||||||
|
staticData.newKey = 'newValue';
|
||||||
|
return { val: staticData };
|
||||||
|
`,
|
||||||
|
nodeMode: mode,
|
||||||
|
}),
|
||||||
|
taskData: newDataRequestResponse(inputItems.map(wrapIntoJson), {
|
||||||
|
staticData: {
|
||||||
|
global: { key: 'value' },
|
||||||
|
'node:OtherNode': { some: 'data' },
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(outcome.staticData).toEqual({
|
||||||
|
global: { key: 'value', newKey: 'newValue' },
|
||||||
|
'node:OtherNode': { some: 'data' },
|
||||||
|
});
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("$getWorkflowStaticData('node')", () => {
|
||||||
|
const createTaskDataWithNodeStaticData = (nodeStaticData: IDataObject) => {
|
||||||
|
const taskData = newDataRequestResponse(inputItems.map(wrapIntoJson));
|
||||||
|
const taskDataKey = `node:${taskData.node.name}`;
|
||||||
|
taskData.workflow.staticData = {
|
||||||
|
global: { 'global-key': 'global-value' },
|
||||||
|
'node:OtherNode': { 'other-key': 'other-value' },
|
||||||
|
[taskDataKey]: nodeStaticData,
|
||||||
|
};
|
||||||
|
|
||||||
|
return taskData;
|
||||||
|
};
|
||||||
|
|
||||||
|
it('should have the node workflow static data available in runOnceForAllItems', async () => {
|
||||||
|
const outcome = await execTaskWithParams({
|
||||||
|
task: newTaskWithSettings({
|
||||||
|
code: 'return { val: $getWorkflowStaticData("node") }',
|
||||||
|
nodeMode: 'runOnceForAllItems',
|
||||||
|
}),
|
||||||
|
taskData: createTaskDataWithNodeStaticData({ key: 'value' }),
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(outcome.result).toEqual([wrapIntoJson({ val: { key: 'value' } })]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should have the node workflow static data available in runOnceForEachItem', async () => {
|
||||||
|
const outcome = await execTaskWithParams({
|
||||||
|
task: newTaskWithSettings({
|
||||||
|
code: 'return { val: $getWorkflowStaticData("node") }',
|
||||||
|
nodeMode: 'runOnceForEachItem',
|
||||||
|
}),
|
||||||
|
taskData: createTaskDataWithNodeStaticData({ key: 'value' }),
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(outcome.result).toEqual([
|
||||||
|
withPairedItem(0, wrapIntoJson({ val: { key: 'value' } })),
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test.each<[CodeExecutionMode]>([['runOnceForAllItems'], ['runOnceForEachItem']])(
|
||||||
|
"does not return static data if it hasn't been modified in %s",
|
||||||
|
async (mode) => {
|
||||||
|
const outcome = await execTaskWithParams({
|
||||||
|
task: newTaskWithSettings({
|
||||||
|
code: `
|
||||||
|
const staticData = $getWorkflowStaticData("node");
|
||||||
|
return { val: staticData };
|
||||||
|
`,
|
||||||
|
nodeMode: mode,
|
||||||
|
}),
|
||||||
|
taskData: createTaskDataWithNodeStaticData({ key: 'value' }),
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(outcome.staticData).toBeUndefined();
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
test.each<[CodeExecutionMode]>([['runOnceForAllItems'], ['runOnceForEachItem']])(
|
||||||
|
'returns the updated static data in %s',
|
||||||
|
async (mode) => {
|
||||||
|
const outcome = await execTaskWithParams({
|
||||||
|
task: newTaskWithSettings({
|
||||||
|
code: `
|
||||||
|
const staticData = $getWorkflowStaticData("node");
|
||||||
|
staticData.newKey = 'newValue';
|
||||||
|
return { val: staticData };
|
||||||
|
`,
|
||||||
|
nodeMode: mode,
|
||||||
|
}),
|
||||||
|
taskData: createTaskDataWithNodeStaticData({ key: 'value' }),
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(outcome.staticData).toEqual({
|
||||||
|
global: { 'global-key': 'global-value' },
|
||||||
|
'node:JsCode': {
|
||||||
|
key: 'value',
|
||||||
|
newKey: 'newValue',
|
||||||
|
},
|
||||||
|
'node:OtherNode': {
|
||||||
|
'other-key': 'other-value',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
it('should allow access to Node.js Buffers', async () => {
|
it('should allow access to Node.js Buffers', async () => {
|
||||||
const outcomeAll = await execTaskWithParams({
|
const outcomeAll = await execTaskWithParams({
|
||||||
task: newTaskWithSettings({
|
task: newTaskWithSettings({
|
||||||
|
|
|
@ -50,7 +50,9 @@ export const newTaskData = (opts: Partial<ITaskData> & Pick<ITaskData, 'source'>
|
||||||
*/
|
*/
|
||||||
export const newDataRequestResponse = (
|
export const newDataRequestResponse = (
|
||||||
inputData: INodeExecutionData[],
|
inputData: INodeExecutionData[],
|
||||||
opts: Partial<DataRequestResponse> = {},
|
opts: Partial<DataRequestResponse> & {
|
||||||
|
staticData?: IDataObject;
|
||||||
|
} = {},
|
||||||
): DataRequestResponse => {
|
): DataRequestResponse => {
|
||||||
const codeNode = newNode({
|
const codeNode = newNode({
|
||||||
name: 'JsCode',
|
name: 'JsCode',
|
||||||
|
@ -81,6 +83,7 @@ export const newDataRequestResponse = (
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
nodes: [manualTriggerNode, codeNode],
|
nodes: [manualTriggerNode, codeNode],
|
||||||
|
staticData: opts.staticData,
|
||||||
},
|
},
|
||||||
inputData: {
|
inputData: {
|
||||||
main: [inputData],
|
main: [inputData],
|
||||||
|
|
Loading…
Reference in a new issue