diff --git a/packages/framework/src/client.test.ts b/packages/framework/src/client.test.ts index 113851cb0b2..cc3c5ac067e 100644 --- a/packages/framework/src/client.test.ts +++ b/packages/framework/src/client.test.ts @@ -475,6 +475,43 @@ describe('Novu Client', () => { await expect(client.executeWorkflow(event)).rejects.toThrow(ExecutionEventInputInvalidError); }); + it('should preview with mocked data during preview', async () => { + const workflowMock = workflow( + 'mock-workflow', + async ({ step, payload }) => { + // eslint-disable-next-line @typescript-eslint/no-base-to-string + await step.email('send-email', async () => ({ body: 'Test: ' + payload.name, subject: 'Subject' })); + }, + { + payloadSchema: { + type: 'object', + properties: { + name: { type: 'string' }, + }, + required: ['name'], + }, + } + ); + + client.addWorkflows([workflowMock]); + + const event: IEvent = { + action: 'preview', + workflowId: 'mock-workflow', + stepId: 'send-email', + subscriber: {}, + state: [], + data: {}, + inputs: {}, + }; + + const executionResult = await client.executeWorkflow(event); + expect(executionResult).toBeDefined(); + expect(executionResult.outputs).toBeDefined(); + + expect((executionResult.outputs as any).body).toBe('Test: [placeholder]'); + }); + it('should preview workflow successfully when action is preview', async () => { const newWorkflow = workflow('test-workflow', async ({ step }) => { await step.email('send-email', async () => ({ body: 'Test Body', subject: 'Subject' })); diff --git a/packages/framework/src/client.ts b/packages/framework/src/client.ts index de252c4a6d5..5d5b803fb57 100644 --- a/packages/framework/src/client.ts +++ b/packages/framework/src/client.ts @@ -39,6 +39,13 @@ import { VERSION } from './version'; import { Skip } from './types/skip.types'; import { Liquid } from 'liquidjs'; +/** + * We want to respond with a consistent string value for preview + */ +JSONSchemaFaker.random.shuffle = function () { + return ['[placeholder]']; +}; + JSONSchemaFaker.option({ useDefaultValue: true, alwaysFakeOptionals: true, @@ -429,11 +436,16 @@ export class Client { } private createExecutionInputs(event: IEvent, workflow: DiscoverWorkflowOutput): Record { - const executionData = event.data; + let payload = event.data; + if (event.action === 'preview') { + const mockResult = this.mock(workflow.data.schema); + + payload = Object.assign(mockResult, payload); + } - this.validate(event.data, workflow.data.validate, workflow.data.schema, 'event', 'input', event.workflowId); + this.validate(payload, workflow.data.validate, workflow.data.schema, 'event', 'input', event.workflowId); - return executionData; + return payload; } private prettyPrintExecute(payload: IEvent, duration: number, error?: Error): void {