From a531b8b5fe717c3885af356506a9754dede9ad93 Mon Sep 17 00:00:00 2001 From: Ali BARIN Date: Fri, 1 Nov 2024 11:30:45 +0000 Subject: [PATCH] test(step): cover tableName, jsonSchema, virtualAttributes, relationMappings in model --- .../models/__snapshots__/step.test.js.snap | 77 ++++++++++++++++ packages/backend/src/models/step.test.js | 88 +++++++++++++++++++ 2 files changed, 165 insertions(+) create mode 100644 packages/backend/src/models/__snapshots__/step.test.js.snap create mode 100644 packages/backend/src/models/step.test.js diff --git a/packages/backend/src/models/__snapshots__/step.test.js.snap b/packages/backend/src/models/__snapshots__/step.test.js.snap new file mode 100644 index 00000000..aa78645e --- /dev/null +++ b/packages/backend/src/models/__snapshots__/step.test.js.snap @@ -0,0 +1,77 @@ +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + +exports[`Step model > jsonSchema should have correct validations 1`] = ` +{ + "properties": { + "appKey": { + "maxLength": 255, + "minLength": 1, + "type": [ + "string", + "null", + ], + }, + "connectionId": { + "format": "uuid", + "type": [ + "string", + "null", + ], + }, + "createdAt": { + "type": "string", + }, + "deletedAt": { + "type": "string", + }, + "flowId": { + "format": "uuid", + "type": "string", + }, + "id": { + "format": "uuid", + "type": "string", + }, + "key": { + "type": [ + "string", + "null", + ], + }, + "parameters": { + "type": "object", + }, + "position": { + "type": "integer", + }, + "status": { + "default": "incomplete", + "enum": [ + "incomplete", + "completed", + ], + "type": "string", + }, + "type": { + "enum": [ + "action", + "trigger", + ], + "type": "string", + }, + "updatedAt": { + "type": "string", + }, + "webhookPath": { + "type": [ + "string", + "null", + ], + }, + }, + "required": [ + "type", + ], + "type": "object", +} +`; diff --git a/packages/backend/src/models/step.test.js b/packages/backend/src/models/step.test.js new file mode 100644 index 00000000..a85b80a3 --- /dev/null +++ b/packages/backend/src/models/step.test.js @@ -0,0 +1,88 @@ +import { describe, it, expect, vi } from 'vitest'; +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'; + +describe('Step model', () => { + it('tableName should return correct name', () => { + expect(Step.tableName).toBe('steps'); + }); + + it('jsonSchema should have correct validations', () => { + expect(Step.jsonSchema).toMatchSnapshot(); + }); + + it('virtualAttributes should return correct attributes', () => { + const virtualAttributes = Step.virtualAttributes; + + const expectedAttributes = ['iconUrl', 'webhookUrl']; + + expect(virtualAttributes).toStrictEqual(expectedAttributes); + }); + + describe('relationMappings', () => { + it('should return correct associations', () => { + const relationMappings = Step.relationMappings(); + + const expectedRelations = { + flow: { + relation: Base.BelongsToOneRelation, + modelClass: Flow, + join: { + from: 'steps.flow_id', + to: 'flows.id', + }, + }, + connection: { + relation: Base.HasOneRelation, + modelClass: Connection, + join: { + from: 'steps.connection_id', + to: 'connections.id', + }, + }, + lastExecutionStep: { + relation: Base.HasOneRelation, + modelClass: ExecutionStep, + join: { + from: 'steps.id', + to: 'execution_steps.step_id', + }, + filter: expect.any(Function), + }, + executionSteps: { + relation: Base.HasManyRelation, + modelClass: ExecutionStep, + join: { + from: 'steps.id', + to: 'execution_steps.step_id', + }, + }, + }; + + expect(relationMappings).toStrictEqual(expectedRelations); + }); + + it('lastExecutionStep should return the trigger step', () => { + const relations = Step.relationMappings(); + + const firstSpy = vi.fn(); + + const limitSpy = vi.fn().mockImplementation(() => ({ + first: firstSpy, + })); + + const orderBySpy = vi.fn().mockImplementation(() => ({ + limit: limitSpy, + })); + + relations.lastExecutionStep.filter({ orderBy: orderBySpy }); + + expect(orderBySpy).toHaveBeenCalledWith('created_at', 'desc'); + expect(limitSpy).toHaveBeenCalledWith(1); + expect(firstSpy).toHaveBeenCalledOnce(); + }); + }); +});