|
| 1 | +import React from 'react'; |
| 2 | +import { render, screen } from '@testing-library/react'; |
| 3 | +import DataList from '../DataList'; |
| 4 | + |
| 5 | +describe('DataList Component', () => { |
| 6 | + test('should render DataList components without crashing', () => { |
| 7 | + render( |
| 8 | + <DataList.Root> |
| 9 | + <DataList.Item> |
| 10 | + <DataList.Label>Name</DataList.Label> |
| 11 | + <DataList.Value>John Doe</DataList.Value> |
| 12 | + </DataList.Item> |
| 13 | + </DataList.Root> |
| 14 | + ); |
| 15 | + expect(screen.getByText('Name')).toBeInTheDocument(); |
| 16 | + expect(screen.getByText('John Doe')).toBeInTheDocument(); |
| 17 | + }); |
| 18 | + |
| 19 | + test('should apply default class names correctly', () => { |
| 20 | + const { container } = render( |
| 21 | + <DataList.Root> |
| 22 | + <DataList.Item> |
| 23 | + <DataList.Label>Email</DataList.Label> |
| 24 | + <DataList.Value>test@example.com</DataList.Value> |
| 25 | + </DataList.Item> |
| 26 | + </DataList.Root> |
| 27 | + ); |
| 28 | + |
| 29 | + const rootElement = container.firstChild; |
| 30 | + expect(rootElement).toHaveClass('rad-ui-data-list'); |
| 31 | + |
| 32 | + const itemElement = container.querySelector('.rad-ui-data-list-item'); |
| 33 | + expect(itemElement).toBeInTheDocument(); |
| 34 | + |
| 35 | + const labelElement = container.querySelector('.rad-ui-data-list-label'); |
| 36 | + expect(labelElement).toBeInTheDocument(); |
| 37 | + |
| 38 | + const valueElement = container.querySelector('.rad-ui-data-list-value'); |
| 39 | + expect(valueElement).toBeInTheDocument(); |
| 40 | + }); |
| 41 | + |
| 42 | + test('should apply custom class names correctly', () => { |
| 43 | + const { container } = render( |
| 44 | + <DataList.Root className="custom-root" customRootClass="custom-datalist"> |
| 45 | + <DataList.Item className="custom-item"> |
| 46 | + <DataList.Label className="custom-label">Phone</DataList.Label> |
| 47 | + <DataList.Value className="custom-value">123-456-7890</DataList.Value> |
| 48 | + </DataList.Item> |
| 49 | + </DataList.Root> |
| 50 | + ); |
| 51 | + |
| 52 | + const rootElement = container.firstChild; |
| 53 | + expect(rootElement).toHaveClass('custom-datalist'); |
| 54 | + expect(rootElement).toHaveClass('custom-root'); |
| 55 | + |
| 56 | + const itemElement = container.querySelector('.custom-datalist-item'); |
| 57 | + expect(itemElement).toHaveClass('custom-item'); |
| 58 | + |
| 59 | + const labelElement = container.querySelector('.custom-datalist-label'); |
| 60 | + expect(labelElement).toHaveClass('custom-label'); |
| 61 | + |
| 62 | + const valueElement = container.querySelector('.custom-datalist-value'); |
| 63 | + expect(valueElement).toHaveClass('custom-value'); |
| 64 | + }); |
| 65 | + |
| 66 | + test('should handle multiple DataList Items', () => { |
| 67 | + render( |
| 68 | + <DataList.Root> |
| 69 | + <DataList.Item> |
| 70 | + <DataList.Label>Name</DataList.Label> |
| 71 | + <DataList.Value>John Doe</DataList.Value> |
| 72 | + </DataList.Item> |
| 73 | + <DataList.Item> |
| 74 | + <DataList.Label>Email</DataList.Label> |
| 75 | + <DataList.Value>john.doe@example.com</DataList.Value> |
| 76 | + </DataList.Item> |
| 77 | + <DataList.Item> |
| 78 | + <DataList.Label>Phone</DataList.Label> |
| 79 | + <DataList.Value>123-456-7890</DataList.Value> |
| 80 | + </DataList.Item> |
| 81 | + </DataList.Root> |
| 82 | + ); |
| 83 | + |
| 84 | + expect(screen.getByText('Name')).toBeInTheDocument(); |
| 85 | + expect(screen.getByText('John Doe')).toBeInTheDocument(); |
| 86 | + expect(screen.getByText('Email')).toBeInTheDocument(); |
| 87 | + expect(screen.getByText('john.doe@example.com')).toBeInTheDocument(); |
| 88 | + expect(screen.getByText('Phone')).toBeInTheDocument(); |
| 89 | + expect(screen.getByText('123-456-7890')).toBeInTheDocument(); |
| 90 | + }); |
| 91 | + |
| 92 | + test('should warn when using DataList directly', () => { |
| 93 | + const consoleSpy = jest.spyOn(console, 'warn').mockImplementation(() => {}); |
| 94 | + render(<DataList />); |
| 95 | + |
| 96 | + expect(consoleSpy).toHaveBeenCalledWith( |
| 97 | + 'Direct usage of DataList is not supported. Please use DataList.Root, DataList.Item, etc. instead.' |
| 98 | + ); |
| 99 | + |
| 100 | + consoleSpy.mockRestore(); |
| 101 | + }); |
| 102 | +}); |
0 commit comments