|
| 1 | +import React from 'react'; |
| 2 | +import { render, screen, fireEvent } from '@testing-library/react'; |
| 3 | +import { describe, it, expect, vi } from 'vitest'; |
| 4 | +import { TabNavigation, TabItem } from './TabNavigation'; |
| 5 | +import { ThemeProvider } from '@mui/material/styles'; |
| 6 | +import { appTheme } from '@root/src/theme'; |
| 7 | + |
| 8 | +describe('TabNavigation', () => { |
| 9 | + const mockTabs: TabItem[] = [ |
| 10 | + { id: 'tab1', label: 'First Tab', disabled: false }, |
| 11 | + { id: 'tab2', label: 'Second Tab', disabled: false }, |
| 12 | + { id: 'tab3', label: 'Third Tab', disabled: true }, |
| 13 | + ]; |
| 14 | + |
| 15 | + const mockOnChange = vi.fn(); |
| 16 | + |
| 17 | + const renderWithTheme = (component: React.ReactElement) => { |
| 18 | + return render(<ThemeProvider theme={appTheme}>{component}</ThemeProvider>); |
| 19 | + }; |
| 20 | + |
| 21 | + afterEach(() => { |
| 22 | + vi.clearAllMocks(); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should render all tabs with correct labels', () => { |
| 26 | + renderWithTheme(<TabNavigation tabs={mockTabs} value={0} onChange={mockOnChange} />); |
| 27 | + |
| 28 | + expect(screen.getByText('First Tab')).toBeInTheDocument(); |
| 29 | + expect(screen.getByText('Second Tab')).toBeInTheDocument(); |
| 30 | + expect(screen.getByText('Third Tab')).toBeInTheDocument(); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should call onChange when a tab is clicked', () => { |
| 34 | + renderWithTheme(<TabNavigation tabs={mockTabs} value={0} onChange={mockOnChange} />); |
| 35 | + |
| 36 | + const secondTab = screen.getByText('Second Tab'); |
| 37 | + fireEvent.click(secondTab); |
| 38 | + |
| 39 | + expect(mockOnChange).toHaveBeenCalledTimes(1); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should mark the correct tab as selected', () => { |
| 43 | + renderWithTheme(<TabNavigation tabs={mockTabs} value={1} onChange={mockOnChange} />); |
| 44 | + |
| 45 | + const secondTab = screen.getByText('Second Tab'); |
| 46 | + expect(secondTab).toHaveClass('Mui-selected'); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should disable tabs correctly', () => { |
| 50 | + renderWithTheme(<TabNavigation tabs={mockTabs} value={0} onChange={mockOnChange} />); |
| 51 | + |
| 52 | + const thirdTab = screen.getByText('Third Tab'); |
| 53 | + expect(thirdTab).toHaveClass('Mui-disabled'); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should not call onChange when a disabled tab is clicked', () => { |
| 57 | + renderWithTheme(<TabNavigation tabs={mockTabs} value={0} onChange={mockOnChange} />); |
| 58 | + |
| 59 | + const disabledTab = screen.getByText('Third Tab'); |
| 60 | + fireEvent.click(disabledTab); |
| 61 | + |
| 62 | + expect(mockOnChange).not.toHaveBeenCalled(); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should render with no tabs', () => { |
| 66 | + const { container } = renderWithTheme(<TabNavigation tabs={[]} value={0} onChange={mockOnChange} />); |
| 67 | + |
| 68 | + expect(container.querySelector('.MuiTabs-root')).toBeInTheDocument(); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should apply correct IDs to tabs', () => { |
| 72 | + renderWithTheme(<TabNavigation tabs={mockTabs} value={0} onChange={mockOnChange} />); |
| 73 | + |
| 74 | + const firstTab = screen.getByText('First Tab'); |
| 75 | + expect(firstTab).toHaveAttribute('id', 'tab1'); |
| 76 | + |
| 77 | + const secondTab = screen.getByText('Second Tab'); |
| 78 | + expect(secondTab).toHaveAttribute('id', 'tab2'); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should handle tab changes between different values', () => { |
| 82 | + const { rerender } = renderWithTheme(<TabNavigation tabs={mockTabs} value={0} onChange={mockOnChange} />); |
| 83 | + |
| 84 | + let firstTab = screen.getByText('First Tab'); |
| 85 | + expect(firstTab).toHaveClass('Mui-selected'); |
| 86 | + |
| 87 | + rerender( |
| 88 | + <ThemeProvider theme={appTheme}> |
| 89 | + <TabNavigation tabs={mockTabs} value={1} onChange={mockOnChange} /> |
| 90 | + </ThemeProvider>, |
| 91 | + ); |
| 92 | + |
| 93 | + const secondTab = screen.getByText('Second Tab'); |
| 94 | + expect(secondTab).toHaveClass('Mui-selected'); |
| 95 | + |
| 96 | + firstTab = screen.getByText('First Tab'); |
| 97 | + expect(firstTab).not.toHaveClass('Mui-selected'); |
| 98 | + }); |
| 99 | + |
| 100 | + it('should use default disabled value as false when not provided', () => { |
| 101 | + const tabsWithoutDisabled: TabItem[] = [{ id: 'tab1', label: 'Tab 1' }]; |
| 102 | + |
| 103 | + renderWithTheme(<TabNavigation tabs={tabsWithoutDisabled} value={0} onChange={mockOnChange} />); |
| 104 | + |
| 105 | + const tab = screen.getByText('Tab 1'); |
| 106 | + expect(tab).not.toHaveClass('Mui-disabled'); |
| 107 | + }); |
| 108 | + |
| 109 | + it('should render tabs with proper styling structure', () => { |
| 110 | + const { container } = renderWithTheme(<TabNavigation tabs={mockTabs} value={0} onChange={mockOnChange} />); |
| 111 | + |
| 112 | + // Check for MUI Tabs component |
| 113 | + expect(container.querySelector('.MuiTabs-root')).toBeInTheDocument(); |
| 114 | + |
| 115 | + // Check for MUI Tab components |
| 116 | + const tabs = container.querySelectorAll('.MuiTab-root'); |
| 117 | + expect(tabs).toHaveLength(3); |
| 118 | + }); |
| 119 | + |
| 120 | + it('should handle single tab', () => { |
| 121 | + const singleTab: TabItem[] = [{ id: 'only-tab', label: 'Only Tab', disabled: false }]; |
| 122 | + |
| 123 | + renderWithTheme(<TabNavigation tabs={singleTab} value={0} onChange={mockOnChange} />); |
| 124 | + |
| 125 | + expect(screen.getByText('Only Tab')).toBeInTheDocument(); |
| 126 | + expect(screen.getByText('Only Tab')).toHaveClass('Mui-selected'); |
| 127 | + }); |
| 128 | +}); |
0 commit comments