1
0
Fork 0
mirror of synced 2024-09-20 19:33:10 +12:00

Simplify tests

This commit is contained in:
Adria Navarro 2024-07-11 10:46:29 +02:00
parent 645abea2cd
commit fac9c35bce
2 changed files with 54 additions and 40 deletions

View file

@ -4,6 +4,7 @@ import tk from "timekeeper"
import { CreateRowActionRequest } from "@budibase/types" import { CreateRowActionRequest } from "@budibase/types"
import * as setup from "./utilities" import * as setup from "./utilities"
import { generator } from "@budibase/backend-core/tests" import { generator } from "@budibase/backend-core/tests"
import { Expectations } from "src/tests/utilities/api/base"
describe("/rowsActions", () => { describe("/rowsActions", () => {
const config = setup.getConfig() const config = setup.getConfig()
@ -22,6 +23,23 @@ describe("/rowsActions", () => {
afterAll(setup.afterAll) afterAll(setup.afterAll)
async function createRowAction(
tableId: string,
rowAction: CreateRowActionRequest,
expectations?: Expectations,
opts?: { publicUser?: boolean }
) {
return await config.api.rowAction.save(
tableId,
rowAction,
{
...expectations,
status: expectations?.status || 201,
},
opts
)
}
function createRowActionRequest(): CreateRowActionRequest { function createRowActionRequest(): CreateRowActionRequest {
return { return {
name: generator.word(), name: generator.word(),
@ -30,7 +48,7 @@ describe("/rowsActions", () => {
function unauthorisedTests() { function unauthorisedTests() {
it("returns unauthorised (401) for unauthenticated requests", async () => { it("returns unauthorised (401) for unauthenticated requests", async () => {
await config.api.rowAction.save( await createRowAction(
tableId, tableId,
createRowActionRequest(), createRowActionRequest(),
{ {
@ -48,20 +66,16 @@ describe("/rowsActions", () => {
builder: {}, builder: {},
}) })
await config.withUser(user, async () => { await config.withUser(user, async () => {
await config.api.rowAction.save( await createRowAction(generator.guid(), createRowActionRequest(), {
generator.guid(), status: 403,
createRowActionRequest(), })
{ status: 403 }
)
}) })
}) })
it("rejects (404) for a non-existing table", async () => { it("rejects (404) for a non-existing table", async () => {
await config.api.rowAction.save( await createRowAction(generator.guid(), createRowActionRequest(), {
generator.guid(), status: 404,
createRowActionRequest(), })
{ status: 404 }
)
}) })
} }
@ -71,9 +85,7 @@ describe("/rowsActions", () => {
it("creates new row actions for tables without existing actions", async () => { it("creates new row actions for tables without existing actions", async () => {
const rowAction = createRowActionRequest() const rowAction = createRowActionRequest()
const res = await config.api.rowAction.save(tableId, rowAction, { const res = await createRowAction(tableId, rowAction, { status: 201 })
status: 201,
})
expect(res).toEqual({ expect(res).toEqual({
_id: `${tableId}_row_actions`, _id: `${tableId}_row_actions`,
@ -88,15 +100,9 @@ describe("/rowsActions", () => {
it("can create multiple row actions for the same table", async () => { it("can create multiple row actions for the same table", async () => {
const rowActions = generator.unique(() => createRowActionRequest(), 3) const rowActions = generator.unique(() => createRowActionRequest(), 3)
await config.api.rowAction.save(tableId, rowActions[0], { await createRowAction(tableId, rowActions[0])
status: 201, await createRowAction(tableId, rowActions[1])
}) const res = await createRowAction(tableId, rowActions[2])
await config.api.rowAction.save(tableId, rowActions[1], {
status: 201,
})
const res = await config.api.rowAction.save(tableId, rowActions[2], {
status: 201,
})
expect(res).toEqual({ expect(res).toEqual({
_id: `${tableId}_row_actions`, _id: `${tableId}_row_actions`,
@ -117,12 +123,8 @@ describe("/rowsActions", () => {
const rowAction1 = createRowActionRequest() const rowAction1 = createRowActionRequest()
const rowAction2 = createRowActionRequest() const rowAction2 = createRowActionRequest()
const res1 = await config.api.rowAction.save(tableId, rowAction1, { const res1 = await createRowAction(tableId, rowAction1)
status: 201, const res2 = await createRowAction(otherTableId, rowAction2)
})
const res2 = await config.api.rowAction.save(otherTableId, rowAction2, {
status: 201,
})
expect(res1).toEqual({ expect(res1).toEqual({
_id: `${tableId}_row_actions`, _id: `${tableId}_row_actions`,
@ -148,7 +150,7 @@ describe("/rowsActions", () => {
name: "", name: "",
} }
await config.api.rowAction.save(tableId, rowAction, { await createRowAction(tableId, rowAction, {
status: 400, status: 400,
body: { body: {
message: 'Invalid body - "name" is not allowed to be empty', message: 'Invalid body - "name" is not allowed to be empty',
@ -165,5 +167,18 @@ describe("/rowsActions", () => {
expect(res).toEqual({ tableId, actions: [] }) expect(res).toEqual({ tableId, actions: [] })
}) })
it("returns only the", async () => {
const rowActions = generator.unique(() => createRowActionRequest(), 5)
for (const rowAction of rowActions) {
await createRowAction(tableId, rowAction)
}
const otherTable = await config.api.table.save(
setup.structures.basicTable()
)
const otherTableId = otherTable._id!
await createRowAction(otherTableId, createRowActionRequest())
})
}) })
}) })

View file

@ -1,8 +1,4 @@
import { import { CreateRowActionRequest, RowActionsResponse } from "@budibase/types"
CreateRowActionRequest,
RowAction,
RowActionsResponse,
} from "@budibase/types"
import { Expectations, TestAPI } from "./base" import { Expectations, TestAPI } from "./base"
export class RowActionAPI extends TestAPI { export class RowActionAPI extends TestAPI {
@ -12,11 +8,14 @@ export class RowActionAPI extends TestAPI {
expectations?: Expectations, expectations?: Expectations,
config?: { publicUser?: boolean } config?: { publicUser?: boolean }
) => { ) => {
return await this._post<RowAction>(`/api/tables/${tableId}/actions`, { return await this._post<RowActionsResponse>(
body: rowAction, `/api/tables/${tableId}/actions`,
expectations, {
...config, body: rowAction,
}) expectations,
...config,
}
)
} }
find = async ( find = async (