|
| 1 | +import React from 'react'; |
| 2 | +import { describe, it, expect, render, vi, fireEvent, beforeEach } from '@test/utils'; |
| 3 | +import { ActionSheetList } from '../ActionSheetList'; |
| 4 | + |
| 5 | +describe('ActionSheetList', () => { |
| 6 | + const mockOnSelected = vi.fn(); |
| 7 | + |
| 8 | + beforeEach(() => { |
| 9 | + vi.clearAllMocks(); |
| 10 | + }); |
| 11 | + |
| 12 | + describe('props', () => { |
| 13 | + it(':items - should render string items', () => { |
| 14 | + const items = ['选项一', '选项二', '选项三']; |
| 15 | + const { queryByText } = render(<ActionSheetList items={items} onSelected={mockOnSelected} />); |
| 16 | + |
| 17 | + expect(queryByText('选项一')).toBeInTheDocument(); |
| 18 | + expect(queryByText('选项二')).toBeInTheDocument(); |
| 19 | + expect(queryByText('选项三')).toBeInTheDocument(); |
| 20 | + }); |
| 21 | + |
| 22 | + it(':items - should render object items', () => { |
| 23 | + const items = [ |
| 24 | + { label: '选项一', color: '#ff0000' }, |
| 25 | + { label: '选项二', color: '#00ff00', disabled: true }, |
| 26 | + ]; |
| 27 | + const { queryByText } = render(<ActionSheetList items={items} onSelected={mockOnSelected} />); |
| 28 | + |
| 29 | + expect(queryByText('选项一')).toBeInTheDocument(); |
| 30 | + expect(queryByText('选项二')).toBeInTheDocument(); |
| 31 | + }); |
| 32 | + |
| 33 | + it(':items - should render items with badges', () => { |
| 34 | + const items = [ |
| 35 | + { |
| 36 | + label: '带徽标选项', |
| 37 | + badge: { count: 5, dot: false }, |
| 38 | + }, |
| 39 | + { |
| 40 | + label: '红点选项', |
| 41 | + badge: { dot: true }, |
| 42 | + }, |
| 43 | + ]; |
| 44 | + const { queryByText } = render(<ActionSheetList items={items} onSelected={mockOnSelected} />); |
| 45 | + |
| 46 | + expect(queryByText('带徽标选项')).toBeInTheDocument(); |
| 47 | + expect(queryByText('红点选项')).toBeInTheDocument(); |
| 48 | + }); |
| 49 | + |
| 50 | + it(':items - should render items with icons', () => { |
| 51 | + const MockIcon = () => <span data-testid="mock-icon">Icon</span>; |
| 52 | + const items = [{ label: '带图标选项', icon: <MockIcon /> }]; |
| 53 | + const { queryByText, queryByTestId } = render(<ActionSheetList items={items} onSelected={mockOnSelected} />); |
| 54 | + |
| 55 | + expect(queryByText('带图标选项')).toBeInTheDocument(); |
| 56 | + expect(queryByTestId('mock-icon')).toBeInTheDocument(); |
| 57 | + }); |
| 58 | + |
| 59 | + it(':align - should apply left alignment', () => { |
| 60 | + const items = ['选项一']; |
| 61 | + const { container } = render(<ActionSheetList items={items} align="left" onSelected={mockOnSelected} />); |
| 62 | + |
| 63 | + expect(container.querySelector('.t-action-sheet__list-item--left')).toBeInTheDocument(); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should handle empty items array', () => { |
| 67 | + const { container } = render(<ActionSheetList items={[]} onSelected={mockOnSelected} />); |
| 68 | + |
| 69 | + const list = container.querySelector('.t-action-sheet__list'); |
| 70 | + expect(list).toBeInTheDocument(); |
| 71 | + expect(list?.children).toHaveLength(0); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should handle undefined items', () => { |
| 75 | + const { container } = render(<ActionSheetList onSelected={mockOnSelected} />); |
| 76 | + |
| 77 | + const list = container.querySelector('.t-action-sheet__list'); |
| 78 | + expect(list).toBeInTheDocument(); |
| 79 | + expect(list?.children).toHaveLength(0); |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + describe('events', () => { |
| 84 | + it(':onSelected - should call onSelected when item is clicked', () => { |
| 85 | + const items = ['选项一', '选项二']; |
| 86 | + const { container } = render(<ActionSheetList items={items} onSelected={mockOnSelected} />); |
| 87 | + |
| 88 | + const buttons = container.querySelectorAll('.t-action-sheet__list-item'); |
| 89 | + fireEvent.click(buttons[0]); |
| 90 | + expect(mockOnSelected).toHaveBeenCalledWith(0); |
| 91 | + |
| 92 | + fireEvent.click(buttons[1]); |
| 93 | + expect(mockOnSelected).toHaveBeenCalledWith(1); |
| 94 | + }); |
| 95 | + |
| 96 | + it(':onSelected - should not call onSelected when disabled item is clicked', () => { |
| 97 | + const items = [ |
| 98 | + { label: '正常选项', disabled: false }, |
| 99 | + { label: '禁用选项', disabled: true }, |
| 100 | + ]; |
| 101 | + const { container } = render(<ActionSheetList items={items} onSelected={mockOnSelected} />); |
| 102 | + |
| 103 | + const buttons = container.querySelectorAll('.t-action-sheet__list-item'); |
| 104 | + |
| 105 | + // Disabled button should not trigger callback |
| 106 | + fireEvent.click(buttons[1]); |
| 107 | + expect(mockOnSelected).not.toHaveBeenCalled(); |
| 108 | + |
| 109 | + // Normal button should trigger callback |
| 110 | + fireEvent.click(buttons[0]); |
| 111 | + expect(mockOnSelected).toHaveBeenCalledWith(0); |
| 112 | + }); |
| 113 | + |
| 114 | + it('should handle onSelected not provided', () => { |
| 115 | + const items = ['选项一']; |
| 116 | + const { container } = render(<ActionSheetList items={items} />); |
| 117 | + |
| 118 | + expect(() => { |
| 119 | + const button = container.querySelector('.t-action-sheet__list-item'); |
| 120 | + if (button) { |
| 121 | + fireEvent.click(button); |
| 122 | + } |
| 123 | + }).not.toThrow(); |
| 124 | + }); |
| 125 | + }); |
| 126 | + |
| 127 | + describe('styling', () => { |
| 128 | + it('should apply custom colors to items', () => { |
| 129 | + const items = [ |
| 130 | + { label: '红色选项', color: '#ff0000' }, |
| 131 | + { label: '蓝色选项', color: '#0000ff' }, |
| 132 | + ]; |
| 133 | + const { container } = render(<ActionSheetList items={items} onSelected={mockOnSelected} />); |
| 134 | + |
| 135 | + const buttons = container.querySelectorAll('.t-action-sheet__list-item'); |
| 136 | + expect(buttons[0]).toHaveStyle({ color: '#ff0000' }); |
| 137 | + expect(buttons[1]).toHaveStyle({ color: '#0000ff' }); |
| 138 | + }); |
| 139 | + |
| 140 | + it('should render disabled items with proper styling', () => { |
| 141 | + const items = [{ label: '禁用选项', disabled: true }]; |
| 142 | + const { container } = render(<ActionSheetList items={items} onSelected={mockOnSelected} />); |
| 143 | + |
| 144 | + const disabledButton = container.querySelector('.t-action-sheet__list-item'); |
| 145 | + expect(disabledButton).toBeDisabled(); |
| 146 | + }); |
| 147 | + }); |
| 148 | + |
| 149 | + describe('badge functionality', () => { |
| 150 | + it('should render badge with count', () => { |
| 151 | + const items = [ |
| 152 | + { |
| 153 | + label: '消息', |
| 154 | + badge: { count: 99, maxCount: 99 }, |
| 155 | + }, |
| 156 | + ]; |
| 157 | + const { queryByText } = render(<ActionSheetList items={items} onSelected={mockOnSelected} />); |
| 158 | + |
| 159 | + expect(queryByText('消息')).toBeInTheDocument(); |
| 160 | + }); |
| 161 | + |
| 162 | + it('should render badge with dot', () => { |
| 163 | + const items = [ |
| 164 | + { |
| 165 | + label: '通知', |
| 166 | + badge: { dot: true }, |
| 167 | + }, |
| 168 | + ]; |
| 169 | + const { queryByText } = render(<ActionSheetList items={items} onSelected={mockOnSelected} />); |
| 170 | + |
| 171 | + expect(queryByText('通知')).toBeInTheDocument(); |
| 172 | + }); |
| 173 | + |
| 174 | + it('should render badge with custom content', () => { |
| 175 | + const items = [ |
| 176 | + { |
| 177 | + label: '自定义', |
| 178 | + badge: { content: 'NEW', size: 'medium' as const }, |
| 179 | + }, |
| 180 | + ]; |
| 181 | + const { container } = render(<ActionSheetList items={items} onSelected={mockOnSelected} />); |
| 182 | + |
| 183 | + expect(container.querySelector('.t-badge')).toBeInTheDocument(); |
| 184 | + }); |
| 185 | + }); |
| 186 | +}); |
0 commit comments