From 42c2131144283f63f2907a55cbc5cd874579a243 Mon Sep 17 00:00:00 2001 From: Ali BARIN Date: Mon, 4 Nov 2024 11:43:25 +0000 Subject: [PATCH] test(step): write tests for getApp, test, getLastExecutionStep and getNextStep --- packages/backend/src/models/step.js | 7 +--- packages/backend/src/models/step.test.js | 53 ++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 6 deletions(-) diff --git a/packages/backend/src/models/step.js b/packages/backend/src/models/step.js index 0e7b6105..44039329 100644 --- a/packages/backend/src/models/step.js +++ b/packages/backend/src/models/step.js @@ -160,12 +160,7 @@ class Step extends Base { } async getLastExecutionStep() { - const lastExecutionStep = await this.$relatedQuery('executionSteps') - .orderBy('created_at', 'desc') - .limit(1) - .first(); - - return lastExecutionStep; + return await this.$relatedQuery('lastExecutionStep'); } async getNextStep() { diff --git a/packages/backend/src/models/step.test.js b/packages/backend/src/models/step.test.js index e812ca02..b57f3c80 100644 --- a/packages/backend/src/models/step.test.js +++ b/packages/backend/src/models/step.test.js @@ -1,10 +1,14 @@ import { describe, it, expect, vi } from 'vitest'; import appConfig from '../config/app.js'; +import App from './app.js'; import Base from './base.js'; import Step from './step.js'; import Flow from './flow.js'; import Connection from './connection.js'; import ExecutionStep from './execution-step.js'; +import * as testRunModule from '../services/test-run.js'; +import { createStep } from '../../test/factories/step.js'; +import { createExecutionStep } from '../../test/factories/execution-step.js'; describe('Step model', () => { it('tableName should return correct name', () => { @@ -165,4 +169,53 @@ describe('Step model', () => { expect(await step.getWebhookUrl()).toBe(undefined); }); }); + describe('getApp', () => { + it('should return app with the given appKey', async () => { + const step = new Step(); + step.appKey = 'gitlab'; + + const findOneByKeySpy = vi.spyOn(App, 'findOneByKey').mockResolvedValue(); + + await step.getApp(); + expect(findOneByKeySpy).toHaveBeenCalledWith('gitlab'); + }); + + it('should return null with no appKey', async () => { + const step = new Step(); + + const findOneByKeySpy = vi.spyOn(App, 'findOneByKey').mockResolvedValue(); + + expect(await step.getApp()).toBe(null); + expect(findOneByKeySpy).not.toHaveBeenCalled(); + }); + }); + + it('test should test the step and mark it as completed', async () => { + const step = await createStep({ status: 'incomplete' }); + + const testRunSpy = vi.spyOn(testRunModule, 'default').mockResolvedValue(); + + const updatedStep = await step.test(); + + expect(testRunSpy).toHaveBeenCalledWith({ stepId: step.id }); + expect(updatedStep.status).toBe('completed'); + }); + + it('getLastExecutionStep should return last execution step', async () => { + const step = await createStep(); + await createExecutionStep({ stepId: step.id }); + const secondExecutionStep = await createExecutionStep({ stepId: step.id }); + + expect(await step.getLastExecutionStep()).toStrictEqual( + secondExecutionStep + ); + }); + + it('getNextStep should return the next step', async () => { + const firstStep = await createStep(); + const secondStep = await createStep({ flowId: firstStep.flowId }); + const thirdStep = await createStep({ flowId: firstStep.flowId }); + + expect(await secondStep.getNextStep()).toStrictEqual(thirdStep); + }); });