From c0a190a9f257ceb8f45587e9db8c84bd2a2e9011 Mon Sep 17 00:00:00 2001 From: Ali BARIN Date: Mon, 4 Nov 2024 15:54:13 +0000 Subject: [PATCH] test(step): write tests for getSetupFields and delete --- packages/backend/src/models/step.js | 21 ++---- packages/backend/src/models/step.test.js | 87 ++++++++++++++++++++++++ 2 files changed, 94 insertions(+), 14 deletions(-) diff --git a/packages/backend/src/models/step.js b/packages/backend/src/models/step.js index 44039329..6bfd26ed 100644 --- a/packages/backend/src/models/step.js +++ b/packages/backend/src/models/step.js @@ -192,19 +192,18 @@ class Step extends Base { } async getSetupFields() { - let setupSupsteps; + let substeps; if (this.isTrigger) { - setupSupsteps = (await this.getTriggerCommand()).substeps; + substeps = (await this.getTriggerCommand()).substeps; } else { - setupSupsteps = (await this.getActionCommand()).substeps; + substeps = (await this.getActionCommand()).substeps; } - const existingArguments = setupSupsteps.find( + const setupSubstep = substeps.find( (substep) => substep.key === 'chooseTrigger' - ).arguments; - - return existingArguments; + ); + return setupSubstep.arguments; } async getSetupAndDynamicFields() { @@ -311,13 +310,7 @@ class Step extends Base { .$relatedQuery('steps') .where('position', '>', this.position); - const nextStepQueries = nextSteps.map(async (nextStep) => { - await nextStep.$query().patch({ - position: nextStep.position - 1, - }); - }); - - await Promise.all(nextStepQueries); + await flow.updateStepPositionsFrom(this.position, nextSteps); } async updateFor(user, newStepData) { diff --git a/packages/backend/src/models/step.test.js b/packages/backend/src/models/step.test.js index 6cc340ce..9a7bd785 100644 --- a/packages/backend/src/models/step.test.js +++ b/packages/backend/src/models/step.test.js @@ -8,6 +8,7 @@ import Connection from './connection.js'; import ExecutionStep from './execution-step.js'; import Telemetry from '../helpers/telemetry/index.js'; import * as testRunModule from '../services/test-run.js'; +import { createFlow } from '../../test/factories/flow.js'; import { createStep } from '../../test/factories/step.js'; import { createExecutionStep } from '../../test/factories/execution-step.js'; @@ -266,6 +267,92 @@ describe('Step model', () => { }); }); + describe('getSetupFields', () => { + it('should return trigger setup substep fields in trigger step', async () => { + const step = new Step(); + step.appKey = 'webhook'; + step.key = 'catchRawWebhook'; + step.type = 'trigger'; + + expect(await step.getSetupFields()).toStrictEqual([ + { + label: 'Wait until flow is done', + key: 'workSynchronously', + type: 'dropdown', + required: true, + options: [ + { label: 'Yes', value: true }, + { label: 'No', value: false }, + ], + }, + ]); + }); + + it('should return action setup substep fields in action step', async () => { + const step = new Step(); + step.appKey = 'datastore'; + step.key = 'getValue'; + step.type = 'action'; + + expect(await step.getSetupFields()).toStrictEqual([ + { + label: 'Key', + key: 'key', + type: 'string', + required: true, + description: 'The key of your value to get.', + variables: true, + }, + ]); + }); + }); + + it.todo('getSetupAndDynamicFields'); + it.todo('createDynamicFields'); + it.todo('createDynamicData'); + it.todo('updateWebhookUrl'); + + describe('delete', () => { + it('should delete the step and align the positions', async () => { + const flow = await createFlow(); + await createStep({ flowId: flow.id, position: 1, type: 'trigger' }); + await createStep({ flowId: flow.id, position: 2 }); + const stepToDelete = await createStep({ flowId: flow.id, position: 3 }); + await createStep({ flowId: flow.id, position: 4 }); + + await stepToDelete.delete(); + + const steps = await flow.$relatedQuery('steps'); + const stepIds = steps.map((step) => step.id); + + expect(stepIds).not.toContain(stepToDelete.id); + }); + + it('should align the positions of remaining steps', async () => { + const flow = await createFlow(); + await createStep({ flowId: flow.id, position: 1, type: 'trigger' }); + await createStep({ flowId: flow.id, position: 2 }); + const stepToDelete = await createStep({ flowId: flow.id, position: 3 }); + await createStep({ flowId: flow.id, position: 4 }); + + await stepToDelete.delete(); + + const steps = await flow.$relatedQuery('steps'); + const stepPositions = steps.map((step) => step.position); + + expect(stepPositions).toMatchObject([1, 2, 3]); + }); + + it('should delete related execution steps', async () => { + const step = await createStep(); + const executionStep = await createExecutionStep({ stepId: step.id }); + + await step.delete(); + + expect(await executionStep.$query()).toBe(undefined); + }); + }); + describe('$afterInsert', () => { it('should call super.$afterInsert', async () => { const superAfterInsertSpy = vi.spyOn(Base.prototype, '$afterInsert');