Skip to content

Commit 3431722

Browse files
add tests for column resize behavior in data grid
Introduces unit tests to validate column resizing functionality, including handling of non-persisted widths, multiple resizes, and invalid column IDs. Ensures proper state persistence and edge case handling.
1 parent 9c065f2 commit 3431722

File tree

1 file changed

+131
-1
lines changed

1 file changed

+131
-1
lines changed

plugins/wazuh-fleet/public/components/common/data-grid/use-data-grid-columns.test.ts

Lines changed: 131 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ describe('useDataGridColumns', () => {
7171
}),
7272
);
7373

74-
expect(result.current.columns).toHaveLength(2);
74+
expect(result.current.columns).toHaveLength(defaultColumns.length);
7575
expect(result.current.columnVisibility.visibleColumns).toEqual(
7676
defaultColumns.map(col => col.id),
7777
);
@@ -174,6 +174,136 @@ describe('useDataGridColumns', () => {
174174
});
175175
});
176176

177+
it('should handle resize of column not in current width state', () => {
178+
const testColumnId = 'col3';
179+
const testColumnWidth = 180;
180+
181+
// Mock column widths with only some columns
182+
mockRetrieveColumnsWidthState.mockReturnValue({
183+
col1: 250,
184+
});
185+
186+
const { result } = renderHook(() =>
187+
useDataGridColumns({
188+
appId,
189+
defaultColumns,
190+
columnSchemaDefinitions,
191+
}),
192+
);
193+
194+
act(() => {
195+
result.current.onColumnResize({
196+
columnId: testColumnId,
197+
width: testColumnWidth,
198+
});
199+
});
200+
201+
// Should add the new column to the existing widths
202+
expect(mockPersistColumnsWidthState).toHaveBeenCalledWith(appId, {
203+
col1: 250,
204+
col3: testColumnWidth,
205+
});
206+
});
207+
208+
it('should initialize with empty column widths when none are persisted', () => {
209+
// Mock empty column widths
210+
mockRetrieveColumnsWidthState.mockReturnValue({});
211+
212+
const { result } = renderHook(() =>
213+
useDataGridColumns({
214+
appId,
215+
defaultColumns,
216+
columnSchemaDefinitions,
217+
}),
218+
);
219+
const testColumnId = 'col1';
220+
const testColumnWidth = 150;
221+
222+
act(() => {
223+
result.current.onColumnResize({
224+
columnId: testColumnId,
225+
width: testColumnWidth,
226+
});
227+
});
228+
229+
// Should persist only the resized column
230+
expect(mockPersistColumnsWidthState).toHaveBeenCalledWith(appId, {
231+
col1: testColumnWidth,
232+
});
233+
});
234+
235+
it('should handle multiple column resizes', () => {
236+
// Initial column widths
237+
mockRetrieveColumnsWidthState.mockReturnValue({
238+
col1: 200,
239+
col2: 200,
240+
});
241+
242+
const { result } = renderHook(() =>
243+
useDataGridColumns({
244+
appId,
245+
defaultColumns,
246+
columnSchemaDefinitions,
247+
}),
248+
);
249+
250+
// First resize
251+
act(() => {
252+
result.current.onColumnResize({
253+
columnId: 'col1',
254+
width: 250,
255+
});
256+
});
257+
258+
mockRetrieveColumnsWidthState.mockReturnValue({
259+
col1: 250,
260+
col2: 200,
261+
});
262+
263+
expect(mockPersistColumnsWidthState).toHaveBeenCalledWith(appId, {
264+
col1: 250,
265+
col2: 200,
266+
});
267+
268+
// Second resize
269+
act(() => {
270+
result.current.onColumnResize({
271+
columnId: 'col2',
272+
width: 300,
273+
});
274+
});
275+
276+
expect(mockPersistColumnsWidthState).toHaveBeenLastCalledWith(appId, {
277+
col1: 250,
278+
col2: 300,
279+
});
280+
});
281+
282+
it('should handle resize with invalid column ID', () => {
283+
mockRetrieveColumnsWidthState.mockReturnValue({
284+
col1: 200,
285+
col2: 200,
286+
});
287+
288+
const { result } = renderHook(() =>
289+
useDataGridColumns({
290+
appId,
291+
defaultColumns,
292+
columnSchemaDefinitions,
293+
}),
294+
);
295+
296+
// Resize with invalid column ID
297+
act(() => {
298+
result.current.onColumnResize({
299+
columnId: 'nonexistent',
300+
width: 300,
301+
});
302+
});
303+
304+
expect(mockPersistColumnsWidthState).toHaveBeenCalledTimes(0);
305+
});
306+
177307
it('should handle missing columns gracefully', () => {
178308
// Constants for readability
179309
const validColumnId = 'col1';

0 commit comments

Comments
 (0)