More progress on fixing up table.spec.ts.

This commit is contained in:
Sam Rose 2024-04-05 11:50:27 +01:00
parent f80a207d28
commit bc072e1424
No known key found for this signature in database
2 changed files with 121 additions and 89 deletions

View File

@ -17,7 +17,6 @@ import {
} from "@budibase/types"
import { checkBuilderEndpoint } from "./utilities/TestFunctions"
import * as setup from "./utilities"
import sdk from "../../../sdk"
import * as uuid from "uuid"
import tk from "timekeeper"
@ -59,8 +58,11 @@ describe.each([
})
it("returns a success message when the table is successfully created", async () => {
const table = await config.api.table.save(basicTable(datasource))
expect(table.name).toEqual("TestTable")
const name = generator.guid()
const table = await config.api.table.save(
tableForDatasource(datasource, { name })
)
expect(table.name).toEqual(name)
expect(events.table.created).toHaveBeenCalledTimes(1)
expect(events.table.created).toHaveBeenCalledWith(table)
})
@ -149,11 +151,8 @@ describe.each([
describe("update", () => {
it("updates a table", async () => {
const table = await config.api.table.save({
name: "TestTable",
type: "table",
sourceId: INTERNAL_TABLE_SOURCE_ID,
sourceType: TableSourceType.INTERNAL,
const table = await config.api.table.save(
tableForDatasource(datasource, {
schema: {
name: {
type: FieldType.STRING,
@ -164,6 +163,12 @@ describe.each([
},
},
})
)
await config.api.table.save({
...table,
name: generator.guid(),
})
expect(events.table.updated).toHaveBeenCalledTimes(1)
expect(events.table.updated).toHaveBeenCalledWith(table)
@ -209,7 +214,7 @@ describe.each([
expect(res.name).toBeUndefined()
})
it.only("updates only the passed fields", async () => {
it("updates only the passed fields", async () => {
const table = await config.api.table.save(
tableForDatasource(datasource, {
name: "TestTable",
@ -350,23 +355,20 @@ describe.each([
describe("fetch", () => {
let testTable: Table
const enrichViewSchemasMock = jest.spyOn(sdk.tables, "enrichViewSchemas")
beforeEach(async () => {
testTable = await config.api.table.save(basicTable(datasource))
testTable = await config.api.table.save(
basicTable(datasource, { name: generator.guid() })
)
})
afterAll(() => {
enrichViewSchemasMock.mockRestore()
})
it("returns all the tables for that instance in the response body", async () => {
it("returns all tables", async () => {
const res = await config.api.table.fetch()
const table = res.find(t => t._id === testTable._id)
expect(table).toBeDefined()
expect(table!.name).toEqual(testTable.name)
expect(table!.type).toEqual("table")
expect(table!.sourceType).toEqual("internal")
expect(table!.sourceType).toEqual(testTable.sourceType)
})
it("should apply authorization to endpoint", async () => {
@ -377,63 +379,84 @@ describe.each([
})
})
it("should fetch views", async () => {
const tableId = config.table!._id!
const views = [
await config.api.viewV2.create({ tableId, name: generator.guid() }),
await config.api.viewV2.create({ tableId, name: generator.guid() }),
]
const res = await config.api.table.fetch()
expect(res).toEqual(
expect.arrayContaining([
expect.objectContaining({
_id: tableId,
views: views.reduce((p, c) => {
p[c.name] = { ...c, schema: expect.anything() }
return p
}, {} as any),
}),
])
)
it("should enrich the view schemas", async () => {
const viewV2 = await config.api.viewV2.create({
tableId: testTable._id!,
name: generator.guid(),
})
const legacyView = await config.api.legacyView.save({
tableId: testTable._id!,
name: generator.guid(),
filters: [],
schema: {},
})
it("should enrich the view schemas for viewsV2", async () => {
const tableId = config.table!._id!
enrichViewSchemasMock.mockImplementation(t => ({
...t,
views: {
view1: {
version: 2,
name: "view1",
schema: {},
id: "new_view_id",
tableId: t._id!,
},
},
}))
await config.api.viewV2.create({ tableId, name: generator.guid() })
await config.createLegacyView()
const res = await config.api.table.fetch()
expect(res).toEqual(
expect.arrayContaining([
expect.objectContaining({
_id: tableId,
views: {
view1: {
version: 2,
name: "view1",
schema: {},
id: "new_view_id",
tableId,
const table = res.find(t => t._id === testTable._id)
expect(table).toBeDefined()
expect(table!.views![viewV2.name]).toBeDefined()
expect(table!.views![viewV2.name!]).toEqual({
...viewV2,
schema: {
description: {
constraints: {
type: "string",
},
name: "description",
type: "string",
visible: false,
},
name: {
constraints: {
type: "string",
},
name: "name",
type: "string",
visible: false,
},
},
}),
])
)
})
if (isInternal) {
expect(table!.views![legacyView.name!]).toBeDefined()
expect(table!.views![legacyView.name!]).toEqual({
...legacyView,
schema: {
description: {
constraints: {
type: "string",
},
name: "description",
type: "string",
},
name: {
constraints: {
type: "string",
},
name: "name",
type: "string",
},
},
})
}
// expect(res).toEqual(
// expect.arrayContaining([
// expect.objectContaining({
// _id: tableId,
// views: {
// view1: {
// version: 2,
// name: "view1",
// schema: {},
// id: "new_view_id",
// tableId,
// },
// },
// }),
// ])
// )
})
})

View File

@ -27,6 +27,7 @@ import {
} from "@budibase/types"
import { LoopInput, LoopStepType } from "../../definitions/automations"
import { merge } from "lodash"
import { generator } from "@budibase/backend-core/tests"
const { BUILTIN_ROLE_IDS } = roles
@ -36,7 +37,7 @@ export function tableForDatasource(
): Table {
return merge(
{
name: "TestTable",
name: generator.guid(),
type: "table",
sourceType: datasource
? TableSourceType.EXTERNAL
@ -48,8 +49,14 @@ export function tableForDatasource(
)
}
export function basicTable(datasource?: Datasource): Table {
return tableForDatasource(datasource, {
export function basicTable(
datasource?: Datasource,
...extra: Partial<Table>[]
): Table {
return tableForDatasource(
datasource,
{
name: "TestTable",
schema: {
name: {
type: FieldType.STRING,
@ -66,7 +73,9 @@ export function basicTable(datasource?: Datasource): Table {
},
},
},
})
},
...extra
)
}
export function basicView(tableId: string) {