Skip to content

Commit 24a7dff

Browse files
authored
fix(ColumnManagementModal): update reset to default behavior (#868)
1 parent 08aaf99 commit 24a7dff

File tree

2 files changed

+77
-3
lines changed

2 files changed

+77
-3
lines changed

packages/module/src/ColumnManagementModal/ColumnManagementModal.test.tsx

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,40 @@ describe('ColumnManagementModal component', () => {
8080
fireEvent.click(screen.getByText('Score'));
8181

8282
fireEvent.click(screen.getByText('Reset to default'));
83-
83+
8484
expect(getCheckboxesState()).toEqual(DEFAULT_COLUMNS.map(c => c.isShownByDefault));
8585
});
8686

87+
it('should call onReset callback when reset to default is clicked', () => {
88+
const onResetMock = jest.fn();
89+
render(<ColumnManagementModal
90+
appliedColumns={DEFAULT_COLUMNS}
91+
applyColumns={setColumns}
92+
isOpen
93+
onClose={onClose}
94+
onReset={onResetMock}
95+
/>);
96+
97+
const resetButtons = screen.getAllByText('Reset to default');
98+
// Click the last one rendered (the new modal)
99+
fireEvent.click(resetButtons[resetButtons.length - 1]);
100+
101+
expect(onResetMock).toHaveBeenCalledTimes(1);
102+
});
103+
104+
it('should display custom reset button label', () => {
105+
const customLabel = 'Restaurer par défaut';
106+
render(<ColumnManagementModal
107+
appliedColumns={DEFAULT_COLUMNS}
108+
applyColumns={setColumns}
109+
isOpen
110+
onClose={onClose}
111+
resetToDefaultLabel={customLabel}
112+
/>);
113+
114+
expect(screen.getByText(customLabel)).toBeInTheDocument();
115+
});
116+
87117
it('should set all columns to show upon clicking on "Select all"', async () => {
88118
// disable Impact column which is enabled by default
89119
fireEvent.click(screen.getByText('Impact'));
@@ -138,4 +168,40 @@ describe('ColumnManagementModal component', () => {
138168
expect(screen.getByTestId('drag-drop-sort')).toBeInTheDocument();
139169
});
140170
});
171+
172+
describe('reset functionality', () => {
173+
it('should reset column order to original when reset to default is clicked', () => {
174+
const reorderedColumns = [
175+
DEFAULT_COLUMNS[3], // Score (last -> first)
176+
DEFAULT_COLUMNS[0], // ID
177+
DEFAULT_COLUMNS[2], // Impact
178+
DEFAULT_COLUMNS[1], // Publish date
179+
];
180+
181+
const applyColumnsMock = jest.fn();
182+
render(<ColumnManagementModal
183+
appliedColumns={reorderedColumns}
184+
applyColumns={applyColumnsMock}
185+
isOpen
186+
onClose={onClose}
187+
/>);
188+
189+
// Click reset to default - get all buttons and click the last one
190+
const resetButtons = screen.getAllByText('Reset to default');
191+
fireEvent.click(resetButtons[resetButtons.length - 1]);
192+
193+
// Click save to apply changes
194+
const saveButtons = screen.getAllByText('Save');
195+
fireEvent.click(saveButtons[saveButtons.length - 1]);
196+
197+
// Verify that the saved columns match the original reordered columns order
198+
// (after reset, it should restore the order from appliedColumns which is reorderedColumns)
199+
expect(applyColumnsMock).toHaveBeenCalledWith(
200+
reorderedColumns.map(col => ({
201+
...col,
202+
isShown: col.isShownByDefault
203+
}))
204+
);
205+
});
206+
});
141207
});

packages/module/src/ColumnManagementModal/ColumnManagementModal.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ export interface ColumnManagementModalProps extends Omit<ModalProps, 'ref' | 'ch
4040
ouiaId?: string | number;
4141
/** Enable drag and drop functionality for reordering columns */
4242
enableDragDrop?: boolean;
43+
/** Invoked when reset to default button is clicked */
44+
onReset?: () => void;
45+
/** Custom label for reset to default button */
46+
resetToDefaultLabel?: string;
4347
}
4448

4549
const ColumnManagementModal: FunctionComponent<ColumnManagementModalProps> = (
@@ -51,6 +55,8 @@ const ColumnManagementModal: FunctionComponent<ColumnManagementModalProps> = (
5155
applyColumns,
5256
ouiaId = 'ColumnManagementModal',
5357
enableDragDrop = false,
58+
onReset,
59+
resetToDefaultLabel = 'Reset to default',
5460
...props }: ColumnManagementModalProps) => {
5561

5662
const [ currentColumns, setCurrentColumns ] = useState(() =>
@@ -72,7 +78,9 @@ const ColumnManagementModal: FunctionComponent<ColumnManagementModalProps> = (
7278
}));
7379

7480
const resetToDefault = () => {
75-
setCurrentColumns(currentColumns.map(column => ({ ...column, isShown: column.isShownByDefault ?? false })));
81+
// Reset both visibility and order to match the original appliedColumns
82+
setCurrentColumns(appliedColumns.map(column => ({ ...column, isShown: column.isShownByDefault ?? false })));
83+
onReset?.();
7684
};
7785

7886
const updateColumns = (items: ListManagerItem[]) => {
@@ -131,7 +139,7 @@ const ColumnManagementModal: FunctionComponent<ColumnManagementModalProps> = (
131139
<>
132140
<Content component={ContentVariants.p}>{description}</Content>
133141
<Button isInline onClick={resetToDefault} variant={ButtonVariant.link} ouiaId={`${ouiaId}-reset-button`}>
134-
Reset to default
142+
{resetToDefaultLabel}
135143
</Button>
136144
</>
137145
}

0 commit comments

Comments
 (0)