|
| 1 | +import React from 'react'; |
| 2 | +import { render } from '@testing-library/react'; |
| 3 | +import Select from '../src'; |
| 4 | + |
| 5 | +describe('Select Semantic Styles', () => { |
| 6 | + const defaultProps = { |
| 7 | + prefixCls: 'rc-select', |
| 8 | + displayValues: [], |
| 9 | + emptyOptions: true, |
| 10 | + id: 'test', |
| 11 | + onDisplayValuesChange: () => {}, |
| 12 | + onSearch: () => {}, |
| 13 | + searchValue: '', |
| 14 | + }; |
| 15 | + |
| 16 | + it('should apply semantic classNames correctly', () => { |
| 17 | + const classNames = { |
| 18 | + prefix: 'custom-prefix', |
| 19 | + suffix: 'custom-suffix', |
| 20 | + input: 'custom-input', |
| 21 | + clear: 'custom-clear', |
| 22 | + placeholder: 'custom-placeholder', |
| 23 | + content: 'custom-content', |
| 24 | + item: 'custom-item', |
| 25 | + itemContent: 'custom-item-content', |
| 26 | + itemRemove: 'custom-item-remove', |
| 27 | + }; |
| 28 | + |
| 29 | + const { container } = render( |
| 30 | + <Select |
| 31 | + {...defaultProps} |
| 32 | + classNames={classNames} |
| 33 | + placeholder="Test placeholder" |
| 34 | + prefix={<span>Prefix</span>} |
| 35 | + suffix={<span>Suffix</span>} |
| 36 | + />, |
| 37 | + ); |
| 38 | + |
| 39 | + // Test prefix className |
| 40 | + expect(container.querySelector('.rc-select-prefix')).toHaveClass('custom-prefix'); |
| 41 | + |
| 42 | + // Test suffix className |
| 43 | + expect(container.querySelector('.rc-select-suffix')).toHaveClass('custom-suffix'); |
| 44 | + |
| 45 | + // Test input className |
| 46 | + expect(container.querySelector('.rc-select-input')).toHaveClass('custom-input'); |
| 47 | + |
| 48 | + // Test content className |
| 49 | + expect(container.querySelector('.rc-select-content')).toHaveClass('custom-content'); |
| 50 | + |
| 51 | + // Test placeholder className |
| 52 | + expect(container.querySelector('.rc-select-placeholder')).toHaveClass('custom-placeholder'); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should apply semantic styles correctly', () => { |
| 56 | + const styles = { |
| 57 | + prefix: { color: 'red' }, |
| 58 | + suffix: { color: 'blue' }, |
| 59 | + input: { fontSize: '16px' }, |
| 60 | + clear: { cursor: 'pointer' }, |
| 61 | + placeholder: { opacity: 0.6 }, |
| 62 | + content: { padding: '4px' }, |
| 63 | + item: { margin: '2px' }, |
| 64 | + itemContent: { fontWeight: 'bold' }, |
| 65 | + itemRemove: { background: 'transparent' }, |
| 66 | + }; |
| 67 | + |
| 68 | + const { container } = render( |
| 69 | + <Select |
| 70 | + {...defaultProps} |
| 71 | + styles={styles} |
| 72 | + placeholder="Test placeholder" |
| 73 | + prefix={<span>Prefix</span>} |
| 74 | + suffix={<span>Suffix</span>} |
| 75 | + />, |
| 76 | + ); |
| 77 | + |
| 78 | + // Test prefix style |
| 79 | + expect(container.querySelector('.rc-select-prefix')).toHaveStyle({ color: 'red' }); |
| 80 | + |
| 81 | + // Test suffix style |
| 82 | + expect(container.querySelector('.rc-select-suffix')).toHaveStyle({ color: 'blue' }); |
| 83 | + |
| 84 | + // Test input style |
| 85 | + expect(container.querySelector('.rc-select-input')).toHaveStyle({ fontSize: '16px' }); |
| 86 | + |
| 87 | + // Test content style |
| 88 | + expect(container.querySelector('.rc-select-content')).toHaveStyle({ padding: '4px' }); |
| 89 | + |
| 90 | + // Test placeholder style |
| 91 | + expect(container.querySelector('.rc-select-placeholder')).toHaveStyle({ opacity: 0.6 }); |
| 92 | + }); |
| 93 | + |
| 94 | + it('should apply item semantic styles in multiple mode', () => { |
| 95 | + const classNames = { |
| 96 | + item: 'custom-item', |
| 97 | + itemContent: 'custom-item-content', |
| 98 | + itemRemove: 'custom-item-remove', |
| 99 | + }; |
| 100 | + |
| 101 | + const styles = { |
| 102 | + item: { margin: '2px' }, |
| 103 | + itemContent: { fontWeight: 'bold' }, |
| 104 | + itemRemove: { background: 'transparent' }, |
| 105 | + }; |
| 106 | + |
| 107 | + const displayValues = [ |
| 108 | + { key: '1', label: 'Option 1', value: '1' }, |
| 109 | + { key: '2', label: 'Option 2', value: '2' }, |
| 110 | + ]; |
| 111 | + |
| 112 | + const { container } = render( |
| 113 | + <Select |
| 114 | + {...defaultProps} |
| 115 | + mode="multiple" |
| 116 | + value={displayValues} |
| 117 | + classNames={classNames} |
| 118 | + styles={styles} |
| 119 | + />, |
| 120 | + ); |
| 121 | + |
| 122 | + // Test item className and style |
| 123 | + const items = container.querySelectorAll('.rc-select-selection-item'); |
| 124 | + expect(items[0]).toHaveClass('custom-item'); |
| 125 | + expect(items[0]).toHaveStyle({ margin: '2px' }); |
| 126 | + |
| 127 | + // Test item content className and style |
| 128 | + const itemContents = container.querySelectorAll('.rc-select-selection-item-content'); |
| 129 | + expect(itemContents[0]).toHaveClass('custom-item-content'); |
| 130 | + expect(itemContents[0]).toHaveStyle({ fontWeight: 'bold' }); |
| 131 | + |
| 132 | + // Test item remove className and style |
| 133 | + const removeButtons = container.querySelectorAll('.rc-select-selection-item-remove'); |
| 134 | + expect(removeButtons[0]).toHaveClass('custom-item-remove'); |
| 135 | + expect(removeButtons[0]).toHaveStyle({ background: 'transparent' }); |
| 136 | + }); |
| 137 | + |
| 138 | + it('should apply clear icon semantic styles when allowClear is enabled', () => { |
| 139 | + const classNames = { |
| 140 | + clear: 'custom-clear', |
| 141 | + }; |
| 142 | + |
| 143 | + const styles = { |
| 144 | + clear: { cursor: 'pointer' }, |
| 145 | + }; |
| 146 | + |
| 147 | + const { container } = render( |
| 148 | + <Select |
| 149 | + {...defaultProps} |
| 150 | + value={[{ key: '1', label: 'Option 1', value: '1' }]} |
| 151 | + allowClear |
| 152 | + classNames={classNames} |
| 153 | + styles={styles} |
| 154 | + />, |
| 155 | + ); |
| 156 | + |
| 157 | + // Test clear icon className and style |
| 158 | + const clearIcon = container.querySelector('.rc-select-clear'); |
| 159 | + expect(clearIcon).toHaveClass('custom-clear'); |
| 160 | + expect(clearIcon).toHaveStyle({ cursor: 'pointer' }); |
| 161 | + }); |
| 162 | +}); |
0 commit comments