From b02512fd3c4203b6dc6697b5a6b7500e14612971 Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Mon, 6 Nov 2023 15:56:58 +0000 Subject: [PATCH] Create a test of a table with 2 link fields in updateRow.spec.ts. --- .../src/automations/tests/updateRow.spec.js | 44 ------- .../src/automations/tests/updateRow.spec.ts | 107 ++++++++++++++++++ .../src/automations/tests/utilities/index.ts | 6 +- 3 files changed, 110 insertions(+), 47 deletions(-) delete mode 100644 packages/server/src/automations/tests/updateRow.spec.js create mode 100644 packages/server/src/automations/tests/updateRow.spec.ts diff --git a/packages/server/src/automations/tests/updateRow.spec.js b/packages/server/src/automations/tests/updateRow.spec.js deleted file mode 100644 index 77383d80e9..0000000000 --- a/packages/server/src/automations/tests/updateRow.spec.js +++ /dev/null @@ -1,44 +0,0 @@ -const setup = require("./utilities") - -describe("test the update row action", () => { - let table, row, inputs - let config = setup.getConfig() - - beforeAll(async () => { - await config.init() - table = await config.createTable() - row = await config.createRow() - inputs = { - rowId: row._id, - row: { - ...row, - name: "Updated name", - // put a falsy option in to be removed - description: "", - } - } - }) - - afterAll(setup.afterAll) - - it("should be able to run the action", async () => { - const res = await setup.runStep(setup.actions.UPDATE_ROW.stepId, inputs) - expect(res.success).toEqual(true) - const updatedRow = await config.getRow(table._id, res.id) - expect(updatedRow.name).toEqual("Updated name") - expect(updatedRow.description).not.toEqual("") - }) - - it("should check invalid inputs return an error", async () => { - const res = await setup.runStep(setup.actions.UPDATE_ROW.stepId, {}) - expect(res.success).toEqual(false) - }) - - it("should return an error when table doesn't exist", async () => { - const res = await setup.runStep(setup.actions.UPDATE_ROW.stepId, { - row: { _id: "invalid" }, - rowId: "invalid", - }) - expect(res.success).toEqual(false) - }) -}) diff --git a/packages/server/src/automations/tests/updateRow.spec.ts b/packages/server/src/automations/tests/updateRow.spec.ts new file mode 100644 index 0000000000..63925b0d3b --- /dev/null +++ b/packages/server/src/automations/tests/updateRow.spec.ts @@ -0,0 +1,107 @@ +import { + FieldSchema, + FieldType, + INTERNAL_TABLE_SOURCE_ID, + InternalTable, + RelationshipType, + Row, + Table, + TableSourceType, +} from "@budibase/types" + +import * as setup from "./utilities" + +describe("test the update row action", () => { + let table: Table, row: Row, inputs: any + let config = setup.getConfig() + + beforeAll(async () => { + await config.init() + table = await config.createTable() + row = await config.createRow() + inputs = { + rowId: row._id, + row: { + ...row, + name: "Updated name", + // put a falsy option in to be removed + description: "", + }, + } + }) + + afterAll(setup.afterAll) + + it("should be able to run the action", async () => { + const res = await setup.runStep(setup.actions.UPDATE_ROW.stepId, inputs) + expect(res.success).toEqual(true) + const updatedRow = await config.getRow(table._id!, res.id) + expect(updatedRow.name).toEqual("Updated name") + expect(updatedRow.description).not.toEqual("") + }) + + it("should check invalid inputs return an error", async () => { + const res = await setup.runStep(setup.actions.UPDATE_ROW.stepId, {}) + expect(res.success).toEqual(false) + }) + + it("should return an error when table doesn't exist", async () => { + const res = await setup.runStep(setup.actions.UPDATE_ROW.stepId, { + row: { _id: "invalid" }, + rowId: "invalid", + }) + expect(res.success).toEqual(false) + }) + + it("should not overwrite links if those links are not set", async () => { + let linkField: FieldSchema = { + type: FieldType.LINK, + name: "", + fieldName: "", + constraints: { + type: "array", + presence: false, + }, + relationshipType: RelationshipType.ONE_TO_MANY, + tableId: InternalTable.USER_METADATA, + } + + let table = await config.api.table.create({ + name: "TestTable", + type: "table", + sourceType: TableSourceType.INTERNAL, + sourceId: INTERNAL_TABLE_SOURCE_ID, + schema: { + user1: { ...linkField, name: "user1", fieldName: "user1" }, + user2: { ...linkField, name: "user2", fieldName: "user2" }, + }, + }) + + let user1 = await config.createUser() + let user2 = await config.createUser() + + let row = await config.api.row.save(table._id!, { + user1: [{ _id: user1._id }], + user2: [{ _id: user2._id }], + }) + + let getResp = await config.api.row.get(table._id!, row._id!) + expect(getResp.body.user1[0]._id).toEqual(user1._id) + expect(getResp.body.user2[0]._id).toEqual(user2._id) + + let stepResp = await setup.runStep(setup.actions.UPDATE_ROW.stepId, { + rowId: row._id, + row: { + _id: row._id, + _rev: row._rev, + tableId: row.tableId, + user1: [{ _id: user2._id }], + }, + }) + expect(stepResp.success).toEqual(true) + + getResp = await config.api.row.get(table._id!, row._id!) + expect(getResp.body.user1[0]._id).toEqual(user2._id) + expect(getResp.body.user2[0]._id).toEqual(user2._id) + }) +}) diff --git a/packages/server/src/automations/tests/utilities/index.ts b/packages/server/src/automations/tests/utilities/index.ts index 9ba4f950f3..cd3ea289ca 100644 --- a/packages/server/src/automations/tests/utilities/index.ts +++ b/packages/server/src/automations/tests/utilities/index.ts @@ -4,11 +4,11 @@ import { BUILTIN_ACTION_DEFINITIONS, getAction } from "../../actions" import emitter from "../../../events/index" import env from "../../../environment" -let config: any +let config: TestConfig -export function getConfig() { +export function getConfig(): TestConfig { if (!config) { - config = new TestConfig(false) + config = new TestConfig(true) } return config }