select.test.js 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  1. import { Select } from '../../index';
  2. import { noop } from 'lodash-es';
  3. const Option = Select.Option;
  4. const OptGroup = Select.OptGroup;
  5. import { IconClear, IconChevronDown } from '@douyinfe/semi-icons';
  6. import { BASE_CLASS_PREFIX } from '../../../semi-foundation/base/constants';
  7. import keyCode from '../../../semi-foundation/utils/keyCode';
  8. const defaultList = [
  9. { value: 'abc', label: 'Abc' },
  10. { value: 'hotsoon', label: 'Hotsoon' },
  11. { value: 'pipixia', label: 'Pipixia' },
  12. { value: 'toutiao', label: 'TopBuzz' },
  13. ];
  14. function getOption(list = defaultList) {
  15. return list.map(optionOpts => <Option {...optionOpts} />);
  16. }
  17. let commonProps = {
  18. // Select use Popup Layer to show candidate option,
  19. // but all Popup Layer which extends from Tooltip (eg Popover, Dropdown) have animation and delay.
  20. // Turn off animation and delay during testing, to avoid wating (something like setTimeOut/balabala...) in the test code
  21. motion: false,
  22. mouseEnterDelay: 0,
  23. mouseLeaveDelay: 0,
  24. };
  25. function getSelect(props) {
  26. if (!props.optionList && !props.children) {
  27. props.children = getOption();
  28. }
  29. return mount(<Select {...commonProps} {...props} />, { attachTo: document.getElementById('container') });
  30. }
  31. let stringData = ['semi', 'ies', 'design', 'platform'];
  32. let objectData = [
  33. { email: '[email protected]', value: 'abc' },
  34. { email: '[email protected]', value: 'bytedance' },
  35. { email: '[email protected]', value: 'vigo' },
  36. ];
  37. describe('Select', () => {
  38. beforeEach(() => {
  39. // Avoid `attachTo: document.body` Warning
  40. const div = document.createElement('div');
  41. div.setAttribute('id', 'container');
  42. document.body.appendChild(div);
  43. });
  44. afterEach(() => {
  45. const div = document.getElementById('container');
  46. if (div) {
  47. document.body.removeChild(div);
  48. }
  49. document.body.innerHTML = '';
  50. });
  51. it('custom className & style', () => {
  52. let props = {
  53. className: 'test',
  54. style: {
  55. color: 'red',
  56. },
  57. };
  58. const wrapper = getSelect(props);
  59. expect(wrapper.hasClass('test')).toEqual(true);
  60. expect(wrapper.find('div.test')).toHaveStyle('color', 'red');
  61. });
  62. it('with placeholder', () => {
  63. const props = { placeholder: 'semi' };
  64. const select = getSelect(props);
  65. expect(select.find(`.${BASE_CLASS_PREFIX}-select-selection-placeholder`).instance().textContent).toEqual('semi');
  66. });
  67. it('with validateStatus', () => {
  68. const props = {};
  69. const select = getSelect(props);
  70. expect(select.exists(`.${BASE_CLASS_PREFIX}-select-error`)).toEqual(false);
  71. expect(select.exists(`.${BASE_CLASS_PREFIX}-select-warning`)).toEqual(false);
  72. select.setProps({ validateStatus: 'error' });
  73. expect(select.exists(`.${BASE_CLASS_PREFIX}-select-error`)).toEqual(true);
  74. select.setProps({ validateStatus: 'warning' });
  75. expect(select.exists(`.${BASE_CLASS_PREFIX}-select-warning`)).toEqual(true);
  76. });
  77. it('different size', () => {
  78. const props = {};
  79. const select = getSelect(props);
  80. expect(select.exists(`.${BASE_CLASS_PREFIX}-select-large`)).toEqual(false);
  81. expect(select.exists(`.${BASE_CLASS_PREFIX}-select-small`)).toEqual(false);
  82. select.setProps({ size: 'large' });
  83. expect(select.exists(`.${BASE_CLASS_PREFIX}-select-large`)).toEqual(true);
  84. select.setProps({ size: 'small' });
  85. expect(select.exists(`.${BASE_CLASS_PREFIX}-select-small`)).toEqual(true);
  86. });
  87. it('custom dropdownClassName & dropdownStyle', () => {
  88. let props = {
  89. dropdownClassName: 'ddc',
  90. dropdownStyle: {
  91. color: 'red',
  92. },
  93. defaultOpen: true,
  94. };
  95. let select = getSelect(props);
  96. expect(select.exists('.ddc')).toEqual(true);
  97. expect(select.find('.ddc')).toHaveStyle('color', 'red');
  98. });
  99. it('different position', () => {
  100. let props = {
  101. position: 'top',
  102. defaultOpen: true,
  103. };
  104. let select = getSelect(props);
  105. expect(
  106. select
  107. .find(`.${BASE_CLASS_PREFIX}-popover-wrapper`)
  108. .instance()
  109. .getAttribute('x-placement')
  110. ).toEqual('top');
  111. });
  112. it('defaultValue (not candidate in optionList)', () => {
  113. // single select
  114. let props = {
  115. defaultValue: 'semi',
  116. };
  117. let select = getSelect(props);
  118. expect(select.find(`.${BASE_CLASS_PREFIX}-select-selection-text`).getDOMNode().textContent).toEqual('semi');
  119. select.unmount();
  120. // multiple select
  121. let mProps = {
  122. multiple: true,
  123. defaultValue: ['semi', 'ies'],
  124. };
  125. let mSelect = getSelect(mProps);
  126. let tags = mSelect.find(`.${BASE_CLASS_PREFIX}-select-selection .semi-tag-content`);
  127. expect(tags.length).toEqual(2);
  128. expect(tags.at(0).getDOMNode().textContent).toEqual('semi');
  129. expect(tags.at(1).getDOMNode().textContent).toEqual('ies');
  130. mSelect.unmount();
  131. });
  132. it('defaultValue (can match in optionList)', () => {
  133. // single select
  134. let props = {
  135. defaultValue: 'abc',
  136. };
  137. let select = getSelect(props);
  138. expect(select.find(`.${BASE_CLASS_PREFIX}-select-selection-text`).getDOMNode().textContent).toEqual('Abc');
  139. select.unmount();
  140. // multiple select
  141. let mProps = {
  142. defaultValue: ['abc', 'hotsoon'],
  143. multiple: true,
  144. };
  145. const mSelect = getSelect(mProps);
  146. let tags = mSelect.find(`.${BASE_CLASS_PREFIX}-select-selection .${BASE_CLASS_PREFIX}-tag-content`);
  147. expect(tags.length).toEqual(2);
  148. expect(tags.at(0).getDOMNode().textContent).toEqual('Abc');
  149. expect(tags.at(1).getDOMNode().textContent).toEqual('Hotsoon');
  150. mSelect.unmount();
  151. });
  152. it('showClear', () => {
  153. const props = { defaultValue: '${BASE_CLASS_PREFIX}', showClear: true };
  154. const select = getSelect(props);
  155. expect(select.exists(`.${BASE_CLASS_PREFIX}-icon-clear`)).toEqual(false);
  156. select.find(`.${BASE_CLASS_PREFIX}-select`).simulate('mouseEnter', {});
  157. expect(select.exists(`.${BASE_CLASS_PREFIX}-icon-clear`)).toEqual(true);
  158. select.unmount();
  159. const emptyProps = { showClear: true };
  160. const emptySelect = getSelect(emptyProps);
  161. emptySelect.find(`.${BASE_CLASS_PREFIX}-select`).simulate('mouseEnter', {});
  162. expect(select.exists(`.${BASE_CLASS_PREFIX}-icon-clear`)).toEqual(false);
  163. emptySelect.unmount();
  164. const notShowProps = { showClear: false, defaultValue: 'semi' };
  165. const noSelect = getSelect(notShowProps);
  166. noSelect.find(`.${BASE_CLASS_PREFIX}-select`).simulate('mouseEnter', {});
  167. expect(select.exists(`.${BASE_CLASS_PREFIX}-icon-clear`)).toEqual(false);
  168. noSelect.unmount();
  169. });
  170. it('showArrow = false', () => {
  171. const props = { defaultValue: 'semi', showArrow: false };
  172. const select = getSelect(props);
  173. expect(select.exists(`.${BASE_CLASS_PREFIX}-icon-chevron_down`)).toEqual(false);
  174. });
  175. it('custom prefix / suffix / insetLabel', () => {
  176. let prefix = <div className="prefix">prefix content</div>;
  177. let suffix = <div className="suffix">suffix content</div>;
  178. let insetLabel = 'semi';
  179. const props = {
  180. prefix: prefix,
  181. suffix: suffix,
  182. };
  183. let select = getSelect(props);
  184. expect(select.contains(prefix)).toEqual(true);
  185. expect(select.contains(suffix)).toEqual(true);
  186. select.unmount();
  187. let ilSelect = getSelect({ insetLabel: insetLabel });
  188. expect(ilSelect.contains(insetLabel)).toEqual(true);
  189. ilSelect.unmount();
  190. });
  191. it('defaultOpen', () => {
  192. let props = {
  193. defaultOpen: true,
  194. };
  195. let select = getSelect(props);
  196. expect(select.state().isOpen).toEqual(true);
  197. let options = select.find(`.${BASE_CLASS_PREFIX}-select-option-list`).children();
  198. expect(options.length).toEqual(4);
  199. expect(options.at(0).getDOMNode().textContent).toEqual('Abc');
  200. expect(options.at(1).getDOMNode().textContent).toEqual('Hotsoon');
  201. });
  202. it('dropdownMatchSelectWidth = true', () => {
  203. // dropdownMatchSelectWidth default is true
  204. let props = {
  205. defaultOpen: true,
  206. style: { width: 90 },
  207. defaultValue: 'abc',
  208. };
  209. let defaultSelect = getSelect(props);
  210. // cause jsdom doesn't support layout engine like browser, so you can't access offsetWidth/scrollWidth or use getBoundingRect(), it will always return 0;
  211. // just use getComputedStyle to avoid this problem.
  212. let selector = defaultSelect.find(`.${BASE_CLASS_PREFIX}-select`).getDOMNode();
  213. let selectorWidth = window.getComputedStyle(selector).width; // expect 90px
  214. let list = defaultSelect.find(`.${BASE_CLASS_PREFIX}-select-option-list`).getDOMNode().parentNode;
  215. let listWidth = window.getComputedStyle(list).minWidth;
  216. expect(selectorWidth).toEqual(listWidth);
  217. defaultSelect.unmount();
  218. });
  219. it('dropdownMatchSelectWidth = false', () => {
  220. let notMatchProps = {
  221. defaultOpen: true,
  222. style: { width: 90 },
  223. defaultValue: 'abc',
  224. dropdownMatchSelectWidth: false,
  225. };
  226. let nmSelect = getSelect(notMatchProps);
  227. let selector = nmSelect.find(`.${BASE_CLASS_PREFIX}-select`).getDOMNode();
  228. let selectorWidth = window.getComputedStyle(selector).width;
  229. let list = nmSelect.find(`.${BASE_CLASS_PREFIX}-select-option-list`).getDOMNode().parentNode;
  230. let listWidth = window.getComputedStyle(list).minWidth;
  231. expect(selectorWidth).not.toEqual(listWidth);
  232. nmSelect.unmount();
  233. });
  234. it('pass options via props.optionList', () => {
  235. // expect number and content correct
  236. const props = {
  237. defaultOpen: true,
  238. optionList: defaultList,
  239. };
  240. const select = getSelect(props);
  241. let candidate = select.find(`.${BASE_CLASS_PREFIX}-select-option-list`).children();
  242. expect(candidate.length).toEqual(4);
  243. expect(candidate.at(0).getDOMNode().textContent).toEqual('Abc');
  244. expect(candidate.at(1).getDOMNode().textContent).toEqual('Hotsoon');
  245. select.unmount();
  246. });
  247. it('pass options via props.children', () => {
  248. let list = defaultList.slice();
  249. list.push({ value: 'semi', label: 'SemiDesign' });
  250. const props = {
  251. defaultOpen: true,
  252. children: getOption(list),
  253. };
  254. const select = getSelect(props);
  255. let candidate = select.find(`.${BASE_CLASS_PREFIX}-select-option-list`).children();
  256. expect(candidate.length).toEqual(5);
  257. expect(candidate.at(0).getDOMNode().textContent).toEqual('Abc');
  258. expect(candidate.at(4).getDOMNode().textContent).toEqual('SemiDesign');
  259. select.unmount();
  260. });
  261. it('can choose more than one option when multiple is true', () => {
  262. const props = {
  263. multiple: true,
  264. defaultValue: ['abc', 'hotsoon'],
  265. defaultOpen: true,
  266. };
  267. const select = getSelect(props);
  268. let selection = select.find(`.${BASE_CLASS_PREFIX}-select-content-wrapper`).children();
  269. expect(selection.length).toEqual(2);
  270. let targetOption = select
  271. .find(`.${BASE_CLASS_PREFIX}-select-option-list`)
  272. .children()
  273. .at(3);
  274. const nativeEvent = { nativeEvent: { stopImmediatePropagation: noop } };
  275. targetOption.simulate('click', nativeEvent);
  276. expect(select.find(`.${BASE_CLASS_PREFIX}-select-content-wrapper`).children().length).toEqual(3);
  277. select.unmount();
  278. });
  279. it('multiple with maxTagCount', () => {
  280. const props = {
  281. multiple: true,
  282. defaultValue: ['abc', 'hotsoon'],
  283. maxTagCount: 2,
  284. defaultOpen: true,
  285. };
  286. const select = getSelect(props);
  287. let targetOption = select
  288. .find(`.${BASE_CLASS_PREFIX}-select-option-list`)
  289. .children()
  290. .at(3);
  291. const nativeEvent = { nativeEvent: { stopImmediatePropagation: noop } };
  292. targetOption.simulate('click', nativeEvent);
  293. let selection = select.find(`.${BASE_CLASS_PREFIX}-tag-group`);
  294. expect(selection.children().length).toEqual(3);
  295. expect(
  296. selection
  297. .children()
  298. .at(2)
  299. .getDOMNode().textContent
  300. ).toEqual('+1');
  301. select.unmount();
  302. });
  303. it('multiple with max, should call onExceed when selected over max', () => {
  304. let onExceed = () => {};
  305. let spyonExceed = sinon.spy(onExceed);
  306. const props = {
  307. multiple: true,
  308. defaultValue: ['abc', 'hotsoon'],
  309. max: 2,
  310. onExceed: spyonExceed,
  311. defaultOpen: true,
  312. };
  313. const select = getSelect(props);
  314. let targetOption = select
  315. .find(`.${BASE_CLASS_PREFIX}-select-option-list`)
  316. .children()
  317. .at(3);
  318. const nativeEvent = { nativeEvent: { stopImmediatePropagation: noop } };
  319. targetOption.simulate('click', nativeEvent);
  320. expect(select.find(`.${BASE_CLASS_PREFIX}-select-content-wrapper`).children().length).toEqual(2);
  321. expect(spyonExceed.calledOnce).toBe(true);
  322. select.unmount();
  323. });
  324. it('innerTopSlot', () => {
  325. let innerTopSlot = <div class="inner-slot">inner</div>;
  326. let props = {
  327. innerTopSlot: innerTopSlot,
  328. defaultOpen: true,
  329. };
  330. const select = getSelect(props);
  331. expect(select.contains(innerTopSlot)).toEqual(true);
  332. });
  333. it('outerTopSlot', () => {
  334. let outerTopSlot = <div class="outer-slot">outer</div>;
  335. let props = {
  336. outerTopSlot: outerTopSlot,
  337. defaultOpen: true,
  338. };
  339. const select = getSelect(props);
  340. expect(select.contains(outerTopSlot)).toEqual(true);
  341. });
  342. // TODO
  343. it('innerBottomSlot', () => {
  344. let innerBottomSlot = <div class="inner-slot">inner</div>;
  345. let props = {
  346. innerBottomSlot: innerBottomSlot,
  347. defaultOpen: true,
  348. };
  349. const select = getSelect(props);
  350. expect(select.contains(innerBottomSlot)).toEqual(true);
  351. });
  352. it('outerBottomSlot', () => {
  353. let outerBottomSlot = <div class="outer-slot">outer</div>;
  354. let props = {
  355. outerBottomSlot: outerBottomSlot,
  356. defaultOpen: true,
  357. };
  358. const select = getSelect(props);
  359. expect(select.contains(outerBottomSlot)).toEqual(true);
  360. });
  361. it('option className & style & disabled & showTick', () => {
  362. let options = [
  363. { className: 'optCls', style: { color: 'red' }, label: 'Abc', value: 'abc' },
  364. { label: 'Vigo', value: 'vigo', disabled: true, className: 'disabled-opt' },
  365. { label: 'NoTick', value: 'noTick', showTick: false },
  366. ];
  367. options = options.map(item => {
  368. return <Option {...item}>{item.label}</Option>;
  369. });
  370. let props = {
  371. children: options,
  372. defaultOpen: true,
  373. };
  374. const select = getSelect(props);
  375. expect(select.find(`.${BASE_CLASS_PREFIX}-select-option.optCls`)).toHaveStyle('color', 'red');
  376. let optionList = select.find(`.${BASE_CLASS_PREFIX}-select-option-list`);
  377. expect(optionList.exists(`.${BASE_CLASS_PREFIX}-select-option.disabled-opt`)).toEqual(true);
  378. expect(
  379. optionList
  380. .children()
  381. .at(2)
  382. .getDOMNode().textContent
  383. ).toEqual('NoTick');
  384. });
  385. it('loading', () => {
  386. let props = {
  387. defaultOpen: true,
  388. loading: true,
  389. };
  390. const select = getSelect(props);
  391. expect(select.exists(`.${BASE_CLASS_PREFIX}-select-loading-wrapper`)).toEqual(true);
  392. expect(select.find(`.${BASE_CLASS_PREFIX}-select-option-list`).children.length).toEqual(1);
  393. expect(select.exists(`.${BASE_CLASS_PREFIX}-select-option`)).toEqual(false);
  394. });
  395. it('spacing', () => {
  396. // Can't test spacing directly, just test whether it is passed to Popover correctly
  397. let props = {
  398. spacing: 20,
  399. defaultOpen: true,
  400. };
  401. const select = getSelect(props);
  402. const tooltip = select.children().children();
  403. expect(tooltip.props().spacing).toEqual(20);
  404. });
  405. it('should open optionList when click selector', () => {
  406. const props = {};
  407. const select = getSelect(props);
  408. expect(select.exists(`.${BASE_CLASS_PREFIX}-select-option-list`)).toEqual(false);
  409. select.find(`.${BASE_CLASS_PREFIX}-select`).simulate('click', {});
  410. expect(select.exists(`.${BASE_CLASS_PREFIX}-select-option-list`)).toEqual(true);
  411. });
  412. it('disabled component when disabled is true', () => {
  413. const props = { disabled: true };
  414. const select = getSelect(props);
  415. expect(select.exists(`.${BASE_CLASS_PREFIX}-select-disabled`)).toEqual(true);
  416. // Does not respond click events when disbaled is true
  417. select.find(`.${BASE_CLASS_PREFIX}-select`).simulate('click', {});
  418. expect(select.exists(`.${BASE_CLASS_PREFIX}-select-option-list`)).toEqual(false);
  419. });
  420. it('onDropdownVisibleChange & clickToHide', () => {
  421. let onDropdownVisible = () => {};
  422. let spyOnDV = sinon.spy(onDropdownVisible);
  423. const props = {
  424. onDropdownVisibleChange: spyOnDV,
  425. clickToHide: true,
  426. };
  427. const select = getSelect(props);
  428. select.find(`.${BASE_CLASS_PREFIX}-select`).simulate('click', {});
  429. expect(select.exists(`.${BASE_CLASS_PREFIX}-select-option-list`)).toEqual(true);
  430. expect(spyOnDV.calledOnce).toEqual(true);
  431. expect(spyOnDV.calledWithMatch(true)).toEqual(true);
  432. select.find(`.${BASE_CLASS_PREFIX}-select`).simulate('click', {});
  433. expect(select.exists(`.${BASE_CLASS_PREFIX}-select-option-list`)).toEqual(false);
  434. expect(spyOnDV.calledWithMatch(false)).toEqual(true);
  435. });
  436. it('filter = true', () => {
  437. let props = {
  438. filter: true,
  439. };
  440. const select = getSelect(props);
  441. // click to show input
  442. select.find(`.${BASE_CLASS_PREFIX}-select`).simulate('click', {});
  443. let inputValue = 'abc';
  444. let event = { target: { value: inputValue } };
  445. select.find('input').simulate('change', event);
  446. let optionList = select.find(`.${BASE_CLASS_PREFIX}-select-option-list`).children();
  447. expect(optionList.length).toEqual(1);
  448. expect(optionList.at(0).text()).toEqual('Abc');
  449. });
  450. it('filter = custom function', () => {
  451. let customFilter = (sugInput, option) => {
  452. return option.label == 'Hotsoon';
  453. };
  454. let props = {
  455. filter: customFilter,
  456. };
  457. const select = getSelect(props);
  458. select.find(`.${BASE_CLASS_PREFIX}-select`).simulate('click', {});
  459. let inputValue = 'tik';
  460. let event = { target: { value: inputValue } };
  461. select.find('input').simulate('change', event);
  462. let optionList = select.find(`.${BASE_CLASS_PREFIX}-select-option-list`).children();
  463. expect(optionList.length).toEqual(1);
  464. expect(optionList.at(0).text()).toEqual('Hotsoon');
  465. });
  466. it('onSearch', () => {
  467. // trigger onSearch when input change
  468. let onSearch = value => {};
  469. let spyOnSearch = sinon.spy(onSearch);
  470. let props = {
  471. onSearch: spyOnSearch,
  472. filter: true,
  473. };
  474. let select = getSelect(props);
  475. // click to show input
  476. select.find(`.${BASE_CLASS_PREFIX}-select`).simulate('click', {});
  477. let inputValue = 'semi';
  478. let event = { target: { value: inputValue } };
  479. select.find('input').simulate('change', event);
  480. expect(spyOnSearch.calledOnce).toBe(true);
  481. expect(spyOnSearch.calledWithMatch(inputValue)).toBe(true);
  482. select.unmount();
  483. // when click clear button, should trigger onSearch
  484. // TODO
  485. let scProps = {
  486. showClear: true,
  487. filter: true,
  488. defaultValue: 'tikok',
  489. };
  490. const scSelect = getSelect(props);
  491. });
  492. it('emptyContent', () => {
  493. let emptyContent = 'no data';
  494. let props = {
  495. filter: true,
  496. emptyContent,
  497. };
  498. const select = getSelect(props);
  499. // click to show input
  500. select.find(`.${BASE_CLASS_PREFIX}-select`).simulate('click', {});
  501. let inputValue = 'semi';
  502. let event = { target: { value: inputValue } };
  503. select.find('input').simulate('change', event);
  504. expect(select.find(`.${BASE_CLASS_PREFIX}-select-option-empty`).text()).toEqual(emptyContent);
  505. });
  506. it('option value & label', () => {
  507. let spyOnChange = sinon.spy(() => {});
  508. let props = {
  509. optionList: [{ label: 'semi', value: 'bytedance' }],
  510. defaultOpen: true,
  511. onChange: spyOnChange,
  512. };
  513. const select = getSelect(props);
  514. let optionList = select.find(`.${BASE_CLASS_PREFIX}-select-option-list`).children();
  515. expect(optionList.at(0).text()).toEqual('semi');
  516. const nativeEvent = { nativeEvent: { stopImmediatePropagation: noop } };
  517. optionList.at(0).simulate('click', nativeEvent);
  518. expect(spyOnChange.calledWithMatch('bytedance')).toEqual(true);
  519. });
  520. it('option.value is number', () => {
  521. let spyOnChange = sinon.spy(() => {});
  522. let props = {
  523. optionList: [{ label: 'semi', value: 0 }],
  524. defaultOpen: true,
  525. onChange: spyOnChange,
  526. };
  527. const select = getSelect(props);
  528. let optionList = select.find(`.${BASE_CLASS_PREFIX}-select-option-list`).children();
  529. expect(optionList.at(0).text()).toEqual('semi');
  530. const nativeEvent = { nativeEvent: { stopImmediatePropagation: noop } };
  531. optionList.at(0).simulate('click', nativeEvent);
  532. expect(spyOnChange.calledWithMatch(0)).toEqual(true);
  533. });
  534. it('renderSelectedItem, single', () => {
  535. const spyRSI = sinon.spy(option => {
  536. return option.value + '-' + option.label;
  537. });
  538. let props = {
  539. renderSelectedItem: spyRSI,
  540. defaultValue: 'abc',
  541. };
  542. const select = getSelect(props);
  543. expect(select.find(`.${BASE_CLASS_PREFIX}-select-selection-text`).text()).toEqual('abc-Abc');
  544. expect(spyRSI.calledWith({ value: 'abc', label: 'Abc' }));
  545. });
  546. it('renderSelectedItem, single & value = 0, not exist in optionList', () => {
  547. // test value = 0 & not match in optionList
  548. const spyRSI2 = sinon.spy(option => option.label + 1);
  549. let props2 = {
  550. renderSelectedItem: spyRSI2,
  551. value: 0,
  552. };
  553. const select2 = getSelect(props2);
  554. expect(select2.find(`.${BASE_CLASS_PREFIX}-select-selection-text`).text()).toEqual('1');
  555. expect(spyRSI2.calledWith({ value: 0, label: 0 }));
  556. });
  557. it('renderSelectedItem - multiple', () => {
  558. const spyRSI = sinon.spy((option, opts) => {
  559. let content = option.value + '-' + option.extra;
  560. return {
  561. isRenderInTag: true,
  562. content,
  563. };
  564. });
  565. let props = {
  566. optionList: [
  567. { value: 'abc', label: 'Abc', extra: 'a1' },
  568. { value: 'hotsoon', label: 'Hotsoon', extra: 'b2' },
  569. { value: 'pipixia', label: 'Pipixia', extra: 'c3' },
  570. { value: 'toutiao', label: 'TopBuzz', extra: 'd4' },
  571. ],
  572. renderSelectedItem: spyRSI,
  573. defaultValue: ['abc', 'hotsoon'],
  574. multiple: true,
  575. };
  576. const select = getSelect(props);
  577. let tags = select.find(`.${BASE_CLASS_PREFIX}-tag-content`);
  578. expect(tags.at(0).text()).toEqual('abc-a1');
  579. expect(tags.at(1).text()).toEqual('hotsoon-b2');
  580. });
  581. it('renderSelectedItem - multiple - isRenderInTag: false', () => {
  582. let item1, item2;
  583. const spyRSI = sinon.spy((option, opts) => {
  584. let content = <div className={opts.index}>{option.value + '-' + option.extra}</div>;
  585. if (opts.index === 0) {
  586. item1 = content;
  587. } else if (opts.index === 1) {
  588. item2 = content;
  589. }
  590. return {
  591. isRenderInTag: false,
  592. content,
  593. };
  594. });
  595. let props = {
  596. optionList: [
  597. { value: 'abc', label: 'Abc', extra: 'a1' },
  598. { value: 'hotsoon', label: 'Hotsoon', extra: 'b2' },
  599. { value: 'pipixia', label: 'Pipixia', extra: 'c3' },
  600. { value: 'toutiao', label: 'TopBuzz', extra: 'd4' },
  601. ],
  602. renderSelectedItem: spyRSI,
  603. defaultValue: ['abc', 'hotsoon'],
  604. multiple: true,
  605. };
  606. const select = getSelect(props);
  607. const items = select.find(`.${BASE_CLASS_PREFIX}-select-content-wrapper`).children();
  608. expect(items.at(0).contains(item1));
  609. expect(items.at(1).contains(item2));
  610. });
  611. it('defaultActiveFirstOption', () => {
  612. const props = {
  613. defaultActiveFirstOption: true,
  614. defaultOpen: true,
  615. };
  616. const select = getSelect(props);
  617. // expect first option active
  618. expect(select.find(`.${BASE_CLASS_PREFIX}-select-option-focused`).text()).toEqual('Abc');
  619. });
  620. it('onSelect', () => {
  621. // trigger onSelect when option has been selected
  622. let spyOnSelect = sinon.spy((value, option) => {});
  623. let props = {
  624. defaultOpen: true,
  625. onSelect: spyOnSelect,
  626. };
  627. let select = getSelect(props);
  628. let options = select.find(`.${BASE_CLASS_PREFIX}-select-option-list`).children();
  629. let firstOption = options.at(0);
  630. const nativeEvent = { nativeEvent: { stopImmediatePropagation: noop } };
  631. firstOption.simulate('click', nativeEvent);
  632. expect(spyOnSelect.calledOnce).toBe(true);
  633. expect(spyOnSelect.calledWith('abc', { value: 'abc', label: 'Abc' })).toBe(true);
  634. expect(spyOnSelect.calledWith('abc', { value: 'abc', label: 'Abc', extraKey: true })).toBe(false);
  635. });
  636. it('onDeselect', () => {
  637. // trigger onDeselect when option is deselectd
  638. let onDeselect = (value, option) => {};
  639. let spyOnDeselect = sinon.spy(onDeselect);
  640. let props = {
  641. multiple: true,
  642. spyOnDeselect,
  643. defaultOpen: true,
  644. defaultValue: ['abc', 'hotsoon'],
  645. onDeselect: spyOnDeselect,
  646. };
  647. const select = getSelect(props);
  648. let options = select.find(`.${BASE_CLASS_PREFIX}-select-option-list`).children();
  649. const secondOption = options.at(1);
  650. const nativeEvent = { nativeEvent: { stopImmediatePropagation: noop } };
  651. secondOption.simulate('click', nativeEvent);
  652. expect(spyOnDeselect.calledOnce).toBe(true);
  653. expect(spyOnDeselect.calledWith('hotsoon', { value: 'hotsoon', label: 'Hotsoon' })).toBe(true);
  654. expect(spyOnDeselect.calledWith('hotsoon', { value: 'hotsoon', label: 'Hotsoon', extraKey: true })).toBe(false);
  655. });
  656. it('onChange (single)', () => {
  657. let spyOnChange = sinon.spy((value, option) => {});
  658. let props = {
  659. defaultOpen: true,
  660. onChange: spyOnChange,
  661. };
  662. let select = getSelect(props);
  663. let options = select.find(`.${BASE_CLASS_PREFIX}-select-option-list`).children();
  664. let firstOption = options.at(0);
  665. const nativeEvent = { nativeEvent: { stopImmediatePropagation: noop } };
  666. firstOption.simulate('click', nativeEvent);
  667. expect(spyOnChange.calledOnce).toBe(true);
  668. expect(spyOnChange.calledWith('abc')).toBe(true);
  669. });
  670. it('onChange (multiple)', () => {
  671. let spyOnChange = sinon.spy((value, option) => {});
  672. let props = {
  673. defaultOpen: true,
  674. multiple: true,
  675. onChange: spyOnChange,
  676. };
  677. let select = getSelect(props);
  678. let options = select.find(`.${BASE_CLASS_PREFIX}-select-option-list`).children();
  679. const nativeEvent = { nativeEvent: { stopImmediatePropagation: noop } };
  680. options.at(0).simulate('click', nativeEvent);
  681. options.at(1).simulate('click', nativeEvent);
  682. expect(spyOnChange.callCount).toEqual(2);
  683. expect(spyOnChange.getCall(0).args[0]).toEqual(['abc']);
  684. expect(spyOnChange.getCall(1).args[0]).toEqual(['abc', 'hotsoon']);
  685. });
  686. it('onChange + onChangeWithObject (single)', () => {
  687. let spyOnChange = sinon.spy((value, option) => {});
  688. let props = {
  689. defaultOpen: true,
  690. onChangeWithObject: true,
  691. onChange: spyOnChange,
  692. };
  693. let select = getSelect(props);
  694. let options = select.find(`.${BASE_CLASS_PREFIX}-select-option-list`).children();
  695. let firstOption = options.at(0);
  696. const nativeEvent = { nativeEvent: { stopImmediatePropagation: noop } };
  697. firstOption.simulate('click', nativeEvent);
  698. expect(spyOnChange.calledWith({ value: 'abc', label: 'Abc' })).toBe(true);
  699. });
  700. it('onChange + onChangeWithObject (multiple)', () => {
  701. let spyOnChange = sinon.spy((value, option) => {});
  702. let props = {
  703. defaultOpen: true,
  704. onChangeWithObject: true,
  705. multiple: true,
  706. onChange: spyOnChange,
  707. };
  708. let select = getSelect(props);
  709. let options = select.find(`.${BASE_CLASS_PREFIX}-select-option-list`).children();
  710. const nativeEvent = { nativeEvent: { stopImmediatePropagation: noop } };
  711. options.at(0).simulate('click', nativeEvent);
  712. options.at(1).simulate('click', nativeEvent);
  713. expect(spyOnChange.callCount).toEqual(2);
  714. expect(spyOnChange.getCall(0).args[0]).toEqual([{ value: 'abc', label: 'Abc' }]);
  715. expect(spyOnChange.getCall(1).args[0]).toEqual([
  716. { value: 'abc', label: 'Abc' },
  717. { value: 'hotsoon', label: 'Hotsoon' },
  718. ]);
  719. });
  720. it('【value】controlled mode', () => {
  721. let props = {
  722. data: [],
  723. value: 'abc',
  724. };
  725. let select = getSelect(props);
  726. expect(select.find(`.${BASE_CLASS_PREFIX}-select-selection-text`).getDOMNode().textContent).toEqual('Abc');
  727. select.setProps({ value: 'hotsoon' });
  728. select.update();
  729. expect(select.find(`.${BASE_CLASS_PREFIX}-select-selection-text`).getDOMNode().textContent).toEqual('Hotsoon');
  730. select.setProps({ value: undefined });
  731. select.update();
  732. expect(select.find(`.${BASE_CLASS_PREFIX}-select-selection-text`).getDOMNode().textContent).toEqual('');
  733. select.unmount();
  734. });
  735. it('【onBlur/onFocus】', () => {
  736. let spyOnBlur = sinon.spy((value, option) => {
  737. });
  738. let spyOnFocus = sinon.spy((value, option) => {
  739. // debugger
  740. });
  741. let props = {
  742. onBlur: spyOnBlur,
  743. onFocus: spyOnFocus,
  744. };
  745. let select = getSelect(props);
  746. let trigger = select.find('.semi-select');
  747. const nativeEvent = { nativeEvent: { stopImmediatePropagation: noop } };
  748. trigger.simulate('click', nativeEvent);
  749. expect(spyOnFocus.callCount).toEqual(1);
  750. // Since there is no mechanism such as event bubbling in enzyme + jsdom, the blur event can only be triggered manually on the blur element,
  751. // and the blur of the `a element` cannot be achieved through the focus `b element`.
  752. // blur usually call when popover close, so use select instance close() method to mock blur click like use in browser
  753. select.instance().close();
  754. expect(spyOnBlur.callCount).toEqual(1);
  755. select.unmount();
  756. });
  757. it('【autoFocus】- filter = false', () => {
  758. // should focus triggerElement after mounted
  759. let spyOnBlur = sinon.spy((value, option) => {
  760. debugger
  761. });
  762. let spyOnFocus = sinon.spy((value, option) => {
  763. debugger
  764. });
  765. let props = {
  766. onBlur: spyOnBlur,
  767. onFocus: spyOnFocus,
  768. autoFocus: true,
  769. };
  770. let select = getSelect(props);
  771. // should not trigger focus when autoFocus
  772. expect(spyOnFocus.callCount).toEqual(0);
  773. expect(select.exists(`.${BASE_CLASS_PREFIX}-select-focus`)).toEqual(true);
  774. select.unmount();
  775. });
  776. it('【autoFocus】- filter = true', () => {
  777. // autoFocus should auto Focus input element when filter is true
  778. let props = {
  779. autoFocus: true,
  780. filter: true
  781. };
  782. let select = getSelect(props);
  783. expect(select.exists(`.${BASE_CLASS_PREFIX}-select-focus`)).toEqual(true);
  784. expect(select.exists(`.${BASE_CLASS_PREFIX}-input-wrapper-focus`)).toEqual(true);
  785. select.unmount();
  786. });
  787. it('【autoFocus】 & onBlur when autoFocus = true', () => {
  788. // autoFocus should trigger onBlur when click ohter element directly (dropdown not open)
  789. let spyOnBlur = sinon.spy((value, option) => {
  790. });
  791. let props = {
  792. autoFocus: true,
  793. onBlur: spyOnBlur,
  794. }
  795. // but we can't test this case, Orz
  796. // Since there is no mechanism such as event bubbling in enzyme + jsdom, the blur event can only be triggered manually on the blur element,
  797. // and the blur of the `a element` cannot be achieved through the focus `b element`.
  798. // mock blur event on trigger element
  799. let select = getSelect(props);
  800. let trigger = select.find('.semi-select');
  801. const nativeEvent = { nativeEvent: { stopImmediatePropagation: noop } };
  802. trigger.simulate('blur', nativeEvent);
  803. expect(spyOnBlur.callCount).toEqual(1);
  804. });
  805. it('vitrual', () => {
  806. let spyOnChange = sinon.spy((value) => {
  807. });
  808. let optionList = Array.from({ length: 100 }, (v, i) => ({ label: `option-${i}`, value: i }));
  809. let props = {
  810. virtualize: {
  811. itemSize: 36, // px
  812. },
  813. defaultOpen: true,
  814. optionList,
  815. onChange: spyOnChange,
  816. };
  817. let select = getSelect(props);
  818. let options = select.find('.semi-select-option');
  819. let firstOption = options.children().at(0);
  820. const nativeEvent = { nativeEvent: { stopImmediatePropagation: noop } };
  821. firstOption.simulate('click', nativeEvent);
  822. expect(spyOnChange.callCount).toEqual(1);
  823. expect(spyOnChange.calledWithMatch(0)).toEqual(true);
  824. });
  825. it('OptionGroup', () => {
  826. let optionList = [
  827. <Select.OptGroup key={1} label="Group1">
  828. <Select.Option value="a-1">a-1</Select.Option>
  829. <Select.Option value="a-2">a-2</Select.Option>
  830. </Select.OptGroup>,
  831. <Select.OptGroup key={2} label="Group2">
  832. <Select.Option value="b-1">b-1</Select.Option>
  833. <Select.Option value="b-2">b-2</Select.Option>
  834. </Select.OptGroup>,
  835. // last option without label
  836. <Select.OptGroup key={3}>
  837. <Select.Option value="c-1">c-1</Select.Option>
  838. </Select.OptGroup>
  839. ]
  840. let props = {
  841. defaultOpen: true,
  842. children: optionList,
  843. };
  844. let select = getSelect(props);
  845. let options = select.find('.semi-select-group');
  846. expect(options.length).toEqual(2);
  847. expect(options.at(0).text()).toEqual('Group1');
  848. expect(options.at(1).text()).toEqual('Group2');
  849. });
  850. it('empty', () => {
  851. let props = {
  852. defaultOpen: true,
  853. optionList: [],
  854. emptyContent: 'empty'
  855. };
  856. let select = getSelect(props);
  857. let options = select.find('.semi-select-option.semi-select-option-empty');
  858. expect(options.length).toEqual(1);
  859. expect(options.at(0).text()).toEqual(props.emptyContent);
  860. select.setProps({
  861. emptyContent: null
  862. })
  863. select.update()
  864. expect(select.find('.semi-select-option').length).toEqual(0);
  865. });
  866. it('renderOptionItem onClick onMouseEnter', () => {
  867. let spyOnMouseEnter = sinon.spy((value) => {
  868. });
  869. let spyOnClick = sinon.spy((value) => {
  870. });
  871. const renderOptionItem = renderProps => {
  872. const {
  873. disabled,
  874. selected,
  875. label,
  876. value,
  877. focused,
  878. className,
  879. style,
  880. onMouseEnter,
  881. onClick,
  882. empty,
  883. emptyContent,
  884. ...rest
  885. } = renderProps;
  886. return <div style={style} className="custom-option" onClick={spyOnClick} onMouseEnter={spyOnMouseEnter}>
  887. <div className='option-right'>
  888. {label}
  889. </div>
  890. </div>
  891. };
  892. let props = {
  893. defaultOpen: true,
  894. optionList: [
  895. { value: 'abc', label: '抖音', },
  896. { value: 'jianying', label: '剪映', },
  897. ],
  898. renderOptionItem
  899. };
  900. let select = getSelect(props);
  901. let options = select.find('.custom-option');
  902. expect(options.length).toEqual(2);
  903. options.at(0).simulate('click');
  904. expect(spyOnClick.callCount).toEqual(1);
  905. options.at(1).simulate('mouseenter');
  906. expect(spyOnMouseEnter.callCount).toEqual(1);
  907. });
  908. it('customTrigger', () => {
  909. const triggerRender = ({ value, ...rest }) => {
  910. return (
  911. <div className="custom-triger">
  912. trigger
  913. </div>
  914. );
  915. };
  916. let props = {
  917. triggerRender,
  918. };
  919. let select = getSelect(props);
  920. let trigger = select.find('.custom-triger');
  921. expect(trigger.length).toEqual(1);
  922. expect(trigger.at(0).text()).toEqual('trigger');
  923. trigger.at(0).simulate('click')
  924. expect(select.find('.semi-select-option').length).toEqual(defaultList.length);
  925. });
  926. it('test keyboard press', () => {
  927. let props = {
  928. defaultOpen: true,
  929. };
  930. let select = getSelect(props);
  931. // press ⬇️
  932. select.find(`.${BASE_CLASS_PREFIX}-select`).simulate('keydown', { keyCode: keyCode.DOWN });
  933. expect(select.find(`.${BASE_CLASS_PREFIX}-select-option`).at(0).hasClass(`${BASE_CLASS_PREFIX}-select-option-focused`)).toBe(true);
  934. // press ⬆️
  935. select.find(`.${BASE_CLASS_PREFIX}-select`).simulate('keydown', { keyCode: keyCode.UP });
  936. expect(select.find(`.${BASE_CLASS_PREFIX}-select-option`).at(defaultList.length-1).hasClass(`${BASE_CLASS_PREFIX}-select-option-focused`)).toBe(true);
  937. // press ESC
  938. select.find(`.${BASE_CLASS_PREFIX}-select`).simulate('keydown', { keyCode: keyCode.ESC });
  939. expect(select.find(`.${BASE_CLASS_PREFIX}-select-option`).exists()).toBe(false);
  940. // reopen select, press ⬇️ and ENTER, the first option should be selected
  941. select.find(`.${BASE_CLASS_PREFIX}-select`).simulate('click', {});
  942. select.find(`.${BASE_CLASS_PREFIX}-select`).simulate('keydown', { keyCode: keyCode.DOWN });
  943. select.find(`.${BASE_CLASS_PREFIX}-select`).simulate('keydown', { keyCode: keyCode.ENTER });
  944. expect(select.find(`.${BASE_CLASS_PREFIX}-select-selection-text`).text()).toBe(defaultList[0].label);
  945. });
  946. it('allowCreate', () => {
  947. const props = {
  948. multiple: true,
  949. allowCreate: true,
  950. filter: true,
  951. optionList: []
  952. };
  953. const select = getSelect(props);
  954. select.find(`.${BASE_CLASS_PREFIX}-select`).simulate('click', {});
  955. select.find(`.${BASE_CLASS_PREFIX}-select .${BASE_CLASS_PREFIX}-input`).simulate('change', { target: { value: '1' } });
  956. select.find(`.${BASE_CLASS_PREFIX}-select-option`).simulate('click', {});
  957. expect(select.find(`.${BASE_CLASS_PREFIX}-select .semi-tag`).length).toBe(1);
  958. select.find(`.${BASE_CLASS_PREFIX}-select .${BASE_CLASS_PREFIX}-input`).simulate('keydown', { keyCode: keyCode.BACKSPACE });
  959. expect(select.find(`.${BASE_CLASS_PREFIX}-select .semi-tag`).length).toBe(0);
  960. });
  961. // TODO ref selectAll \deselectAll when onChangeWithObject is true
  962. // TODO when loading is true, do not response any keyborard event
  963. // TODO can't remove tag when option is diabled
  964. // TODO
  965. // it('allowCreate-renderCreateItem', ()=>{})
  966. // it('autoAdjustOverflow', ()=>{})
  967. // it('remote', ()=>{})
  968. // it('【data】updateOptionList when data change', () => {
  969. // let props = {
  970. // defaultOpen: true,
  971. // data: ['semi'],
  972. // ...commonProps
  973. // };
  974. // let ac = getAc(props);
  975. // let candidate = ac.find(`.${BASE_CLASS_PREFIX}-autocomplete-option-list`).children();
  976. // expect(candidate.length).toEqual(1);
  977. // expect(candidate.at(0).getDOMNode().textContent).toEqual('${BASE_CLASS_PREFIX}');
  978. // ac.setProps({ data: ['ies', 'design']});
  979. // ac.update();
  980. // candidate = ac.find(`.${BASE_CLASS_PREFIX}-autocomplete-option-list`).children();
  981. // expect(candidate.length).toEqual(2);
  982. // expect(candidate.at(0).getDOMNode().textContent).toEqual('ies');
  983. // expect(candidate.at(1).getDOMNode().textContent).toEqual('design');
  984. // })
  985. // // 即将废弃的兼容API测试
  986. // OptGroup
  987. });