select.test.js 51 KB

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