|
| 1 | +import React from 'react'; |
| 2 | +import { render, screen, fireEvent } from '@testing-library/react'; |
| 3 | +import '@testing-library/jest-dom'; |
| 4 | +import { Actions, EnableState, StatusColumn, TableBase } from '../../components/Tables/TableBase'; |
| 5 | + |
| 6 | +jest.mock('../../components/ComponentsLib', () => ({ |
| 7 | + Checkbox: ({ isChecked, checkHandler }) => ( |
| 8 | + <input |
| 9 | + type="checkbox" |
| 10 | + checked={isChecked} |
| 11 | + onChange={checkHandler} |
| 12 | + data-testid="checkbox" |
| 13 | + /> |
| 14 | + ), |
| 15 | + AddButton: ({ disabled, onClick }) => ( |
| 16 | + <button disabled={disabled} onClick={onClick} data-testid="add-button">Add</button> |
| 17 | + ), |
| 18 | +})); |
| 19 | + |
| 20 | +describe('Actions Component', () => { |
| 21 | + test('renders edit and delete buttons and triggers click events', () => { |
| 22 | + const onEditClick = jest.fn(); |
| 23 | + const onDeleteClick = jest.fn(); |
| 24 | + |
| 25 | + const { container } = render(<Actions onEditClick={onEditClick} onDeleteClick={onDeleteClick} />); |
| 26 | + |
| 27 | + const editButton = container.querySelector('.edit'); |
| 28 | + const deleteButton = container.querySelector('.delete'); |
| 29 | + |
| 30 | + fireEvent.click(editButton); |
| 31 | + fireEvent.click(deleteButton); |
| 32 | + |
| 33 | + expect(onEditClick).toHaveBeenCalledTimes(1); |
| 34 | + expect(onDeleteClick).toHaveBeenCalledTimes(1); |
| 35 | + }); |
| 36 | +}); |
| 37 | + |
| 38 | +describe('EnableState Component', () => { |
| 39 | + test('renders checkbox with correct state and triggers change event', () => { |
| 40 | + const checkHandler = jest.fn(); |
| 41 | + |
| 42 | + render(<EnableState isChecked={true} checkHandler={checkHandler} />); |
| 43 | + |
| 44 | + const checkbox = screen.getByTestId('checkbox'); |
| 45 | + expect(checkbox.checked).toBe(true); |
| 46 | + |
| 47 | + fireEvent.click(checkbox); |
| 48 | + expect(checkHandler).toHaveBeenCalledTimes(1); |
| 49 | + }); |
| 50 | +}); |
| 51 | + |
| 52 | +describe('StatusColumn Component', () => { |
| 53 | + test('renders "done" icon when there are no messages', () => { |
| 54 | + const { container } = render(<StatusColumn messages={[]} />); |
| 55 | + |
| 56 | + const doneIcon = container.querySelector('svg'); |
| 57 | + expect(doneIcon).toBeInTheDocument(); |
| 58 | + |
| 59 | + const checkmarkPath = doneIcon.querySelectorAll('path')[1]; |
| 60 | + expect(checkmarkPath.getAttribute('d')).toBe('M9 16.2 4.8 12l-1.4 1.4L9 19 21 7l-1.4-1.4L9 16.2z'); |
| 61 | + }); |
| 62 | +}); |
| 63 | + |
| 64 | +describe('TableBase Component', () => { |
| 65 | + test('renders table with headers and calls onClick when AddButton is clicked', () => { |
| 66 | + const onClick = jest.fn(); |
| 67 | + const headers = ['Header 1', 'Header 2']; |
| 68 | + |
| 69 | + render(<TableBase header={headers} onClick={onClick} hideAddBtn={false} disabled={false} />); |
| 70 | + |
| 71 | + const header1 = screen.getByText(/Header 1/i); |
| 72 | + const header2 = screen.getByText(/Header 2/i); |
| 73 | + const addButton = screen.getByTestId('add-button'); |
| 74 | + |
| 75 | + expect(header1).toBeInTheDocument(); |
| 76 | + expect(header2).toBeInTheDocument(); |
| 77 | + |
| 78 | + fireEvent.click(addButton); |
| 79 | + expect(onClick).toHaveBeenCalledTimes(1); |
| 80 | + }); |
| 81 | + |
| 82 | + test('does not render "Add" button when hideAddBtn is true', () => { |
| 83 | + const onClick = jest.fn(); |
| 84 | + const headers = ['Header 1', 'Header 2']; |
| 85 | + |
| 86 | + render(<TableBase header={headers} onClick={onClick} hideAddBtn={true} disabled={false} />); |
| 87 | + |
| 88 | + const addButton = screen.queryByTestId('add-button'); |
| 89 | + expect(addButton).not.toBeInTheDocument(); |
| 90 | + }); |
| 91 | +}); |
0 commit comments