test: Add tests for $getWorkflowStaticData (#12203)

This commit is contained in:
Tomi Turtiainen 2024-12-13 11:30:32 +02:00 committed by GitHub
parent 1bfd9c0e91
commit 889b15d33e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 175 additions and 1 deletions

View file

@ -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 () => {
const outcomeAll = await execTaskWithParams({
task: newTaskWithSettings({

View file

@ -50,7 +50,9 @@ export const newTaskData = (opts: Partial<ITaskData> & Pick<ITaskData, 'source'>
*/
export const newDataRequestResponse = (
inputData: INodeExecutionData[],
opts: Partial<DataRequestResponse> = {},
opts: Partial<DataRequestResponse> & {
staticData?: IDataObject;
} = {},
): DataRequestResponse => {
const codeNode = newNode({
name: 'JsCode',
@ -81,6 +83,7 @@ export const newDataRequestResponse = (
},
},
nodes: [manualTriggerNode, codeNode],
staticData: opts.staticData,
},
inputData: {
main: [inputData],