From 810bb415471afabc3e18c6a31871ca1202ed6ed9 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Mon, 24 Jul 2023 13:37:11 +0200 Subject: [PATCH] Add sdk tests --- .../src/sdk/app/tables/tests/tables.spec.ts | 198 ++++++++++++++++++ 1 file changed, 198 insertions(+) create mode 100644 packages/server/src/sdk/app/tables/tests/tables.spec.ts diff --git a/packages/server/src/sdk/app/tables/tests/tables.spec.ts b/packages/server/src/sdk/app/tables/tests/tables.spec.ts new file mode 100644 index 0000000000..48bcac7920 --- /dev/null +++ b/packages/server/src/sdk/app/tables/tests/tables.spec.ts @@ -0,0 +1,198 @@ +import { FieldType, Table, ViewV2 } from "@budibase/types" +import { generator } from "@budibase/backend-core/tests" +import sdk from "../../.." + +describe("table sdk", () => { + describe("enrichViewSchemas", () => { + describe("fetch", () => { + const basicTable: Table = { + _id: generator.guid(), + name: "TestTable", + type: "table", + schema: { + name: { + type: FieldType.STRING, + name: "name", + visible: true, + width: 80, + constraints: { + type: "string", + }, + }, + description: { + type: FieldType.STRING, + name: "description", + visible: true, + width: 200, + constraints: { + type: "string", + }, + }, + id: { + type: FieldType.NUMBER, + name: "id", + visible: true, + constraints: { + type: "number", + }, + }, + hiddenField: { + type: FieldType.STRING, + name: "hiddenField", + visible: false, + constraints: { + type: "string", + }, + }, + }, + } + + it("should fetch the default schema if not overriden", async () => { + const tableId = basicTable._id! + const view: ViewV2 = { + version: 2, + id: generator.guid(), + name: generator.guid(), + tableId, + } + const res = sdk.tables.enrichViewSchemas({ + ...basicTable, + views: { [view.name]: view }, + }) + + expect(res).toEqual({ + ...basicTable, + views: { + [view.name]: { + ...view, + schema: { + name: { + type: "string", + name: "name", + visible: true, + width: 80, + constraints: { + type: "string", + }, + }, + description: { + type: "string", + name: "description", + visible: true, + width: 200, + constraints: { + type: "string", + }, + }, + id: { + type: "number", + name: "id", + visible: true, + constraints: { + type: "number", + }, + }, + hiddenField: { + type: "string", + name: "hiddenField", + visible: false, + constraints: { + type: "string", + }, + }, + }, + }, + }, + }) + }) + + it("if view schema only defines visiblility, should only fetch the selected fields", async () => { + const tableId = basicTable._id! + const view: ViewV2 = { + version: 2, + id: generator.guid(), + name: generator.guid(), + tableId, + columns: { + name: { visible: true }, + id: { visible: true }, + description: { visible: false }, + }, + } + + const res = sdk.tables.enrichViewSchemas({ + ...basicTable, + views: { [view.name]: view }, + }) + + expect(res).toEqual( + expect.objectContaining({ + ...basicTable, + views: { + [view.name]: { + ...view, + schema: { + name: { + type: "string", + name: "name", + visible: true, + width: 80, + constraints: { + type: "string", + }, + }, + id: { + type: "number", + name: "id", + visible: true, + constraints: { + type: "number", + }, + }, + }, + }, + }, + }) + ) + }) + + it("schema does not break if the view has corrupted columns", async () => { + const tableId = basicTable._id! + const view: ViewV2 = { + version: 2, + id: generator.guid(), + name: generator.guid(), + tableId, + columns: { unnexisting: { visible: true }, name: { visible: true } }, + } + + const res = sdk.tables.enrichViewSchemas({ + ...basicTable, + views: { [view.name]: view }, + }) + + expect(res).toEqual( + expect.objectContaining({ + ...basicTable, + views: { + [view.name]: { + ...view, + schema: { + name: { + type: "string", + name: "name", + visible: true, + width: 80, + constraints: { + type: "string", + }, + }, + }, + }, + }, + }) + ) + }) + }) + }) +})