Trims names

This commit is contained in:
Adria Navarro 2024-07-17 12:16:14 +02:00
parent 7fb13b757b
commit c51d2cd431
2 changed files with 59 additions and 4 deletions

View File

@ -95,6 +95,31 @@ describe("/rowsActions", () => {
})
})
it("trims row action names", async () => {
const name = " action name "
const res = await createRowAction(
tableId,
{ name },
{
status: 201,
}
)
expect(res).toEqual({
id: expect.stringMatching(/^row_action_\w+/),
tableId: tableId,
name: "action name",
})
expect(await config.api.rowAction.find(tableId)).toEqual({
actions: {
[res.id]: expect.objectContaining({
name: "action name",
}),
},
})
})
it("can create multiple row actions for the same table", async () => {
const rowActions = createRowActionRequests(3)
const responses: RowActionResponse[] = []
@ -224,6 +249,33 @@ describe("/rowsActions", () => {
)
})
it("trims row action names", async () => {
const rowAction = await createRowAction(
tableId,
createRowActionRequest(),
{
status: 201,
}
)
const res = await config.api.rowAction.update(tableId, rowAction.id, {
...rowAction,
name: " action name ",
})
expect(res).toEqual(expect.objectContaining({ name: "action name" }))
expect(await config.api.rowAction.find(tableId)).toEqual(
expect.objectContaining({
actions: expect.objectContaining({
[rowAction.id]: expect.objectContaining({
name: "action name",
}),
}),
})
)
})
it("throws Bad Request when trying to update by a non-existing id", async () => {
await createRowAction(tableId, createRowActionRequest())

View File

@ -8,6 +8,8 @@ import {
} from "@budibase/types"
export async function create(tableId: string, rowAction: { name: string }) {
const action = { name: rowAction.name.trim() }
const db = context.getAppDB()
const rowActionsId = generateRowActionsID(tableId)
let doc: TableRowActions
@ -22,12 +24,12 @@ export async function create(tableId: string, rowAction: { name: string }) {
}
const newId = `${VirtualDocumentType.ROW_ACTION}${SEPARATOR}${utils.newid()}`
doc.actions[newId] = rowAction
doc.actions[newId] = action
await db.put(doc)
return {
id: newId,
...rowAction,
...action,
}
}
@ -49,6 +51,7 @@ export async function update(
rowActionId: string,
rowAction: { name: string }
) {
const action = { name: rowAction.name.trim() }
const actionsDoc = await get(tableId)
if (!actionsDoc.actions[rowActionId]) {
@ -57,14 +60,14 @@ export async function update(
400
)
}
actionsDoc.actions[rowActionId] = rowAction
actionsDoc.actions[rowActionId] = action
const db = context.getAppDB()
await db.put(actionsDoc)
return {
id: rowActionId,
...rowAction,
...action,
}
}