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

169 lines
4.2 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",
},
},
},
}
}
2023-07-17 22:26:57 +02:00
describe("/v2/views", () => {
2023-07-12 16:13:00 +02:00
const request = setup.getRequest()
const config = setup.getConfig()
let table: Table
afterAll(setup.afterAll)
beforeAll(async () => {
await config.init()
table = await config.createTable(priceTable())
})
2023-07-12 16:33:06 +02:00
describe("fetch", () => {
2023-07-18 09:58:43 +02:00
const views: ViewV2[] = []
2023-07-12 16:33:06 +02:00
beforeAll(async () => {
2023-07-18 10:14:13 +02:00
await config.createTable(priceTable())
2023-07-12 16:33:06 +02:00
for (let id = 0; id < 10; id++) {
2023-07-18 10:14:13 +02:00
views.push(await config.createViewV2())
2023-07-12 16:33:06 +02:00
}
})
it("returns all views", async () => {
const res = await request
2023-07-17 22:26:57 +02:00
.get(`/api/v2/views`)
2023-07-12 16:33:06 +02:00
.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-18 10:14:13 +02:00
newViews.push(await config.createViewV2({ tableId: newTable._id }))
2023-07-12 17:02:54 +02:00
}
2023-07-18 10:14:13 +02:00
2023-07-12 17:02:54 +02:00
const res = await request
2023-07-17 22:26:57 +02:00
.get(`/api/v2/views?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(
2023-07-17 22:26:57 +02:00
`/api/v2/views?tableId=${structures.generator.guid()}&tableId=${structures.generator.guid()}`
2023-07-13 11:36:26 +02:00
)
.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", () => {
2023-07-18 10:26:53 +02:00
const getView = (viewId: string) => {
return request
.get(`/api/v2/views/${viewId}`)
.set(config.defaultHeaders())
.expect("Content-Type", /json/)
}
2023-07-18 09:58:43 +02:00
let view: ViewV2
2023-07-12 17:48:11 +02:00
beforeAll(async () => {
2023-07-18 10:14:13 +02:00
view = await config.createViewV2()
2023-07-12 17:48:11 +02:00
})
2023-07-12 18:01:46 +02:00
it("can fetch the expected view", async () => {
2023-07-18 09:58:43 +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).toEqual({
2023-07-18 09:58:43 +02:00
data: {
...view,
_id: view._id,
_rev: view._rev,
createdAt: expect.any(String),
updatedAt: expect.any(String),
},
2023-07-12 17:48:11 +02:00
})
})
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-18 10:14:13 +02:00
const newView: ViewV2 = {
name: generator.name(),
tableId: config.table!._id!,
}
const res = await request
.post(`/api/v2/views`)
.send(newView)
.set(config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(200)
2023-07-12 16:13:00 +02:00
expect(res.status).toBe(200)
expect(res.body._id).toBeDefined()
2023-07-12 16:49:50 +02:00
expect(res.body).toEqual({
2023-07-18 09:58:43 +02:00
...newView,
2023-07-12 16:49:50 +02:00
_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", () => {
2023-07-18 09:58:43 +02:00
let view: ViewV2
2023-07-12 18:09:13 +02:00
beforeAll(async () => {
2023-07-18 10:14:13 +02:00
await config.createTable(priceTable())
view = await config.createViewV2()
2023-07-12 18:09:13 +02:00
})
it("can delete an existing view", async () => {
2023-07-18 10:26:53 +02:00
await config.getViewV2(view._id!).expect(200)
2023-07-12 18:09:13 +02:00
await request
2023-07-17 22:26:57 +02:00
.delete(`/api/v2/views/${view._id}`)
2023-07-12 18:09:13 +02:00
.set(config.defaultHeaders())
.expect(204)
2023-07-18 10:26:53 +02:00
await config.getViewV2(view._id!).expect(404)
2023-07-12 18:09:13 +02:00
})
})
2023-07-12 16:13:00 +02:00
})