budibase/packages/server/src/api/routes/tests/viewV2.spec.ts

177 lines
4.4 KiB
TypeScript
Raw Normal View History

2023-07-12 16:13:00 +02:00
import * as setup from "./utilities"
import { FieldType, Table, ViewV2 } from "@budibase/types"
2023-07-12 18:01:46 +02:00
import { generator, structures } from "@budibase/backend-core/tests"
2023-07-12 16:13:00 +02:00
function priceTable(): Table {
return {
name: "table",
type: "table",
schema: {
Price: {
type: FieldType.NUMBER,
name: "Price",
constraints: {},
},
Category: {
type: FieldType.STRING,
name: "Category",
constraints: {
type: "string",
},
},
},
}
}
describe("/views/v2", () => {
const request = setup.getRequest()
const config = setup.getConfig()
let table: Table
afterAll(setup.afterAll)
beforeAll(async () => {
await config.init()
table = await config.createTable(priceTable())
})
const saveView = async (view: ViewV2) => {
return request
.post(`/api/views/v2`)
.send(view)
.set(config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(200)
}
2023-07-13 11:36:26 +02:00
const getView = (viewId: string) => {
2023-07-12 17:48:11 +02:00
return request
2023-07-13 11:36:26 +02:00
.get(`/api/views/v2/${viewId}`)
2023-07-12 17:48:11 +02:00
.set(config.defaultHeaders())
.expect("Content-Type", /json/)
}
2023-07-12 17:02:54 +02:00
function createView(tableId: string): ViewV2 {
2023-07-12 16:33:06 +02:00
return {
name: generator.guid(),
2023-07-12 17:02:54 +02:00
tableId,
2023-07-12 16:33:06 +02:00
}
}
describe("fetch", () => {
2023-07-12 16:49:50 +02:00
const views: any[] = []
2023-07-12 16:33:06 +02:00
beforeAll(async () => {
table = await config.createTable(priceTable())
for (let id = 0; id < 10; id++) {
2023-07-12 17:02:54 +02:00
const res = await saveView(createView(table._id!))
2023-07-12 16:49:50 +02:00
views.push(res.body)
2023-07-12 16:33:06 +02:00
}
})
it("returns all views", async () => {
const res = await request
.get(`/api/views/v2`)
.set(config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(200)
expect(res.body.views.length).toBe(10)
2023-07-12 16:49:50 +02:00
expect(res.body.views).toEqual(
expect.arrayContaining(views.map(v => expect.objectContaining(v)))
)
2023-07-12 16:33:06 +02:00
})
2023-07-12 17:02:54 +02:00
2023-07-13 11:36:26 +02:00
it("can filter by table id", async () => {
const newTable = await config.createTable(priceTable())
const newViews = []
2023-07-12 17:02:54 +02:00
for (let id = 0; id < 5; id++) {
2023-07-13 11:36:26 +02:00
const res = await saveView(createView(newTable._id!))
newViews.push(res.body)
2023-07-12 17:02:54 +02:00
}
const res = await request
2023-07-13 11:36:26 +02:00
.get(`/api/views/v2?tableId=${newTable._id}`)
2023-07-12 17:02:54 +02:00
.set(config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(200)
expect(res.body.views.length).toBe(5)
2023-07-13 11:36:26 +02:00
expect(res.body.views).toEqual(
expect.arrayContaining(newViews.map(v => expect.objectContaining(v)))
)
})
it("can not filter by multiple table ids", async () => {
const res = await request
.get(
`/api/views/v2?tableId=${structures.generator.guid()}&tableId=${structures.generator.guid()}`
)
.set(config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(400)
expect(res.body.message).toBe("tableId type is not valid")
2023-07-12 17:02:54 +02:00
})
})
2023-07-12 17:48:11 +02:00
describe("getView", () => {
let view: any
beforeAll(async () => {
view = (await saveView(createView(table._id!))).body
})
2023-07-12 18:01:46 +02:00
it("can fetch the expected view", async () => {
2023-07-13 11:36:26 +02:00
const res = await getView(view._id).expect(200)
2023-07-12 17:48:11 +02:00
expect(res.status).toBe(200)
expect(res.body._id).toBeDefined()
expect(res.body).toEqual({
...view,
_id: expect.any(String),
_rev: expect.any(String),
createdAt: expect.any(String),
updatedAt: expect.any(String),
})
})
2023-07-12 18:01:46 +02:00
2023-07-13 11:36:26 +02:00
it("will return 404 if the unnexisting id is provided", async () => {
await getView(structures.generator.guid()).expect(404)
2023-07-12 18:01:46 +02:00
})
2023-07-12 17:48:11 +02:00
})
2023-07-12 16:13:00 +02:00
describe("create", () => {
it("persist the view when the view is successfully created", async () => {
2023-07-12 17:02:54 +02:00
const view = createView(table._id!)
2023-07-12 16:13:00 +02:00
const res = await saveView(view)
expect(res.status).toBe(200)
expect(res.body._id).toBeDefined()
2023-07-12 16:49:50 +02:00
expect(res.body).toEqual({
...view,
_id: expect.any(String),
_rev: expect.any(String),
2023-07-12 16:13:00 +02:00
})
})
})
2023-07-12 18:09:13 +02:00
describe("delete", () => {
let view: any
beforeAll(async () => {
table = await config.createTable(priceTable())
view = (await saveView(createView(table._id!))).body
})
it("can delete an existing view", async () => {
2023-07-13 11:36:26 +02:00
await getView(view._id).expect(200)
2023-07-12 18:09:13 +02:00
await request
2023-07-13 11:36:26 +02:00
.delete(`/api/views/v2/${view._id}`)
2023-07-12 18:09:13 +02:00
.set(config.defaultHeaders())
.expect(204)
2023-07-13 11:36:26 +02:00
await getView(view._id).expect(404)
2023-07-12 18:09:13 +02:00
})
})
2023-07-12 16:13:00 +02:00
})