// Licensed to Cloudera, Inc. under one // or more contributor license agreements. See the NOTICE file // distributed with this work for additional information // regarding copyright ownership. Cloudera, Inc. licenses this file // to you under the Apache License, Version 2.0 (the // "License"); you may not use this file except in compliance // with the License. You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. import React from 'react'; import { render, fireEvent, waitFor } from '@testing-library/react'; import Table from './PaginatedTable'; import { ColumnProps } from 'antd/lib/table'; import '@testing-library/jest-dom'; interface TestData { id: string; name: string; age: number; } describe('Table', () => { const mockData: TestData[] = [ { id: '1', name: 'John', age: 25 }, { id: '2', name: 'Jane', age: 30 } ]; const mockColumns: ColumnProps[] = [ { title: 'Name', dataIndex: 'name', key: 'name', sorter: true }, { title: 'Age', dataIndex: 'age', key: 'age', sorter: false } ]; const defaultProps = { data: mockData, columns: mockColumns, rowKey: (record: TestData) => record.id, testId: 'test-table' }; beforeEach(() => { jest.clearAllMocks(); }); it('renders table with data', () => { const { getByTestId, getByText } = render(); waitFor(() => { expect(getByTestId('test-table')).toBeInTheDocument(); expect(getByText('John')).toBeInTheDocument(); expect(getByText('30')).toBeInTheDocument(); }); }); it('handles row selection', () => { const onRowSelect = jest.fn(); const { container } = render(
); const checkboxes = container.querySelectorAll('input[type="checkbox"]'); fireEvent.click(checkboxes[1]); // First data row checkbox expect(onRowSelect).toHaveBeenCalledWith([mockData[0]]); }); it('handles row click', () => { const mockClick = jest.fn(); const handleRowClick = jest.fn().mockReturnValue({ onClick: mockClick }); const { getByText } = render(
); const row = getByText('John').closest('tr'); fireEvent.click(row as HTMLElement); expect(handleRowClick).toHaveBeenCalled(); expect(handleRowClick.mock.calls[0][0]).toEqual(mockData[0]); expect(mockClick).toHaveBeenCalled(); }); it('renders pagination when provided with valid stats', () => { const { container } = render(
); const pagination = container.querySelector('.hue-pagination'); expect(pagination).toBeInTheDocument(); }); it('does not render pagination when totalPages is 0', () => { const { container } = render(
); const pagination = container.querySelector('.hue-pagination'); expect(pagination).not.toBeInTheDocument(); }); });