|
| 1 | +/** |
| 2 | + * ObjectUI |
| 3 | + * Copyright (c) 2024-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +import { describe, it, expect, vi } from 'vitest'; |
| 10 | +import { render, screen, fireEvent } from '@testing-library/react'; |
| 11 | +import { ConfigRow } from '../custom/config-row'; |
| 12 | +import { SectionHeader } from '../custom/section-header'; |
| 13 | + |
| 14 | +describe('ConfigRow', () => { |
| 15 | + it('should render label and value', () => { |
| 16 | + render(<ConfigRow label="Color" value="Blue" />); |
| 17 | + expect(screen.getByText('Color')).toBeDefined(); |
| 18 | + expect(screen.getByText('Blue')).toBeDefined(); |
| 19 | + }); |
| 20 | + |
| 21 | + it('should render as div when not clickable', () => { |
| 22 | + const { container } = render(<ConfigRow label="Label" value="Value" />); |
| 23 | + expect(container.querySelector('button')).toBeNull(); |
| 24 | + expect(container.querySelector('div')).not.toBeNull(); |
| 25 | + }); |
| 26 | + |
| 27 | + it('should render as button when onClick is provided', () => { |
| 28 | + const onClick = vi.fn(); |
| 29 | + const { container } = render(<ConfigRow label="Label" value="Value" onClick={onClick} />); |
| 30 | + const button = container.querySelector('button'); |
| 31 | + expect(button).not.toBeNull(); |
| 32 | + fireEvent.click(button!); |
| 33 | + expect(onClick).toHaveBeenCalledTimes(1); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should render children instead of value when provided', () => { |
| 37 | + render( |
| 38 | + <ConfigRow label="Custom"> |
| 39 | + <span data-testid="custom-child">Custom Content</span> |
| 40 | + </ConfigRow>, |
| 41 | + ); |
| 42 | + expect(screen.getByTestId('custom-child')).toBeDefined(); |
| 43 | + expect(screen.getByText('Custom Content')).toBeDefined(); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should apply custom className', () => { |
| 47 | + const { container } = render(<ConfigRow label="Label" className="custom-class" />); |
| 48 | + expect(container.firstElementChild?.className).toContain('custom-class'); |
| 49 | + }); |
| 50 | +}); |
| 51 | + |
| 52 | +describe('SectionHeader', () => { |
| 53 | + it('should render title text', () => { |
| 54 | + render(<SectionHeader title="Data" />); |
| 55 | + expect(screen.getByText('Data')).toBeDefined(); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should render as div when not collapsible', () => { |
| 59 | + const { container } = render(<SectionHeader title="Data" testId="section" />); |
| 60 | + const element = screen.getByTestId('section'); |
| 61 | + expect(element.tagName).toBe('DIV'); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should render as button when collapsible', () => { |
| 65 | + render(<SectionHeader title="Data" collapsible testId="section" />); |
| 66 | + const element = screen.getByTestId('section'); |
| 67 | + expect(element.tagName).toBe('BUTTON'); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should set aria-expanded when collapsible', () => { |
| 71 | + render(<SectionHeader title="Data" collapsible collapsed={false} testId="section" />); |
| 72 | + const element = screen.getByTestId('section'); |
| 73 | + expect(element.getAttribute('aria-expanded')).toBe('true'); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should set aria-expanded=false when collapsed', () => { |
| 77 | + render(<SectionHeader title="Data" collapsible collapsed testId="section" />); |
| 78 | + const element = screen.getByTestId('section'); |
| 79 | + expect(element.getAttribute('aria-expanded')).toBe('false'); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should call onToggle when collapsible button is clicked', () => { |
| 83 | + const onToggle = vi.fn(); |
| 84 | + render(<SectionHeader title="Data" collapsible onToggle={onToggle} testId="section" />); |
| 85 | + fireEvent.click(screen.getByTestId('section')); |
| 86 | + expect(onToggle).toHaveBeenCalledTimes(1); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should apply custom className', () => { |
| 90 | + render(<SectionHeader title="Data" collapsible className="custom-class" testId="section" />); |
| 91 | + const element = screen.getByTestId('section'); |
| 92 | + expect(element.className).toContain('custom-class'); |
| 93 | + }); |
| 94 | +}); |
0 commit comments