|
| 1 | +import React from 'react'; |
| 2 | +import { describe, it, expect, render, vi, fireEvent, beforeEach, afterEach } from '@test/utils'; |
| 3 | + |
| 4 | +import Cascader from '../Cascader'; |
| 5 | + |
| 6 | +const prefix = 't'; |
| 7 | +const name = `.${prefix}-cascader`; |
| 8 | + |
| 9 | +const data = { |
| 10 | + areaList: [ |
| 11 | + { |
| 12 | + label: '北京市', |
| 13 | + value: '110000', |
| 14 | + children: [ |
| 15 | + { |
| 16 | + value: '110100', |
| 17 | + label: '北京市', |
| 18 | + children: [ |
| 19 | + { value: '110101', label: '东城区' }, |
| 20 | + { value: '110102', label: '西城区' }, |
| 21 | + { value: '110105', label: '朝阳区' }, |
| 22 | + { value: '110106', label: '丰台区' }, |
| 23 | + { value: '110107', label: '石景山区' }, |
| 24 | + { value: '110108', label: '海淀区' }, |
| 25 | + { value: '110109', label: '门头沟区' }, |
| 26 | + { value: '110111', label: '房山区' }, |
| 27 | + { value: '110112', label: '通州区' }, |
| 28 | + { value: '110113', label: '顺义区' }, |
| 29 | + { value: '110114', label: '昌平区' }, |
| 30 | + { value: '110115', label: '大兴区' }, |
| 31 | + { value: '110116', label: '怀柔区' }, |
| 32 | + { value: '110117', label: '平谷区' }, |
| 33 | + { value: '110118', label: '密云区' }, |
| 34 | + { value: '', label: '' }, |
| 35 | + ], |
| 36 | + }, |
| 37 | + ], |
| 38 | + }, |
| 39 | + { |
| 40 | + label: '天津市', |
| 41 | + value: '120000', |
| 42 | + children: [ |
| 43 | + { |
| 44 | + value: '120100', |
| 45 | + label: '天津市', |
| 46 | + children: [ |
| 47 | + { value: '120101', label: '和平区' }, |
| 48 | + { value: '120102', label: '河东区' }, |
| 49 | + { value: '120103', label: '河西区' }, |
| 50 | + { value: '120104', label: '南开区' }, |
| 51 | + { value: '120105', label: '河北区' }, |
| 52 | + { value: '120106', label: '红桥区' }, |
| 53 | + { value: '120110', label: '东丽区' }, |
| 54 | + { value: '120111', label: '西青区' }, |
| 55 | + { value: '120112', label: '津南区' }, |
| 56 | + { value: '120113', label: '北辰区' }, |
| 57 | + { value: '120114', label: '武清区' }, |
| 58 | + { value: '120115', label: '宝坻区' }, |
| 59 | + { value: '120116', label: '滨海新区' }, |
| 60 | + { value: '120117', label: '宁河区' }, |
| 61 | + { value: '120118', label: '静海区' }, |
| 62 | + { value: '120119', label: '蓟州区' }, |
| 63 | + ], |
| 64 | + }, |
| 65 | + ], |
| 66 | + }, |
| 67 | + ], |
| 68 | +}; |
| 69 | + |
| 70 | +describe('Cascader', () => { |
| 71 | + beforeEach(() => { |
| 72 | + vi.useFakeTimers(); |
| 73 | + }); |
| 74 | + |
| 75 | + afterEach(() => { |
| 76 | + vi.useRealTimers(); |
| 77 | + }); |
| 78 | + |
| 79 | + describe('props', () => { |
| 80 | + it(': visible', async () => { |
| 81 | + const onClose = vi.fn(); |
| 82 | + await render(<Cascader options={data.areaList} onClose={onClose} visible={true} value="110000" />); |
| 83 | + expect(document.querySelector(`.${prefix}-popup`)).not.toHaveStyle({ display: 'none' }); |
| 84 | + fireEvent.click(document.querySelector('.t-overlay')); |
| 85 | + expect(onClose).toHaveBeenCalledTimes(1); |
| 86 | + }); |
| 87 | + |
| 88 | + it(': closeBtn', async () => { |
| 89 | + await render(<Cascader options={data.areaList} closeBtn={true} visible={true} value="110000" theme="tab" />); |
| 90 | + expect(document.querySelector(`.${prefix}-icon-close`)).toBeTruthy(); |
| 91 | + }); |
| 92 | + |
| 93 | + it(': closeBtn - custom', async () => { |
| 94 | + await render( |
| 95 | + <Cascader |
| 96 | + options={data.areaList} |
| 97 | + closeBtn={<span className="close-button">Button</span>} |
| 98 | + visible={true} |
| 99 | + value="110000" |
| 100 | + theme="tab" |
| 101 | + />, |
| 102 | + ); |
| 103 | + expect(document.querySelector('.close-button')).toBeTruthy(); |
| 104 | + }); |
| 105 | + |
| 106 | + it(': title', async () => { |
| 107 | + const title = '标题'; |
| 108 | + await render(<Cascader options={data.areaList} title={title} visible={true} value="110000" theme="tab" />); |
| 109 | + expect(document.querySelector(`${name}__title`).innerHTML).toBe(title); |
| 110 | + }); |
| 111 | + |
| 112 | + it(': options', async () => { |
| 113 | + await render(<Cascader options={data.areaList} visible={true} value="110000" theme="tab" />); |
| 114 | + expect(document.querySelectorAll(`${name}__options`).length).toBe(1); |
| 115 | + expect(document.querySelectorAll(`.${prefix}-radio`).length).toBe(2); |
| 116 | + }); |
| 117 | + |
| 118 | + it(': value', async () => { |
| 119 | + await render(<Cascader options={data.areaList} visible={true} value="110114" />); |
| 120 | + expect(document.querySelector(`${name}__step-label--active`).innerHTML).toBe('昌平区'); |
| 121 | + }); |
| 122 | + |
| 123 | + it(': placeholder', async () => { |
| 124 | + const placeholder = '请选择'; |
| 125 | + await render(<Cascader options={data.areaList} placeholder={placeholder} visible={true} />); |
| 126 | + expect(document.querySelector(`${name}__step-label--active`).innerHTML).toBe(placeholder); |
| 127 | + }); |
| 128 | + |
| 129 | + it(': theme - step', async () => { |
| 130 | + await render(<Cascader options={data.areaList} visible={true} value="110000" theme="step" />); |
| 131 | + expect(document.querySelector(`${name}__steps`)).toBeTruthy(); |
| 132 | + }); |
| 133 | + |
| 134 | + it(': subTitles', async () => { |
| 135 | + const subTitles = ['一级', '二级', '三级']; |
| 136 | + await render(<Cascader options={data.areaList} subTitles={subTitles} visible={true} theme="tab" />); |
| 137 | + expect(document.querySelector(`${name}__options-title`).innerHTML).toBe(subTitles[0]); |
| 138 | + }); |
| 139 | + |
| 140 | + it(': checkStrictly', async () => { |
| 141 | + const onChange = vi.fn(); |
| 142 | + const { rerender } = await render( |
| 143 | + <Cascader |
| 144 | + options={data.areaList} |
| 145 | + onChange={onChange} |
| 146 | + closeBtn={<span style={{ color: '#0052d9' }}>确定</span>} |
| 147 | + checkStrictly={true} |
| 148 | + visible={true} |
| 149 | + value="110000" |
| 150 | + />, |
| 151 | + ); |
| 152 | + fireEvent.click(document.querySelectorAll(`.${prefix}-radio`)[0]); |
| 153 | + fireEvent.click(document.querySelector(`${name}__close-btn`)); |
| 154 | + expect(onChange).toHaveBeenCalled(); |
| 155 | + expect(onChange).toHaveBeenCalledWith('110000', [{ value: '110000', label: '北京市' }]); |
| 156 | + |
| 157 | + rerender( |
| 158 | + <Cascader |
| 159 | + options={[ |
| 160 | + { |
| 161 | + label: '', |
| 162 | + value: '', |
| 163 | + }, |
| 164 | + ]} |
| 165 | + onChange={onChange} |
| 166 | + closeBtn={<span style={{ color: '#0052d9' }}>确定</span>} |
| 167 | + checkStrictly={true} |
| 168 | + visible={true} |
| 169 | + value="110000" |
| 170 | + />, |
| 171 | + ); |
| 172 | + fireEvent.click(document.querySelectorAll(`.${prefix}-radio`)[0]); |
| 173 | + expect(onChange).toHaveBeenCalledWith('', [{ value: '', label: '' }]); |
| 174 | + }); |
| 175 | + }); |
| 176 | + |
| 177 | + describe('events', () => { |
| 178 | + it(': onChange', async () => { |
| 179 | + const onChange = vi.fn(); |
| 180 | + const { rerender } = await render( |
| 181 | + <Cascader options={data.areaList} onChange={onChange} visible={true} value="110114" />, |
| 182 | + ); |
| 183 | + fireEvent.click(document.querySelectorAll(`${name}__step`)[1]); |
| 184 | + fireEvent.click(document.querySelectorAll(`.${prefix}-radio`)[6]); |
| 185 | + expect(onChange).toHaveBeenCalled(); |
| 186 | + |
| 187 | + rerender(<Cascader options={data.areaList} onChange={onChange} visible={true} value="110114" theme="tab" />); |
| 188 | + fireEvent.click(document.querySelectorAll(`.${prefix}-tabs__item`)[1]); |
| 189 | + fireEvent.click(document.querySelectorAll(`.${prefix}-radio`)[2]); |
| 190 | + fireEvent.click(document.querySelectorAll(`.${prefix}-tabs__item`)[2]); |
| 191 | + fireEvent.click(document.querySelectorAll(`.${prefix}-radio`)[4]); |
| 192 | + expect(onChange).toHaveBeenCalledTimes(2); |
| 193 | + }); |
| 194 | + |
| 195 | + it(': onPick', async () => { |
| 196 | + const onPick = vi.fn(); |
| 197 | + await render(<Cascader options={data.areaList} onPick={onPick} visible={true} value="110000" theme="tab" />); |
| 198 | + fireEvent.click(document.querySelectorAll(`.${prefix}-radio`)[1]); |
| 199 | + expect(onPick).toHaveBeenCalled(); |
| 200 | + }); |
| 201 | + |
| 202 | + it(': onClose', async () => { |
| 203 | + const onClose = vi.fn(); |
| 204 | + await render(<Cascader options={data.areaList} onClose={onClose} visible={true} value="110000" theme="tab" />); |
| 205 | + fireEvent.click(document.querySelector(`.${prefix}-icon-close`)); |
| 206 | + expect(onClose).toHaveBeenCalled(); |
| 207 | + }); |
| 208 | + }); |
| 209 | +}); |
0 commit comments