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. 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, width is string', () => {
  220. let stringProps = {
  221. defaultOpen: true,
  222. style: { width: '90px' },
  223. defaultValue: 'abc',
  224. };
  225. let stringSelect = getSelect(stringProps);
  226. let strSelector = stringSelect.find(`.${BASE_CLASS_PREFIX}-select`).getDOMNode();
  227. let strSelectorWidth = window.getComputedStyle(strSelector).width; // expect 90px
  228. let strList = stringSelect.find(`.${BASE_CLASS_PREFIX}-select-option-list`).getDOMNode().parentNode;
  229. let strListWidth = window.getComputedStyle(strList).minWidth;
  230. expect(strSelectorWidth).toEqual(strListWidth);
  231. stringSelect.unmount();
  232. });
  233. it('dropdownMatchSelectWidth = false', () => {
  234. let notMatchProps = {
  235. defaultOpen: true,
  236. style: { width: 90 },
  237. defaultValue: 'abc',
  238. dropdownMatchSelectWidth: false,
  239. };
  240. let nmSelect = getSelect(notMatchProps);
  241. let selector = nmSelect.find(`.${BASE_CLASS_PREFIX}-select`).getDOMNode();
  242. let selectorWidth = window.getComputedStyle(selector).width;
  243. let list = nmSelect.find(`.${BASE_CLASS_PREFIX}-select-option-list`).getDOMNode().parentNode;
  244. let listWidth = window.getComputedStyle(list).minWidth;
  245. expect(selectorWidth).not.toEqual(listWidth);
  246. nmSelect.unmount();
  247. });
  248. it('pass options via props.optionList', () => {
  249. // expect number and content correct
  250. const props = {
  251. defaultOpen: true,
  252. optionList: defaultList,
  253. };
  254. const select = getSelect(props);
  255. let candidate = select.find(`.${BASE_CLASS_PREFIX}-select-option-list`).children();
  256. expect(candidate.length).toEqual(4);
  257. expect(candidate.at(0).getDOMNode().textContent).toEqual('Abc');
  258. expect(candidate.at(1).getDOMNode().textContent).toEqual('Hotsoon');
  259. select.unmount();
  260. });
  261. it('pass options via props.children', () => {
  262. let list = defaultList.slice();
  263. list.push({ value: 'semi', label: 'SemiDesign' });
  264. const props = {
  265. defaultOpen: true,
  266. children: getOption(list),
  267. };
  268. const select = getSelect(props);
  269. let candidate = select.find(`.${BASE_CLASS_PREFIX}-select-option-list`).children();
  270. expect(candidate.length).toEqual(5);
  271. expect(candidate.at(0).getDOMNode().textContent).toEqual('Abc');
  272. expect(candidate.at(4).getDOMNode().textContent).toEqual('SemiDesign');
  273. select.unmount();
  274. });
  275. it('can choose more than one option when multiple is true', () => {
  276. const props = {
  277. multiple: true,
  278. defaultValue: ['abc', 'hotsoon'],
  279. defaultOpen: true,
  280. };
  281. const select = getSelect(props);
  282. let selection = select.find(`.${BASE_CLASS_PREFIX}-select-content-wrapper`).children();
  283. expect(selection.length).toEqual(2);
  284. let targetOption = select
  285. .find(`.${BASE_CLASS_PREFIX}-select-option-list`)
  286. .children()
  287. .at(3);
  288. const nativeEvent = { nativeEvent: { stopImmediatePropagation: noop } };
  289. targetOption.simulate('click', nativeEvent);
  290. expect(select.find(`.${BASE_CLASS_PREFIX}-select-content-wrapper`).children().length).toEqual(3);
  291. select.unmount();
  292. });
  293. it('multiple with maxTagCount', () => {
  294. const props = {
  295. multiple: true,
  296. defaultValue: ['abc', 'hotsoon'],
  297. maxTagCount: 2,
  298. defaultOpen: true,
  299. };
  300. const select = getSelect(props);
  301. let targetOption = select
  302. .find(`.${BASE_CLASS_PREFIX}-select-option-list`)
  303. .children()
  304. .at(3);
  305. const nativeEvent = { nativeEvent: { stopImmediatePropagation: noop } };
  306. targetOption.simulate('click', nativeEvent);
  307. let selection = select.find(`.${BASE_CLASS_PREFIX}-tag-group`);
  308. expect(selection.children().length).toEqual(3);
  309. expect(
  310. selection
  311. .children()
  312. .at(2)
  313. .getDOMNode().textContent
  314. ).toEqual('+1');
  315. select.unmount();
  316. });
  317. it('multiple with max, should call onExceed when selected over max', () => {
  318. let onExceed = () => {};
  319. let spyonExceed = sinon.spy(onExceed);
  320. const props = {
  321. multiple: true,
  322. defaultValue: ['abc', 'hotsoon'],
  323. max: 2,
  324. onExceed: spyonExceed,
  325. defaultOpen: true,
  326. };
  327. const select = getSelect(props);
  328. let targetOption = select
  329. .find(`.${BASE_CLASS_PREFIX}-select-option-list`)
  330. .children()
  331. .at(3);
  332. const nativeEvent = { nativeEvent: { stopImmediatePropagation: noop } };
  333. targetOption.simulate('click', nativeEvent);
  334. expect(select.find(`.${BASE_CLASS_PREFIX}-select-content-wrapper`).children().length).toEqual(2);
  335. expect(spyonExceed.calledOnce).toBe(true);
  336. select.unmount();
  337. });
  338. it('innerTopSlot', () => {
  339. let innerTopSlot = <div class="inner-slot">inner</div>;
  340. let props = {
  341. innerTopSlot: innerTopSlot,
  342. defaultOpen: true,
  343. };
  344. const select = getSelect(props);
  345. expect(select.contains(innerTopSlot)).toEqual(true);
  346. });
  347. it('outerTopSlot', () => {
  348. let outerTopSlot = <div class="outer-slot">outer</div>;
  349. let props = {
  350. outerTopSlot: outerTopSlot,
  351. defaultOpen: true,
  352. };
  353. const select = getSelect(props);
  354. expect(select.contains(outerTopSlot)).toEqual(true);
  355. });
  356. // TODO
  357. it('innerBottomSlot', () => {
  358. let innerBottomSlot = <div class="inner-slot">inner</div>;
  359. let props = {
  360. innerBottomSlot: innerBottomSlot,
  361. defaultOpen: true,
  362. };
  363. const select = getSelect(props);
  364. expect(select.contains(innerBottomSlot)).toEqual(true);
  365. });
  366. it('outerBottomSlot', () => {
  367. let outerBottomSlot = <div class="outer-slot">outer</div>;
  368. let props = {
  369. outerBottomSlot: outerBottomSlot,
  370. defaultOpen: true,
  371. };
  372. const select = getSelect(props);
  373. expect(select.contains(outerBottomSlot)).toEqual(true);
  374. });
  375. it('option className & style & disabled & showTick', () => {
  376. let options = [
  377. { className: 'optCls', style: { color: 'red' }, label: 'Abc', value: 'abc' },
  378. { label: 'Vigo', value: 'vigo', disabled: true, className: 'disabled-opt' },
  379. { label: 'NoTick', value: 'noTick', showTick: false },
  380. ];
  381. options = options.map(item => {
  382. return <Option {...item}>{item.label}</Option>;
  383. });
  384. let props = {
  385. children: options,
  386. defaultOpen: true,
  387. };
  388. const select = getSelect(props);
  389. expect(select.find(`.${BASE_CLASS_PREFIX}-select-option.optCls`)).toHaveStyle('color', 'red');
  390. let optionList = select.find(`.${BASE_CLASS_PREFIX}-select-option-list`);
  391. expect(optionList.exists(`.${BASE_CLASS_PREFIX}-select-option.disabled-opt`)).toEqual(true);
  392. expect(
  393. optionList
  394. .children()
  395. .at(2)
  396. .getDOMNode().textContent
  397. ).toEqual('NoTick');
  398. });
  399. it('loading', () => {
  400. let props = {
  401. defaultOpen: true,
  402. loading: true,
  403. };
  404. const select = getSelect(props);
  405. expect(select.exists(`.${BASE_CLASS_PREFIX}-select-loading-wrapper`)).toEqual(true);
  406. expect(select.find(`.${BASE_CLASS_PREFIX}-select-option-list`).children.length).toEqual(1);
  407. expect(select.exists(`.${BASE_CLASS_PREFIX}-select-option`)).toEqual(false);
  408. });
  409. it('spacing', () => {
  410. // Can't test spacing directly, just test whether it is passed to Popover correctly
  411. let props = {
  412. spacing: 20,
  413. defaultOpen: true,
  414. };
  415. const select = getSelect(props);
  416. const tooltip = select.children().children();
  417. expect(tooltip.props().spacing).toEqual(20);
  418. });
  419. it('should open optionList when click selector', () => {
  420. const props = {};
  421. const select = getSelect(props);
  422. expect(select.exists(`.${BASE_CLASS_PREFIX}-select-option-list`)).toEqual(false);
  423. select.find(`.${BASE_CLASS_PREFIX}-select`).simulate('click', {});
  424. expect(select.exists(`.${BASE_CLASS_PREFIX}-select-option-list`)).toEqual(true);
  425. });
  426. it('disabled component when disabled is true', () => {
  427. const props = { disabled: true };
  428. const select = getSelect(props);
  429. expect(select.exists(`.${BASE_CLASS_PREFIX}-select-disabled`)).toEqual(true);
  430. // Does not respond click events when disbaled is true
  431. select.find(`.${BASE_CLASS_PREFIX}-select`).simulate('click', {});
  432. expect(select.exists(`.${BASE_CLASS_PREFIX}-select-option-list`)).toEqual(false);
  433. });
  434. it('onDropdownVisibleChange & clickToHide', () => {
  435. let onDropdownVisible = () => {};
  436. let spyOnDV = sinon.spy(onDropdownVisible);
  437. const props = {
  438. onDropdownVisibleChange: spyOnDV,
  439. clickToHide: true,
  440. };
  441. const select = getSelect(props);
  442. select.find(`.${BASE_CLASS_PREFIX}-select`).simulate('click', {});
  443. expect(select.exists(`.${BASE_CLASS_PREFIX}-select-option-list`)).toEqual(true);
  444. expect(spyOnDV.calledOnce).toEqual(true);
  445. expect(spyOnDV.calledWithMatch(true)).toEqual(true);
  446. select.find(`.${BASE_CLASS_PREFIX}-select`).simulate('click', {});
  447. expect(select.exists(`.${BASE_CLASS_PREFIX}-select-option-list`)).toEqual(false);
  448. expect(spyOnDV.calledWithMatch(false)).toEqual(true);
  449. });
  450. it('filter = true', () => {
  451. let props = {
  452. filter: true,
  453. };
  454. const select = getSelect(props);
  455. // click to show input
  456. select.find(`.${BASE_CLASS_PREFIX}-select`).simulate('click', {});
  457. let inputValue = 'abc';
  458. let event = { target: { value: inputValue } };
  459. select.find('input').simulate('change', event);
  460. let optionList = select.find(`.${BASE_CLASS_PREFIX}-select-option-list`).children();
  461. expect(optionList.length).toEqual(1);
  462. expect(optionList.at(0).text()).toEqual('Abc');
  463. });
  464. it('filter = true,label includes regex special character and key it at first', () => {
  465. let props = {
  466. filter: true,
  467. optionList: [{label: 'label++',value: ''}]
  468. };
  469. const select = getSelect(props);
  470. // click to show input
  471. select.find(`.${BASE_CLASS_PREFIX}-select`).simulate('click', {});
  472. let inputValue = '+';
  473. let event = { target: { value: inputValue } };
  474. select.find('input').simulate('change', event);
  475. let optionList = select.find(`.${BASE_CLASS_PREFIX}-select-option-list`).children();
  476. expect(optionList.length).toEqual(1);
  477. expect(optionList.at(0).text()).toEqual('label++');
  478. });
  479. it('filter = custom function', () => {
  480. let customFilter = (sugInput, option) => {
  481. return option.label == 'Hotsoon';
  482. };
  483. let props = {
  484. filter: customFilter,
  485. };
  486. const select = getSelect(props);
  487. select.find(`.${BASE_CLASS_PREFIX}-select`).simulate('click', {});
  488. let inputValue = 'tik';
  489. let event = { target: { value: inputValue } };
  490. select.find('input').simulate('change', event);
  491. let optionList = select.find(`.${BASE_CLASS_PREFIX}-select-option-list`).children();
  492. expect(optionList.length).toEqual(1);
  493. expect(optionList.at(0).text()).toEqual('Hotsoon');
  494. });
  495. it('onSearch', () => {
  496. // trigger onSearch when input change
  497. let onSearch = value => {};
  498. let spyOnSearch = sinon.spy(onSearch);
  499. let props = {
  500. onSearch: spyOnSearch,
  501. filter: true,
  502. };
  503. let select = getSelect(props);
  504. // click to show input
  505. select.find(`.${BASE_CLASS_PREFIX}-select`).simulate('click', {});
  506. let inputValue = 'semi';
  507. let event = { target: { value: inputValue } };
  508. select.find('input').simulate('change', event);
  509. expect(spyOnSearch.calledOnce).toBe(true);
  510. expect(spyOnSearch.calledWithMatch(inputValue)).toBe(true);
  511. select.unmount();
  512. // when click clear button, should trigger onSearch
  513. // TODO
  514. let scProps = {
  515. showClear: true,
  516. filter: true,
  517. defaultValue: 'tikok',
  518. };
  519. const scSelect = getSelect(props);
  520. });
  521. it('emptyContent', () => {
  522. let emptyContent = 'no data';
  523. let props = {
  524. filter: true,
  525. emptyContent,
  526. };
  527. const select = getSelect(props);
  528. // click to show input
  529. select.find(`.${BASE_CLASS_PREFIX}-select`).simulate('click', {});
  530. let inputValue = 'semi';
  531. let event = { target: { value: inputValue } };
  532. select.find('input').simulate('change', event);
  533. expect(select.find(`.${BASE_CLASS_PREFIX}-select-option-empty`).text()).toEqual(emptyContent);
  534. });
  535. it('option value & label', () => {
  536. let spyOnChange = sinon.spy(() => {});
  537. let props = {
  538. optionList: [{ label: 'semi', value: 'bytedance' }],
  539. defaultOpen: true,
  540. onChange: spyOnChange,
  541. };
  542. const select = getSelect(props);
  543. let optionList = select.find(`.${BASE_CLASS_PREFIX}-select-option-list`).children();
  544. expect(optionList.at(0).text()).toEqual('semi');
  545. const nativeEvent = { nativeEvent: { stopImmediatePropagation: noop } };
  546. optionList.at(0).simulate('click', nativeEvent);
  547. expect(spyOnChange.calledWithMatch('bytedance')).toEqual(true);
  548. });
  549. it('option.value is number', () => {
  550. let spyOnChange = sinon.spy(() => {});
  551. let props = {
  552. optionList: [{ label: 'semi', value: 0 }],
  553. defaultOpen: true,
  554. onChange: spyOnChange,
  555. };
  556. const select = getSelect(props);
  557. let optionList = select.find(`.${BASE_CLASS_PREFIX}-select-option-list`).children();
  558. expect(optionList.at(0).text()).toEqual('semi');
  559. const nativeEvent = { nativeEvent: { stopImmediatePropagation: noop } };
  560. optionList.at(0).simulate('click', nativeEvent);
  561. expect(spyOnChange.calledWithMatch(0)).toEqual(true);
  562. });
  563. it('renderSelectedItem, single', () => {
  564. const spyRSI = sinon.spy(option => {
  565. return option.value + '-' + option.label;
  566. });
  567. let props = {
  568. renderSelectedItem: spyRSI,
  569. defaultValue: 'abc',
  570. };
  571. const select = getSelect(props);
  572. expect(select.find(`.${BASE_CLASS_PREFIX}-select-selection-text`).text()).toEqual('abc-Abc');
  573. expect(spyRSI.calledWith({ value: 'abc', label: 'Abc' }));
  574. });
  575. it('renderSelectedItem, single & value = 0, not exist in optionList', () => {
  576. // test value = 0 & not match in optionList
  577. const spyRSI2 = sinon.spy(option => option.label + 1);
  578. let props2 = {
  579. renderSelectedItem: spyRSI2,
  580. value: 0,
  581. };
  582. const select2 = getSelect(props2);
  583. expect(select2.find(`.${BASE_CLASS_PREFIX}-select-selection-text`).text()).toEqual('1');
  584. expect(spyRSI2.calledWith({ value: 0, label: 0 }));
  585. });
  586. it('renderSelectedItem - multiple', () => {
  587. const spyRSI = sinon.spy((option, opts) => {
  588. let content = option.value + '-' + option.extra;
  589. return {
  590. isRenderInTag: true,
  591. content,
  592. };
  593. });
  594. let props = {
  595. optionList: [
  596. { value: 'abc', label: 'Abc', extra: 'a1' },
  597. { value: 'hotsoon', label: 'Hotsoon', extra: 'b2' },
  598. { value: 'pipixia', label: 'Pipixia', extra: 'c3' },
  599. { value: 'toutiao', label: 'TopBuzz', extra: 'd4' },
  600. ],
  601. renderSelectedItem: spyRSI,
  602. defaultValue: ['abc', 'hotsoon'],
  603. multiple: true,
  604. };
  605. const select = getSelect(props);
  606. let tags = select.find(`.${BASE_CLASS_PREFIX}-tag-content`);
  607. expect(tags.at(0).text()).toEqual('abc-a1');
  608. expect(tags.at(1).text()).toEqual('hotsoon-b2');
  609. });
  610. it('renderSelectedItem - multiple - isRenderInTag: false', () => {
  611. let item1, item2;
  612. const spyRSI = sinon.spy((option, opts) => {
  613. let content = <div className={opts.index}>{option.value + '-' + option.extra}</div>;
  614. if (opts.index === 0) {
  615. item1 = content;
  616. } else if (opts.index === 1) {
  617. item2 = content;
  618. }
  619. return {
  620. isRenderInTag: false,
  621. content,
  622. };
  623. });
  624. let props = {
  625. optionList: [
  626. { value: 'abc', label: 'Abc', extra: 'a1' },
  627. { value: 'hotsoon', label: 'Hotsoon', extra: 'b2' },
  628. { value: 'pipixia', label: 'Pipixia', extra: 'c3' },
  629. { value: 'toutiao', label: 'TopBuzz', extra: 'd4' },
  630. ],
  631. renderSelectedItem: spyRSI,
  632. defaultValue: ['abc', 'hotsoon'],
  633. multiple: true,
  634. };
  635. const select = getSelect(props);
  636. const items = select.find(`.${BASE_CLASS_PREFIX}-select-content-wrapper`).children();
  637. expect(items.at(0).contains(item1));
  638. expect(items.at(1).contains(item2));
  639. });
  640. it('defaultActiveFirstOption', () => {
  641. const props = {
  642. defaultActiveFirstOption: true,
  643. defaultOpen: true,
  644. };
  645. const select = getSelect(props);
  646. // expect first option active
  647. expect(select.find(`.${BASE_CLASS_PREFIX}-select-option-focused`).text()).toEqual('Abc');
  648. });
  649. it('onSelect', () => {
  650. // trigger onSelect when option has been selected
  651. let spyOnSelect = sinon.spy((value, option) => {});
  652. let props = {
  653. defaultOpen: true,
  654. onSelect: spyOnSelect,
  655. };
  656. let select = getSelect(props);
  657. let options = select.find(`.${BASE_CLASS_PREFIX}-select-option-list`).children();
  658. let firstOption = options.at(0);
  659. const nativeEvent = { nativeEvent: { stopImmediatePropagation: noop } };
  660. firstOption.simulate('click', nativeEvent);
  661. expect(spyOnSelect.calledOnce).toBe(true);
  662. expect(spyOnSelect.calledWith('abc', { value: 'abc', label: 'Abc' })).toBe(true);
  663. expect(spyOnSelect.calledWith('abc', { value: 'abc', label: 'Abc', extraKey: true })).toBe(false);
  664. });
  665. it('onDeselect', () => {
  666. // trigger onDeselect when option is deselectd
  667. let onDeselect = (value, option) => {};
  668. let spyOnDeselect = sinon.spy(onDeselect);
  669. let props = {
  670. multiple: true,
  671. spyOnDeselect,
  672. defaultOpen: true,
  673. defaultValue: ['abc', 'hotsoon'],
  674. onDeselect: spyOnDeselect,
  675. };
  676. const select = getSelect(props);
  677. let options = select.find(`.${BASE_CLASS_PREFIX}-select-option-list`).children();
  678. const secondOption = options.at(1);
  679. const nativeEvent = { nativeEvent: { stopImmediatePropagation: noop } };
  680. secondOption.simulate('click', nativeEvent);
  681. expect(spyOnDeselect.calledOnce).toBe(true);
  682. expect(spyOnDeselect.calledWith('hotsoon', { value: 'hotsoon', label: 'Hotsoon' })).toBe(true);
  683. expect(spyOnDeselect.calledWith('hotsoon', { value: 'hotsoon', label: 'Hotsoon', extraKey: true })).toBe(false);
  684. });
  685. it('onChange (single)', () => {
  686. let spyOnChange = sinon.spy((value, option) => {});
  687. let props = {
  688. defaultOpen: true,
  689. onChange: spyOnChange,
  690. };
  691. let select = getSelect(props);
  692. let options = select.find(`.${BASE_CLASS_PREFIX}-select-option-list`).children();
  693. let firstOption = options.at(0);
  694. const nativeEvent = { nativeEvent: { stopImmediatePropagation: noop } };
  695. firstOption.simulate('click', nativeEvent);
  696. expect(spyOnChange.calledOnce).toBe(true);
  697. expect(spyOnChange.calledWith('abc')).toBe(true);
  698. });
  699. it('onChange (multiple)', () => {
  700. let spyOnChange = sinon.spy((value, option) => {});
  701. let props = {
  702. defaultOpen: true,
  703. multiple: true,
  704. onChange: spyOnChange,
  705. };
  706. let select = getSelect(props);
  707. let options = select.find(`.${BASE_CLASS_PREFIX}-select-option-list`).children();
  708. const nativeEvent = { nativeEvent: { stopImmediatePropagation: noop } };
  709. options.at(0).simulate('click', nativeEvent);
  710. options.at(1).simulate('click', nativeEvent);
  711. expect(spyOnChange.callCount).toEqual(2);
  712. expect(spyOnChange.getCall(0).args[0]).toEqual(['abc']);
  713. expect(spyOnChange.getCall(1).args[0]).toEqual(['abc', 'hotsoon']);
  714. });
  715. it('onChange + onChangeWithObject (single)', () => {
  716. let spyOnChange = sinon.spy((value, option) => {});
  717. let props = {
  718. defaultOpen: true,
  719. onChangeWithObject: true,
  720. onChange: spyOnChange,
  721. };
  722. let select = getSelect(props);
  723. let options = select.find(`.${BASE_CLASS_PREFIX}-select-option-list`).children();
  724. let firstOption = options.at(0);
  725. const nativeEvent = { nativeEvent: { stopImmediatePropagation: noop } };
  726. firstOption.simulate('click', nativeEvent);
  727. expect(spyOnChange.calledWith({ value: 'abc', label: 'Abc' })).toBe(true);
  728. });
  729. it('onChange + onChangeWithObject (multiple)', () => {
  730. let spyOnChange = sinon.spy((value, option) => {});
  731. let props = {
  732. defaultOpen: true,
  733. onChangeWithObject: true,
  734. multiple: true,
  735. onChange: spyOnChange,
  736. };
  737. let select = getSelect(props);
  738. let options = select.find(`.${BASE_CLASS_PREFIX}-select-option-list`).children();
  739. const nativeEvent = { nativeEvent: { stopImmediatePropagation: noop } };
  740. options.at(0).simulate('click', nativeEvent);
  741. options.at(1).simulate('click', nativeEvent);
  742. expect(spyOnChange.callCount).toEqual(2);
  743. expect(spyOnChange.getCall(0).args[0]).toEqual([{ value: 'abc', label: 'Abc' }]);
  744. expect(spyOnChange.getCall(1).args[0]).toEqual([
  745. { value: 'abc', label: 'Abc' },
  746. { value: 'hotsoon', label: 'Hotsoon' },
  747. ]);
  748. });
  749. it('【value】controlled mode', () => {
  750. let spyOnChange = sinon.spy((value, option) => {});
  751. let props = {
  752. value: 'abc',
  753. };
  754. let select = getSelect(props);
  755. expect(select.find(`.${BASE_CLASS_PREFIX}-select-selection-text`).getDOMNode().textContent).toEqual('Abc');
  756. select.setProps({ value: 'hotsoon' });
  757. select.update();
  758. expect(select.find(`.${BASE_CLASS_PREFIX}-select-selection-text`).getDOMNode().textContent).toEqual('Hotsoon');
  759. select.setProps({ value: undefined });
  760. select.update();
  761. expect(select.find(`.${BASE_CLASS_PREFIX}-select-selection-text`).getDOMNode().textContent).toEqual('');
  762. select.unmount();
  763. let singleProps = {
  764. value: 'abc',
  765. optionList: defaultList,
  766. defaultOpen: true,
  767. onChange: spyOnChange,
  768. };
  769. select = getSelect(singleProps);
  770. let options = select.find(`.${BASE_CLASS_PREFIX}-select-option-list`).children();
  771. const nativeEvent = { nativeEvent: { stopImmediatePropagation: noop } };
  772. options.at(1).simulate('click', nativeEvent);
  773. expect(spyOnChange.getCall(0).args[0]).toEqual('hotsoon');
  774. select.unmount();
  775. let spyMOnChange = sinon.spy((value, option) => {});
  776. let spyMOnClear = sinon.spy(() => {});
  777. let multipleProps = {
  778. value: '',
  779. optionList: defaultList,
  780. defaultOpen: true,
  781. multiple: true,
  782. filter: true,
  783. onChange: spyMOnChange,
  784. showClear: true,
  785. onClear: spyMOnClear,
  786. };
  787. select = getSelect(multipleProps);
  788. let mOptions = select.find(`.${BASE_CLASS_PREFIX}-select-option-list`).children();
  789. mOptions.at(1).simulate('click', nativeEvent);
  790. expect(spyMOnChange.getCall(0).args[0]).toEqual(['hotsoon']);
  791. // TODO
  792. // test
  793. });
  794. it('【onBlur/onFocus】', () => {
  795. let spyOnBlur = sinon.spy((value, option) => {
  796. });
  797. let spyOnFocus = sinon.spy((value, option) => {
  798. });
  799. let props = {
  800. onBlur: spyOnBlur,
  801. onFocus: spyOnFocus,
  802. };
  803. let select = getSelect(props);
  804. let trigger = select.find('.semi-select');
  805. const nativeEvent = { nativeEvent: { stopImmediatePropagation: noop } };
  806. trigger.simulate('click', nativeEvent);
  807. expect(spyOnFocus.callCount).toEqual(1);
  808. // Since there is no mechanism such as event bubbling in enzyme + jsdom, the blur event can only be triggered manually on the blur element,
  809. // and the blur of the `a element` cannot be achieved through the focus `b element`.
  810. // blur usually call when popover close, so use select instance close() method to mock blur click like use in browser
  811. select.instance().close();
  812. expect(spyOnBlur.callCount).toEqual(1);
  813. select.unmount();
  814. });
  815. it('【autoFocus】- filter = false', () => {
  816. // should focus triggerElement after mounted
  817. let spyOnBlur = sinon.spy((value, option) => {
  818. debugger
  819. });
  820. let spyOnFocus = sinon.spy((value, option) => {
  821. debugger
  822. });
  823. let props = {
  824. onBlur: spyOnBlur,
  825. onFocus: spyOnFocus,
  826. autoFocus: true,
  827. };
  828. let select = getSelect(props);
  829. // should not trigger focus when autoFocus
  830. expect(spyOnFocus.callCount).toEqual(0);
  831. expect(select.exists(`.${BASE_CLASS_PREFIX}-select-focus`)).toEqual(true);
  832. select.unmount();
  833. });
  834. it('【autoFocus】- filter = true', () => {
  835. // autoFocus should auto Focus input element when filter is true
  836. let props = {
  837. autoFocus: true,
  838. filter: true
  839. };
  840. let select = getSelect(props);
  841. expect(select.exists(`.${BASE_CLASS_PREFIX}-select-focus`)).toEqual(true);
  842. expect(select.exists(`.${BASE_CLASS_PREFIX}-input-wrapper-focus`)).toEqual(true);
  843. select.unmount();
  844. });
  845. it('【autoFocus】 & onBlur when autoFocus = true', () => {
  846. // autoFocus should trigger onBlur when click ohter element directly (dropdown not open)
  847. let spyOnBlur = sinon.spy((value, option) => {
  848. });
  849. let props = {
  850. autoFocus: true,
  851. onBlur: spyOnBlur,
  852. }
  853. // but we can't test this case, Orz
  854. // Since there is no mechanism such as event bubbling in enzyme + jsdom, the blur event can only be triggered manually on the blur element,
  855. // and the blur of the `a element` cannot be achieved through the focus `b element`.
  856. // mock blur event on trigger element
  857. let select = getSelect(props);
  858. let trigger = select.find('.semi-select');
  859. const nativeEvent = { nativeEvent: { stopImmediatePropagation: noop } };
  860. trigger.simulate('blur', nativeEvent);
  861. expect(spyOnBlur.callCount).toEqual(1);
  862. });
  863. it('vitrual', () => {
  864. let spyOnChange = sinon.spy((value) => {
  865. });
  866. let optionList = Array.from({ length: 100 }, (v, i) => ({ label: `option-${i}`, value: i }));
  867. let props = {
  868. virtualize: {
  869. itemSize: 36, // px
  870. },
  871. defaultOpen: true,
  872. optionList,
  873. onChange: spyOnChange,
  874. };
  875. let select = getSelect(props);
  876. let options = select.find('.semi-select-option');
  877. let firstOption = options.children().at(0);
  878. const nativeEvent = { nativeEvent: { stopImmediatePropagation: noop } };
  879. firstOption.simulate('click', nativeEvent);
  880. expect(spyOnChange.callCount).toEqual(1);
  881. expect(spyOnChange.calledWithMatch(0)).toEqual(true);
  882. });
  883. it('OptionGroup', () => {
  884. let optionList = [
  885. <Select.OptGroup key={1} label="Group1">
  886. <Select.Option value="a-1">a-1</Select.Option>
  887. <Select.Option value="a-2">a-2</Select.Option>
  888. </Select.OptGroup>,
  889. <Select.OptGroup key={2} label="Group2">
  890. <Select.Option value="b-1">b-1</Select.Option>
  891. <Select.Option value="b-2">b-2</Select.Option>
  892. </Select.OptGroup>,
  893. // last option without label
  894. <Select.OptGroup key={3}>
  895. <Select.Option value="c-1">c-1</Select.Option>
  896. </Select.OptGroup>
  897. ]
  898. let props = {
  899. defaultOpen: true,
  900. children: optionList,
  901. };
  902. let select = getSelect(props);
  903. let options = select.find('.semi-select-group');
  904. expect(options.length).toEqual(2);
  905. expect(options.at(0).text()).toEqual('Group1');
  906. expect(options.at(1).text()).toEqual('Group2');
  907. });
  908. it('empty', () => {
  909. let props = {
  910. defaultOpen: true,
  911. optionList: [],
  912. emptyContent: 'empty'
  913. };
  914. let select = getSelect(props);
  915. let options = select.find('.semi-select-option.semi-select-option-empty');
  916. expect(options.length).toEqual(1);
  917. expect(options.at(0).text()).toEqual(props.emptyContent);
  918. select.setProps({
  919. emptyContent: null
  920. })
  921. select.update()
  922. expect(select.find('.semi-select-option').length).toEqual(0);
  923. });
  924. it('renderOptionItem onClick onMouseEnter', () => {
  925. let spyOnMouseEnter = sinon.spy((value) => {
  926. });
  927. let spyOnClick = sinon.spy((value) => {
  928. });
  929. const renderOptionItem = renderProps => {
  930. const {
  931. disabled,
  932. selected,
  933. label,
  934. value,
  935. focused,
  936. className,
  937. style,
  938. onMouseEnter,
  939. onClick,
  940. empty,
  941. emptyContent,
  942. ...rest
  943. } = renderProps;
  944. return <div style={style} className="custom-option" onClick={spyOnClick} onMouseEnter={spyOnMouseEnter}>
  945. <div className='option-right'>
  946. {label}
  947. </div>
  948. </div>
  949. };
  950. let props = {
  951. defaultOpen: true,
  952. optionList: [
  953. { value: 'abc', label: '抖音', },
  954. { value: 'jianying', label: '剪映', },
  955. ],
  956. renderOptionItem
  957. };
  958. let select = getSelect(props);
  959. let options = select.find('.custom-option');
  960. expect(options.length).toEqual(2);
  961. options.at(0).simulate('click');
  962. expect(spyOnClick.callCount).toEqual(1);
  963. options.at(1).simulate('mouseenter');
  964. expect(spyOnMouseEnter.callCount).toEqual(1);
  965. });
  966. it('customTrigger', () => {
  967. const triggerRender = ({ value, ...rest }) => {
  968. return (
  969. <div className="custom-triger">
  970. trigger
  971. </div>
  972. );
  973. };
  974. let props = {
  975. triggerRender,
  976. };
  977. let select = getSelect(props);
  978. let trigger = select.find('.custom-triger');
  979. expect(trigger.length).toEqual(1);
  980. expect(trigger.at(0).text()).toEqual('trigger');
  981. trigger.at(0).simulate('click')
  982. expect(select.find('.semi-select-option').length).toEqual(defaultList.length);
  983. });
  984. it('test keyboard press', () => {
  985. let props = {
  986. defaultOpen: true,
  987. optionList: [
  988. { value: 'abc', label: 'Abc' },
  989. { value: 'hotsoon', label: 'Hotsoon' },
  990. { value: 'pipixia', label: 'Pipixia' },
  991. { value: 'toutiao', label: 'TopBuzz' },
  992. ],
  993. };
  994. let select = getSelect(props);
  995. // press ⬇️
  996. select.find(`.${BASE_CLASS_PREFIX}-select`).simulate('keydown', { keyCode: keyCode.DOWN });
  997. expect(select.find(`.${BASE_CLASS_PREFIX}-select-option`).at(0).hasClass(`${BASE_CLASS_PREFIX}-select-option-focused`)).toBe(true);
  998. // press ⬆️
  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, uncontroled 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, controled 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 keyborard event
  1197. // TODO can't remove tag when option is diabled
  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. });