From b1cdb7084748172af438ee403c9352328166ea68 Mon Sep 17 00:00:00 2001 From: CJ Cenizal Date: Mon, 11 Dec 2023 13:30:43 -0800 Subject: [PATCH] Support decorating Table actions with testIds. --- src/docs/pages/table/Table.tsx | 10 +++++++--- src/lib/components/table/Table.tsx | 3 +++ src/lib/components/table/TableRowActions.tsx | 9 ++++++--- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/docs/pages/table/Table.tsx b/src/docs/pages/table/Table.tsx index 2fd42cd..52acb04 100644 --- a/src/docs/pages/table/Table.tsx +++ b/src/docs/pages/table/Table.tsx @@ -156,17 +156,20 @@ export const Table = () => { label: "Edit", onClick: (person: Person) => { console.log("Edit", person); - } + }, + testId: "editAction" }, { label: "Delete", onClick: (person: Person) => { console.log("Delete", person); - } + }, + testId: "deleteAction" }, { label: "Search", - href: (person: Person) => `https://www.google.com/search?q=${person.name}` + href: (person: Person) => `https://www.google.com/search?q=${person.name}`, + testId: "searchAction" } ]; @@ -297,6 +300,7 @@ export const Table = () => { rows={rows} content={content} actions={actions} + actionsTestIdProvider={(person: Person) => `actions-${person.id}`} pagination={pagination} selection={selection} onSort={(column, direction) => console.log("Sort", column, direction)} diff --git a/src/lib/components/table/Table.tsx b/src/lib/components/table/Table.tsx index 449a38d..432d007 100644 --- a/src/lib/components/table/Table.tsx +++ b/src/lib/components/table/Table.tsx @@ -36,6 +36,7 @@ type Props = { columns: Column[]; rows: T[]; actions?: TableRowActionsProps["actions"]; + actionsTestIdProvider?: (row: T) => string; pagination?: Pagination | Pager; selection?: Selection; search?: Search; @@ -75,6 +76,7 @@ export const VuiTable = ({ columns, rows, actions, + actionsTestIdProvider, pagination, selection, search, @@ -195,6 +197,7 @@ export const VuiTable = ({ setRowBeingActedUpon(undefined); } }} + testId={actionsTestIdProvider?.(row) ?? undefined} /> )} diff --git a/src/lib/components/table/TableRowActions.tsx b/src/lib/components/table/TableRowActions.tsx index dd31170..80f8aa3 100644 --- a/src/lib/components/table/TableRowActions.tsx +++ b/src/lib/components/table/TableRowActions.tsx @@ -11,22 +11,24 @@ export type Action = { isDisabled?: (row: T) => boolean; onClick?: (row: T) => void; href?: (row: T) => string | undefined; + testId?: string; }; export type Props = { row: any; actions: Action[]; onToggle: (isSelected: boolean) => void; + testId?: string; }; -export const VuiTableRowActions = ({ row, actions, onToggle }: Props) => { +export const VuiTableRowActions = ({ row, actions, onToggle, testId }: Props) => { const [isOpen, setIsOpen] = useState(false); // Filter out disabled actions. const actionOptions = actions.reduce((acc, action) => { - const { label, isDisabled, onClick, href } = action; + const { label, isDisabled, onClick, href, testId } = action; if (!isDisabled?.(row)) { - acc.push({ label, onClick, href: href?.(row), value: row }); + acc.push({ label, onClick, href: href?.(row), value: row, testId }); } return acc; }, [] as any); @@ -51,6 +53,7 @@ export const VuiTableRowActions = ({ row, actions, onToggle }: Pr } + data-testid={testId} /> } >