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

162 lines
3.8 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
import sdk from "../../../sdk"
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-12 18:01:46 +02:00
const getView = ({
tableId,
viewId,
}: {
tableId: string
viewId: string
}) => {
2023-07-12 17:48:11 +02:00
return request
2023-07-12 18:01:46 +02:00
.get(`/api/views/v2/${tableId}/${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
describe("findByTable", () => {
const views: any[] = []
beforeAll(async () => {
table = await config.createTable(priceTable())
for (let id = 0; id < 5; id++) {
const res = await saveView(createView(table._id!))
views.push(res.body)
}
})
it("returns all views", async () => {
const res = await request
.get(`/api/views/v2/${table._id}`)
.set(config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(200)
expect(res.body.views.length).toBe(5)
expect(res.body.views).toEqual(expect.arrayContaining([]))
})
})
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 () => {
const res = await getView({
tableId: view.tableId,
viewId: 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
it("will return 404 if the wrong table id is provided", async () => {
await getView({
tableId: structures.generator.guid(),
viewId: view._id,
}).expect(404)
})
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
})
})
})
})