|
| 1 | +import React from 'react'; |
| 2 | +import { IntlProvider } from '@edx/frontend-platform/i18n'; |
| 3 | +import { fireEvent, render, screen } from '@testing-library/react'; |
| 4 | + |
| 5 | +import TableBody from './TableBody'; |
| 6 | +import { TreeTableContext } from './TreeTableContext'; |
| 7 | + |
| 8 | +jest.mock('./CreateRow', () => () => ( |
| 9 | + <tr data-testid="create-row"> |
| 10 | + <td>Create row</td> |
| 11 | + </tr> |
| 12 | +)); |
| 13 | + |
| 14 | +jest.mock('./EditRow', () => ({ |
| 15 | + initialValue, |
| 16 | + handleUpdateRow, |
| 17 | + cancelEditRow, |
| 18 | +}: { |
| 19 | + initialValue: string; |
| 20 | + handleUpdateRow: (value: string) => void; |
| 21 | + cancelEditRow: () => void; |
| 22 | +}) => ( |
| 23 | + <tr data-testid="edit-row"> |
| 24 | + <td>{initialValue}</td> |
| 25 | + <td> |
| 26 | + <button onClick={() => handleUpdateRow('updated root')}>save edit</button> |
| 27 | + </td> |
| 28 | + <td> |
| 29 | + <button onClick={cancelEditRow}>cancel edit</button> |
| 30 | + </td> |
| 31 | + </tr> |
| 32 | +)); |
| 33 | + |
| 34 | +jest.mock('./NestedRows', () => ({ |
| 35 | + parentRowValue, |
| 36 | + isCreating, |
| 37 | + onSaveNewChildRow, |
| 38 | + onCancelCreation, |
| 39 | +}: { |
| 40 | + parentRowValue: string; |
| 41 | + isCreating: boolean; |
| 42 | + onSaveNewChildRow: (value: string, parentRowValue?: string) => void; |
| 43 | + onCancelCreation: () => void; |
| 44 | +}) => ( |
| 45 | + <tr data-testid={`nested-rows-${parentRowValue}`}> |
| 46 | + <td>{parentRowValue}</td> |
| 47 | + <td>{String(isCreating)}</td> |
| 48 | + <td> |
| 49 | + <button onClick={() => onSaveNewChildRow('new child', parentRowValue)}>save child</button> |
| 50 | + </td> |
| 51 | + <td> |
| 52 | + <button onClick={onCancelCreation}>cancel child</button> |
| 53 | + </td> |
| 54 | + </tr> |
| 55 | +)); |
| 56 | + |
| 57 | +const wrapper = ({ children }: { children: React.ReactNode; }) => ( |
| 58 | + <IntlProvider locale="en" messages={{}}>{children}</IntlProvider> |
| 59 | +); |
| 60 | + |
| 61 | +const baseContextValue = (): any => ({ |
| 62 | + treeData: [], |
| 63 | + columns: [{ accessorKey: 'value', header: 'Tag name', cell: () => 'unused' }], |
| 64 | + pageCount: -1, |
| 65 | + pagination: { pageIndex: 0, pageSize: 10 }, |
| 66 | + handlePaginationChange: jest.fn(), |
| 67 | + isLoading: false, |
| 68 | + isCreatingTopRow: false, |
| 69 | + draftError: '', |
| 70 | + createRowMutation: {}, |
| 71 | + updateRowMutation: {}, |
| 72 | + toast: { show: false, message: '', variant: 'success' as const }, |
| 73 | + setToast: jest.fn(), |
| 74 | + setIsCreatingTopRow: jest.fn(), |
| 75 | + exitDraftWithoutSave: jest.fn(), |
| 76 | + handleCreateRow: jest.fn(), |
| 77 | + creatingParentId: null, |
| 78 | + setCreatingParentId: jest.fn(), |
| 79 | + setDraftError: jest.fn(), |
| 80 | + validate: jest.fn(() => true), |
| 81 | + handleUpdateRow: jest.fn(), |
| 82 | + editingRowId: null, |
| 83 | + setEditingRowId: jest.fn(), |
| 84 | + table: null, |
| 85 | +}); |
| 86 | + |
| 87 | +const makeCell = (id: string, content: string) => ({ |
| 88 | + id, |
| 89 | + column: { columnDef: { cell: () => content } }, |
| 90 | + getContext: () => ({}), |
| 91 | +}); |
| 92 | + |
| 93 | +const makeRow = ({ |
| 94 | + id, |
| 95 | + value, |
| 96 | + depth = 0, |
| 97 | + subRows = [], |
| 98 | +}: { |
| 99 | + id: number; |
| 100 | + value: string; |
| 101 | + depth?: number; |
| 102 | + subRows?: any[]; |
| 103 | +}) => ({ |
| 104 | + id: String(id), |
| 105 | + depth, |
| 106 | + original: { id, value }, |
| 107 | + subRows, |
| 108 | + getVisibleCells: () => [makeCell(`${id}-cell`, `${value} cell`)], |
| 109 | +}); |
| 110 | + |
| 111 | +const renderTableBody = (contextValue = baseContextValue()) => render( |
| 112 | + <TreeTableContext.Provider value={contextValue as any}> |
| 113 | + <table> |
| 114 | + <TableBody /> |
| 115 | + </table> |
| 116 | + </TreeTableContext.Provider>, |
| 117 | + { wrapper }, |
| 118 | +); |
| 119 | + |
| 120 | +describe('TableBody', () => { |
| 121 | + it('returns null when no table instance is available in context', () => { |
| 122 | + const { container } = render( |
| 123 | + <table> |
| 124 | + <TableBody /> |
| 125 | + </table>, |
| 126 | + { wrapper }, |
| 127 | + ); |
| 128 | + |
| 129 | + expect(container.querySelector('tbody')).toBeNull(); |
| 130 | + }); |
| 131 | + |
| 132 | + it('shows an empty-state row when the table has no rows', () => { |
| 133 | + const contextValue = baseContextValue(); |
| 134 | + contextValue.table = { |
| 135 | + getRowModel: () => ({ rows: [] }), |
| 136 | + }; |
| 137 | + |
| 138 | + renderTableBody(contextValue); |
| 139 | + |
| 140 | + expect(screen.getByText('No results found')).toBeInTheDocument(); |
| 141 | + }); |
| 142 | + |
| 143 | + it('shows a loading row when table data is still loading', () => { |
| 144 | + const contextValue = baseContextValue(); |
| 145 | + contextValue.isLoading = true; |
| 146 | + contextValue.table = { |
| 147 | + getRowModel: () => ({ rows: [] }), |
| 148 | + }; |
| 149 | + |
| 150 | + renderTableBody(contextValue); |
| 151 | + |
| 152 | + expect(screen.getByRole('status')).toBeInTheDocument(); |
| 153 | + }); |
| 154 | + |
| 155 | + it('renders top-level creation and nested-row callbacks from context', () => { |
| 156 | + const contextValue = baseContextValue(); |
| 157 | + const rootRow = makeRow({ id: 1, value: 'root tag' }); |
| 158 | + |
| 159 | + contextValue.isCreatingTopRow = true; |
| 160 | + contextValue.creatingParentId = 1; |
| 161 | + contextValue.table = { |
| 162 | + getRowModel: () => ({ rows: [rootRow] }), |
| 163 | + }; |
| 164 | + |
| 165 | + renderTableBody(contextValue); |
| 166 | + |
| 167 | + expect(screen.getByTestId('create-row')).toBeInTheDocument(); |
| 168 | + expect(screen.getByText('root tag cell')).toBeInTheDocument(); |
| 169 | + expect(screen.getByTestId('nested-rows-root tag')).toHaveTextContent('true'); |
| 170 | + |
| 171 | + fireEvent.click(screen.getByRole('button', { name: 'save child' })); |
| 172 | + expect(contextValue.handleCreateRow).toHaveBeenCalledWith('new child', 'root tag'); |
| 173 | + |
| 174 | + fireEvent.click(screen.getByRole('button', { name: 'cancel child' })); |
| 175 | + expect(contextValue.setDraftError).toHaveBeenCalledWith(''); |
| 176 | + expect(contextValue.setCreatingParentId).toHaveBeenCalledWith(null); |
| 177 | + expect(contextValue.exitDraftWithoutSave).toHaveBeenCalled(); |
| 178 | + }); |
| 179 | + |
| 180 | + it('renders edit mode for the matching row and wires save and cancel through context', () => { |
| 181 | + const contextValue = baseContextValue(); |
| 182 | + const rootRow = makeRow({ id: 1, value: 'root tag' }); |
| 183 | + |
| 184 | + contextValue.editingRowId = '1:root tag'; |
| 185 | + contextValue.table = { |
| 186 | + getRowModel: () => ({ rows: [rootRow] }), |
| 187 | + }; |
| 188 | + |
| 189 | + renderTableBody(contextValue); |
| 190 | + |
| 191 | + expect(screen.getByTestId('edit-row')).toBeInTheDocument(); |
| 192 | + |
| 193 | + fireEvent.click(screen.getByRole('button', { name: 'save edit' })); |
| 194 | + expect(contextValue.handleUpdateRow).toHaveBeenCalledWith('updated root', 'root tag'); |
| 195 | + |
| 196 | + fireEvent.click(screen.getByRole('button', { name: 'cancel edit' })); |
| 197 | + expect(contextValue.setEditingRowId).toHaveBeenCalledWith(null); |
| 198 | + expect(contextValue.exitDraftWithoutSave).toHaveBeenCalled(); |
| 199 | + }); |
| 200 | +}); |
0 commit comments