diff --git a/packages/server/src/api/routes/tests/rowAction.spec.ts b/packages/server/src/api/routes/tests/rowAction.spec.ts index 08a57eccb8..6fbdce3afa 100644 --- a/packages/server/src/api/routes/tests/rowAction.spec.ts +++ b/packages/server/src/api/routes/tests/rowAction.spec.ts @@ -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()) diff --git a/packages/server/src/sdk/app/rowActions.ts b/packages/server/src/sdk/app/rowActions.ts index 4e96339b06..f47d4348ea 100644 --- a/packages/server/src/sdk/app/rowActions.ts +++ b/packages/server/src/sdk/app/rowActions.ts @@ -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, } }