From e5d9e67c1f23245c47b0c1239af2e22099a957b6 Mon Sep 17 00:00:00 2001 From: justinpark Date: Fri, 23 Aug 2024 08:28:05 +0900 Subject: [PATCH] Add specs for additional menu actions --- .../components/GridTable/HeaderMenu.test.tsx | 68 ++++++++++++++++++- .../src/components/GridTable/HeaderMenu.tsx | 8 ++- 2 files changed, 72 insertions(+), 4 deletions(-) diff --git a/superset-frontend/src/components/GridTable/HeaderMenu.test.tsx b/superset-frontend/src/components/GridTable/HeaderMenu.test.tsx index 0233308d0640b..3a187e4cf8684 100644 --- a/superset-frontend/src/components/GridTable/HeaderMenu.test.tsx +++ b/superset-frontend/src/components/GridTable/HeaderMenu.test.tsx @@ -89,14 +89,24 @@ const mockInvisibleColumn3 = { getDataAsCsv: jest.fn().mockReturnValue('csv'), } as any as Column; +const mockSetColDef = jest.fn(); + const mockGridApi = { + autoSizeColumn: jest.fn(), + autoSizeAllColumns: jest.fn(), setColumnPinned: jest.fn(), + getColumn: jest.fn().mockReturnValue({ + setColDef: mockSetColDef, + getColDef: jest.fn().mockReturnValue({}), + }), getColumns: jest.fn().mockReturnValue([]), getDataAsCsv: jest.fn().mockReturnValue('csv'), exportDataAsCsv: jest.fn().mockReturnValue('csv'), getAllDisplayedColumns: jest.fn().mockReturnValue([]), + setColumnsPinned: jest.fn(), setColumnsVisible: jest.fn(), setColumnVisible: jest.fn(), + moveColumns: jest.fn(), } as any as GridApi; const mockedProps = { @@ -107,13 +117,27 @@ const mockedProps = { }; afterEach(() => { + mockSetColDef.mockClear(); + (mockGridApi.getDataAsCsv as jest.Mock).mockClear(); (mockGridApi.setColumnPinned as jest.Mock).mockClear(); (mockGridApi.setColumnVisible as jest.Mock).mockClear(); + (mockGridApi.setColumnsVisible as jest.Mock).mockClear(); + (mockGridApi.setColumnsPinned as jest.Mock).mockClear(); + (mockGridApi.autoSizeColumn as jest.Mock).mockClear(); + (mockGridApi.autoSizeAllColumns as jest.Mock).mockClear(); + (mockGridApi.moveColumns as jest.Mock).mockClear(); }); -test('renders copy data', () => { - const { queryByText } = render(); - expect(queryByText('Copy')).toBeTruthy(); +test('renders copy data', async () => { + const { getByText } = render(); + fireEvent.click(getByText('Copy')); + await waitFor(() => + expect(mockGridApi.getDataAsCsv).toHaveBeenCalledTimes(1), + ); + expect(mockGridApi.getDataAsCsv).toHaveBeenCalledWith({ + columnKeys: [mockedProps.colId], + suppressQuotes: true, + }); }); test('renders buttons pinning both side', () => { @@ -153,6 +177,15 @@ test('renders unpin on pinned right', () => { expect(queryByText('Unpin')).toBeTruthy(); }); +test('renders autosize column', async () => { + const { getByText } = render(); + fireEvent.click(getByText('Autosize Column')); + await waitFor(() => + expect(mockGridApi.autoSizeColumn).toHaveBeenCalledTimes(1), + ); + expect(mockSetColDef).toHaveBeenCalledTimes(1); +}); + test('renders unhide when invisible column exists', async () => { const { queryByText } = render( , @@ -189,6 +222,14 @@ describe('for main menu', () => { }); }); + test('renders autosize column', async () => { + const { getByText } = render(); + fireEvent.click(getByText('Autosize all columns')); + await waitFor(() => + expect(mockGridApi.autoSizeAllColumns).toHaveBeenCalledTimes(1), + ); + }); + test('renders all unhide all hidden columns when multiple invisible columns exist', async () => { render( { true, ); }); + + test('reset columns configuration', async () => { + const { getByText } = render( + , + ); + fireEvent.click(getByText('Reset columns')); + await waitFor(() => + expect(mockGridApi.setColumnsVisible).toHaveBeenCalledTimes(1), + ); + expect(mockGridApi.setColumnsVisible).toHaveBeenCalledWith( + [mockInvisibleColumn], + true, + ); + expect(mockGridApi.setColumnsPinned).toHaveBeenCalledTimes(1); + expect(mockGridApi.setColumnsPinned).toHaveBeenCalledWith([], null); + expect(mockGridApi.moveColumns).toHaveBeenCalledTimes(1); + }); }); diff --git a/superset-frontend/src/components/GridTable/HeaderMenu.tsx b/superset-frontend/src/components/GridTable/HeaderMenu.tsx index ecb51dde5b7a1..38c6ab74171d6 100644 --- a/superset-frontend/src/components/GridTable/HeaderMenu.tsx +++ b/superset-frontend/src/components/GridTable/HeaderMenu.tsx @@ -223,7 +223,13 @@ const HeaderMenu: React.FC = ({ { const column = api.getColumn(colId); - column?.setColDef(column?.getColDef(), { minWidth: undefined }); + column?.setColDef( + { + ...column?.getColDef(), + minWidth: undefined, + }, + column?.getColDef(), + ); api.autoSizeColumn(colId); }} >