select.test.js 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323
  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 waiting (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 disabled 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. });
  515. it('emptyContent', () => {
  516. let emptyContent = 'no data';
  517. let props = {
  518. filter: true,
  519. emptyContent,
  520. };
  521. const select = getSelect(props);
  522. // click to show input
  523. select.find(`.${BASE_CLASS_PREFIX}-select`).simulate('click', {});
  524. let inputValue = 'semi';
  525. let event = { target: { value: inputValue } };
  526. select.find('input').simulate('change', event);
  527. expect(select.find(`.${BASE_CLASS_PREFIX}-select-option-empty`).text()).toEqual(emptyContent);
  528. });
  529. it('option value & label', () => {
  530. let spyOnChange = sinon.spy(() => {});
  531. let props = {
  532. optionList: [{ label: 'semi', value: 'bytedance' }],
  533. defaultOpen: true,
  534. onChange: spyOnChange,
  535. };
  536. const select = getSelect(props);
  537. let optionList = select.find(`.${BASE_CLASS_PREFIX}-select-option-list`).children();
  538. expect(optionList.at(0).text()).toEqual('semi');
  539. const nativeEvent = { nativeEvent: { stopImmediatePropagation: noop } };
  540. optionList.at(0).simulate('click', nativeEvent);
  541. expect(spyOnChange.calledWithMatch('bytedance')).toEqual(true);
  542. });
  543. it('option.value is number', () => {
  544. let spyOnChange = sinon.spy(() => {});
  545. let props = {
  546. optionList: [{ label: 'semi', value: 0 }],
  547. defaultOpen: true,
  548. onChange: spyOnChange,
  549. };
  550. const select = getSelect(props);
  551. let optionList = select.find(`.${BASE_CLASS_PREFIX}-select-option-list`).children();
  552. expect(optionList.at(0).text()).toEqual('semi');
  553. const nativeEvent = { nativeEvent: { stopImmediatePropagation: noop } };
  554. optionList.at(0).simulate('click', nativeEvent);
  555. expect(spyOnChange.calledWithMatch(0)).toEqual(true);
  556. });
  557. it('renderSelectedItem, single', () => {
  558. const spyRSI = sinon.spy(option => {
  559. return option.value + '-' + option.label;
  560. });
  561. let props = {
  562. renderSelectedItem: spyRSI,
  563. defaultValue: 'abc',
  564. };
  565. const select = getSelect(props);
  566. expect(select.find(`.${BASE_CLASS_PREFIX}-select-selection-text`).text()).toEqual('abc-Abc');
  567. expect(spyRSI.calledWith({ value: 'abc', label: 'Abc' }));
  568. });
  569. it('renderSelectedItem, single & value = 0, not exist in optionList', () => {
  570. // test value = 0 & not match in optionList
  571. const spyRSI2 = sinon.spy(option => option.label + 1);
  572. let props2 = {
  573. renderSelectedItem: spyRSI2,
  574. value: 0,
  575. };
  576. const select2 = getSelect(props2);
  577. expect(select2.find(`.${BASE_CLASS_PREFIX}-select-selection-text`).text()).toEqual('1');
  578. expect(spyRSI2.calledWith({ value: 0, label: 0 }));
  579. });
  580. it('renderSelectedItem - multiple', () => {
  581. const spyRSI = sinon.spy((option, opts) => {
  582. let content = option.value + '-' + option.extra;
  583. return {
  584. isRenderInTag: true,
  585. content,
  586. };
  587. });
  588. let props = {
  589. optionList: [
  590. { value: 'abc', label: 'Abc', extra: 'a1' },
  591. { value: 'hotsoon', label: 'Hotsoon', extra: 'b2' },
  592. { value: 'pipixia', label: 'Pipixia', extra: 'c3' },
  593. { value: 'toutiao', label: 'TopBuzz', extra: 'd4' },
  594. ],
  595. renderSelectedItem: spyRSI,
  596. defaultValue: ['abc', 'hotsoon'],
  597. multiple: true,
  598. };
  599. const select = getSelect(props);
  600. let tags = select.find(`.${BASE_CLASS_PREFIX}-tag-content`);
  601. expect(tags.at(0).text()).toEqual('abc-a1');
  602. expect(tags.at(1).text()).toEqual('hotsoon-b2');
  603. });
  604. it('renderSelectedItem - multiple - isRenderInTag: false', () => {
  605. let item1, item2;
  606. const spyRSI = sinon.spy((option, opts) => {
  607. let content = <div className={opts.index}>{option.value + '-' + option.extra}</div>;
  608. if (opts.index === 0) {
  609. item1 = content;
  610. } else if (opts.index === 1) {
  611. item2 = content;
  612. }
  613. return {
  614. isRenderInTag: false,
  615. content,
  616. };
  617. });
  618. let props = {
  619. optionList: [
  620. { value: 'abc', label: 'Abc', extra: 'a1' },
  621. { value: 'hotsoon', label: 'Hotsoon', extra: 'b2' },
  622. { value: 'pipixia', label: 'Pipixia', extra: 'c3' },
  623. { value: 'toutiao', label: 'TopBuzz', extra: 'd4' },
  624. ],
  625. renderSelectedItem: spyRSI,
  626. defaultValue: ['abc', 'hotsoon'],
  627. multiple: true,
  628. };
  629. const select = getSelect(props);
  630. const items = select.find(`.${BASE_CLASS_PREFIX}-select-content-wrapper`).children();
  631. expect(items.at(0).contains(item1));
  632. expect(items.at(1).contains(item2));
  633. });
  634. it('defaultActiveFirstOption', () => {
  635. const props = {
  636. defaultActiveFirstOption: true,
  637. defaultOpen: true,
  638. };
  639. const select = getSelect(props);
  640. // expect first option active
  641. expect(select.find(`.${BASE_CLASS_PREFIX}-select-option-focused`).text()).toEqual('Abc');
  642. });
  643. it('onSelect', () => {
  644. // trigger onSelect when option has been selected
  645. let spyOnSelect = sinon.spy((value, option) => {});
  646. let props = {
  647. defaultOpen: true,
  648. onSelect: spyOnSelect,
  649. };
  650. let select = getSelect(props);
  651. let options = select.find(`.${BASE_CLASS_PREFIX}-select-option-list`).children();
  652. let firstOption = options.at(0);
  653. const nativeEvent = { nativeEvent: { stopImmediatePropagation: noop } };
  654. firstOption.simulate('click', nativeEvent);
  655. expect(spyOnSelect.calledOnce).toBe(true);
  656. expect(spyOnSelect.calledWith('abc', { value: 'abc', label: 'Abc' })).toBe(true);
  657. expect(spyOnSelect.calledWith('abc', { value: 'abc', label: 'Abc', extraKey: true })).toBe(false);
  658. });
  659. it('onDeselect', () => {
  660. // trigger onDeselect when option is deselected
  661. let onDeselect = (value, option) => {};
  662. let spyOnDeselect = sinon.spy(onDeselect);
  663. let props = {
  664. multiple: true,
  665. spyOnDeselect,
  666. defaultOpen: true,
  667. defaultValue: ['abc', 'hotsoon'],
  668. onDeselect: spyOnDeselect,
  669. };
  670. const select = getSelect(props);
  671. let options = select.find(`.${BASE_CLASS_PREFIX}-select-option-list`).children();
  672. const secondOption = options.at(1);
  673. const nativeEvent = { nativeEvent: { stopImmediatePropagation: noop } };
  674. secondOption.simulate('click', nativeEvent);
  675. expect(spyOnDeselect.calledOnce).toBe(true);
  676. expect(spyOnDeselect.calledWith('hotsoon', { value: 'hotsoon', label: 'Hotsoon' })).toBe(true);
  677. expect(spyOnDeselect.calledWith('hotsoon', { value: 'hotsoon', label: 'Hotsoon', extraKey: true })).toBe(false);
  678. });
  679. it('onChange (single)', () => {
  680. let spyOnChange = sinon.spy((value, option) => {});
  681. let props = {
  682. defaultOpen: true,
  683. onChange: spyOnChange,
  684. };
  685. let select = getSelect(props);
  686. let options = select.find(`.${BASE_CLASS_PREFIX}-select-option-list`).children();
  687. let firstOption = options.at(0);
  688. const nativeEvent = { nativeEvent: { stopImmediatePropagation: noop } };
  689. firstOption.simulate('click', nativeEvent);
  690. expect(spyOnChange.calledOnce).toBe(true);
  691. expect(spyOnChange.calledWith('abc')).toBe(true);
  692. });
  693. it('onChange (multiple)', () => {
  694. let spyOnChange = sinon.spy((value, option) => {});
  695. let props = {
  696. defaultOpen: true,
  697. multiple: true,
  698. onChange: spyOnChange,
  699. };
  700. let select = getSelect(props);
  701. let options = select.find(`.${BASE_CLASS_PREFIX}-select-option-list`).children();
  702. const nativeEvent = { nativeEvent: { stopImmediatePropagation: noop } };
  703. options.at(0).simulate('click', nativeEvent);
  704. options.at(1).simulate('click', nativeEvent);
  705. expect(spyOnChange.callCount).toEqual(2);
  706. expect(spyOnChange.getCall(0).args[0]).toEqual(['abc']);
  707. expect(spyOnChange.getCall(1).args[0]).toEqual(['abc', 'hotsoon']);
  708. });
  709. it('onChange + onChangeWithObject (single)', () => {
  710. let spyOnChange = sinon.spy((value, option) => {});
  711. let props = {
  712. defaultOpen: true,
  713. onChangeWithObject: true,
  714. onChange: spyOnChange,
  715. };
  716. let select = getSelect(props);
  717. let options = select.find(`.${BASE_CLASS_PREFIX}-select-option-list`).children();
  718. let firstOption = options.at(0);
  719. const nativeEvent = { nativeEvent: { stopImmediatePropagation: noop } };
  720. firstOption.simulate('click', nativeEvent);
  721. expect(spyOnChange.calledWith({ value: 'abc', label: 'Abc' })).toBe(true);
  722. });
  723. it('onChange + onChangeWithObject (multiple)', () => {
  724. let spyOnChange = sinon.spy((value, option) => {});
  725. let props = {
  726. defaultOpen: true,
  727. onChangeWithObject: true,
  728. multiple: true,
  729. onChange: spyOnChange,
  730. };
  731. let select = getSelect(props);
  732. let options = select.find(`.${BASE_CLASS_PREFIX}-select-option-list`).children();
  733. const nativeEvent = { nativeEvent: { stopImmediatePropagation: noop } };
  734. options.at(0).simulate('click', nativeEvent);
  735. options.at(1).simulate('click', nativeEvent);
  736. expect(spyOnChange.callCount).toEqual(2);
  737. expect(spyOnChange.getCall(0).args[0]).toEqual([{ value: 'abc', label: 'Abc' }]);
  738. expect(spyOnChange.getCall(1).args[0]).toEqual([
  739. { value: 'abc', label: 'Abc' },
  740. { value: 'hotsoon', label: 'Hotsoon' },
  741. ]);
  742. });
  743. it('【value】controlled mode', () => {
  744. let spyOnChange = sinon.spy((value, option) => {});
  745. let props = {
  746. value: 'abc',
  747. };
  748. let select = getSelect(props);
  749. expect(select.find(`.${BASE_CLASS_PREFIX}-select-selection-text`).getDOMNode().textContent).toEqual('Abc');
  750. select.setProps({ value: 'hotsoon' });
  751. select.update();
  752. expect(select.find(`.${BASE_CLASS_PREFIX}-select-selection-text`).getDOMNode().textContent).toEqual('Hotsoon');
  753. select.setProps({ value: undefined });
  754. select.update();
  755. expect(select.find(`.${BASE_CLASS_PREFIX}-select-selection-text`).getDOMNode().textContent).toEqual('');
  756. select.unmount();
  757. let singleProps = {
  758. value: 'abc',
  759. optionList: defaultList,
  760. defaultOpen: true,
  761. onChange: spyOnChange,
  762. };
  763. select = getSelect(singleProps);
  764. let options = select.find(`.${BASE_CLASS_PREFIX}-select-option-list`).children();
  765. const nativeEvent = { nativeEvent: { stopImmediatePropagation: noop } };
  766. options.at(1).simulate('click', nativeEvent);
  767. expect(spyOnChange.getCall(0).args[0]).toEqual('hotsoon');
  768. select.unmount();
  769. let spyMOnChange = sinon.spy((value, option) => {});
  770. let spyMOnClear = sinon.spy(() => {});
  771. let multipleProps = {
  772. value: '',
  773. optionList: defaultList,
  774. defaultOpen: true,
  775. multiple: true,
  776. filter: true,
  777. onChange: spyMOnChange,
  778. showClear: true,
  779. onClear: spyMOnClear,
  780. };
  781. select = getSelect(multipleProps);
  782. let mOptions = select.find(`.${BASE_CLASS_PREFIX}-select-option-list`).children();
  783. mOptions.at(1).simulate('click', nativeEvent);
  784. expect(spyMOnChange.getCall(0).args[0]).toEqual(['hotsoon']);
  785. // TODO
  786. // test
  787. });
  788. it('【onBlur/onFocus】', () => {
  789. let spyOnBlur = sinon.spy((value, option) => {
  790. });
  791. let spyOnFocus = sinon.spy((value, option) => {
  792. });
  793. let props = {
  794. onBlur: spyOnBlur,
  795. onFocus: spyOnFocus,
  796. };
  797. let select = getSelect(props);
  798. let trigger = select.find('.semi-select');
  799. const nativeEvent = { nativeEvent: { stopImmediatePropagation: noop } };
  800. trigger.simulate('click', nativeEvent);
  801. expect(spyOnFocus.callCount).toEqual(1);
  802. // Since there is no mechanism such as event bubbling in enzyme + jsdom, the blur event can only be triggered manually on the blur element,
  803. // and the blur of the `a element` cannot be achieved through the focus `b element`.
  804. // Adapt to A11y requirements, close the panel will not call the onBlur func
  805. select.instance().close();
  806. expect(spyOnBlur.callCount).toEqual(0);
  807. select.unmount();
  808. });
  809. it('【autoFocus】- filter = false', () => {
  810. // should focus triggerElement after mounted
  811. let spyOnBlur = sinon.spy((value, option) => {
  812. debugger
  813. });
  814. let spyOnFocus = sinon.spy((value, option) => {
  815. debugger
  816. });
  817. let props = {
  818. onBlur: spyOnBlur,
  819. onFocus: spyOnFocus,
  820. autoFocus: true,
  821. };
  822. let select = getSelect(props);
  823. // should not trigger focus when autoFocus
  824. expect(spyOnFocus.callCount).toEqual(0);
  825. expect(select.exists(`.${BASE_CLASS_PREFIX}-select-focus`)).toEqual(true);
  826. select.unmount();
  827. });
  828. it('【autoFocus】- filter = true', () => {
  829. // autoFocus should auto Focus input element when filter is true
  830. let props = {
  831. autoFocus: true,
  832. filter: true
  833. };
  834. let select = getSelect(props);
  835. expect(select.exists(`.${BASE_CLASS_PREFIX}-select-focus`)).toEqual(true);
  836. expect(select.exists(`.${BASE_CLASS_PREFIX}-input-wrapper-focus`)).toEqual(true);
  837. select.unmount();
  838. });
  839. it('【autoFocus】 & onBlur when autoFocus = true', () => {
  840. // autoFocus should trigger onBlur when click other element directly (dropdown not open)
  841. let spyOnBlur = sinon.spy((value, option) => {
  842. });
  843. let props = {
  844. autoFocus: true,
  845. onBlur: spyOnBlur,
  846. }
  847. // but we can't test this case, Orz
  848. // Since there is no mechanism such as event bubbling in enzyme + jsdom, the blur event can only be triggered manually on the blur element,
  849. // and the blur of the `a element` cannot be achieved through the focus `b element`.
  850. // mock blur event on trigger element
  851. let select = getSelect(props);
  852. let trigger = select.find('.semi-select');
  853. const nativeEvent = { nativeEvent: { stopImmediatePropagation: noop } };
  854. trigger.simulate('blur', nativeEvent);
  855. expect(spyOnBlur.callCount).toEqual(1);
  856. });
  857. it('virtual', () => {
  858. let spyOnChange = sinon.spy((value) => {
  859. });
  860. let optionList = Array.from({ length: 100 }, (v, i) => ({ label: `option-${i}`, value: i }));
  861. let props = {
  862. virtualize: {
  863. itemSize: 36, // px
  864. },
  865. defaultOpen: true,
  866. optionList,
  867. onChange: spyOnChange,
  868. };
  869. let select = getSelect(props);
  870. let options = select.find('.semi-select-option');
  871. let firstOption = options.children().at(0);
  872. const nativeEvent = { nativeEvent: { stopImmediatePropagation: noop } };
  873. firstOption.simulate('click', nativeEvent);
  874. expect(spyOnChange.callCount).toEqual(1);
  875. expect(spyOnChange.calledWithMatch(0)).toEqual(true);
  876. });
  877. it('OptionGroup', () => {
  878. let optionList = [
  879. <Select.OptGroup key={1} label="Group1">
  880. <Select.Option value="a-1">a-1</Select.Option>
  881. <Select.Option value="a-2">a-2</Select.Option>
  882. </Select.OptGroup>,
  883. <Select.OptGroup key={2} label="Group2">
  884. <Select.Option value="b-1">b-1</Select.Option>
  885. <Select.Option value="b-2">b-2</Select.Option>
  886. </Select.OptGroup>,
  887. // last option without label
  888. <Select.OptGroup key={3}>
  889. <Select.Option value="c-1">c-1</Select.Option>
  890. </Select.OptGroup>
  891. ]
  892. let props = {
  893. defaultOpen: true,
  894. children: optionList,
  895. };
  896. let select = getSelect(props);
  897. let options = select.find('.semi-select-group');
  898. expect(options.length).toEqual(2);
  899. expect(options.at(0).text()).toEqual('Group1');
  900. expect(options.at(1).text()).toEqual('Group2');
  901. });
  902. it('empty', () => {
  903. let props = {
  904. defaultOpen: true,
  905. optionList: [],
  906. emptyContent: 'empty'
  907. };
  908. let select = getSelect(props);
  909. let options = select.find('.semi-select-option.semi-select-option-empty');
  910. expect(options.length).toEqual(1);
  911. expect(options.at(0).text()).toEqual(props.emptyContent);
  912. select.setProps({
  913. emptyContent: null
  914. })
  915. select.update()
  916. expect(select.find('.semi-select-option').length).toEqual(0);
  917. });
  918. it('renderOptionItem onClick onMouseEnter', () => {
  919. let spyOnMouseEnter = sinon.spy((value) => {
  920. });
  921. let spyOnClick = sinon.spy((value) => {
  922. });
  923. const renderOptionItem = renderProps => {
  924. const {
  925. disabled,
  926. selected,
  927. label,
  928. value,
  929. focused,
  930. className,
  931. style,
  932. onMouseEnter,
  933. onClick,
  934. empty,
  935. emptyContent,
  936. ...rest
  937. } = renderProps;
  938. return <div style={style} className="custom-option" onClick={spyOnClick} onMouseEnter={spyOnMouseEnter}>
  939. <div className='option-right'>
  940. {label}
  941. </div>
  942. </div>
  943. };
  944. let props = {
  945. defaultOpen: true,
  946. optionList: [
  947. { value: 'abc', label: '抖音', },
  948. { value: 'jianying', label: '剪映', },
  949. ],
  950. renderOptionItem
  951. };
  952. let select = getSelect(props);
  953. let options = select.find('.custom-option');
  954. expect(options.length).toEqual(2);
  955. options.at(0).simulate('click');
  956. expect(spyOnClick.callCount).toEqual(1);
  957. options.at(1).simulate('mouseenter');
  958. expect(spyOnMouseEnter.callCount).toEqual(1);
  959. });
  960. it('customTrigger', () => {
  961. const triggerRender = ({ value, ...rest }) => {
  962. return (
  963. <div className="custom-trigger">
  964. trigger
  965. </div>
  966. );
  967. };
  968. let props = {
  969. triggerRender,
  970. };
  971. let select = getSelect(props);
  972. let trigger = select.find('.custom-trigger');
  973. expect(trigger.length).toEqual(1);
  974. expect(trigger.at(0).text()).toEqual('trigger');
  975. trigger.at(0).simulate('click')
  976. expect(select.find('.semi-select-option').length).toEqual(defaultList.length);
  977. });
  978. it('test keyboard press', () => {
  979. let props = {
  980. defaultOpen: true,
  981. optionList: [
  982. { value: 'abc', label: 'Abc' },
  983. { value: 'hotsoon', label: 'Hotsoon' },
  984. { value: 'pipixia', label: 'Pipixia' },
  985. { value: 'toutiao', label: 'TopBuzz' },
  986. ],
  987. };
  988. let select = getSelect(props);
  989. // press ⬇️
  990. // since the defaultActiveFirstOption default to be true, after ⬇️, the second option focused
  991. select.find(`.${BASE_CLASS_PREFIX}-select`).simulate('keydown', { keyCode: keyCode.DOWN });
  992. expect(select.find(`.${BASE_CLASS_PREFIX}-select-option`).at(1).hasClass(`${BASE_CLASS_PREFIX}-select-option-focused`)).toBe(true);
  993. // press ⬆️
  994. select.find(`.${BASE_CLASS_PREFIX}-select`).simulate('keydown', { keyCode: keyCode.UP });
  995. select.find(`.${BASE_CLASS_PREFIX}-select`).simulate('keydown', { keyCode: keyCode.UP });
  996. expect(select.find(`.${BASE_CLASS_PREFIX}-select-option`).at(defaultList.length-1).hasClass(`${BASE_CLASS_PREFIX}-select-option-focused`)).toBe(true);
  997. // press ESC
  998. select.find(`.${BASE_CLASS_PREFIX}-select`).simulate('keydown', { keyCode: keyCode.ESC });
  999. expect(select.find(`.${BASE_CLASS_PREFIX}-select-option`).exists()).toBe(false);
  1000. // reopen select, press ⬇️ and ENTER, the first option should be selected
  1001. select.find(`.${BASE_CLASS_PREFIX}-select`).simulate('click', {});
  1002. select.find(`.${BASE_CLASS_PREFIX}-select`).simulate('keydown', { keyCode: keyCode.DOWN });
  1003. select.find(`.${BASE_CLASS_PREFIX}-select`).simulate('keydown', { keyCode: keyCode.ENTER });
  1004. expect(select.find(`.${BASE_CLASS_PREFIX}-select-selection-text`).text()).toBe(defaultList[0].label);
  1005. select.unmount();
  1006. // test whether backspace can skip disabled option
  1007. let dProps = {
  1008. defaultOpen: true,
  1009. optionList: [
  1010. { value: 'abc', label: 'Abc' },
  1011. { value: 'hotsoon', label: 'Hotsoon', disabled: true },
  1012. { value: 'pipixia', label: 'Pipixia' },
  1013. ],
  1014. defaultValue: ['hotsoon', 'abc'],
  1015. multiple: true,
  1016. };
  1017. let dSelect = getSelect(dProps);
  1018. dSelect.find(`.${BASE_CLASS_PREFIX}-select`).simulate('keydown', { keyCode: keyCode.BACKSPACE });
  1019. let selections = Array.from(dSelect.state().selections);
  1020. expect(selections[0][0]).toEqual('Hotsoon');
  1021. });
  1022. it('allowCreate', () => {
  1023. const props = {
  1024. multiple: true,
  1025. allowCreate: true,
  1026. filter: true,
  1027. optionList: []
  1028. };
  1029. const select = getSelect(props);
  1030. select.find(`.${BASE_CLASS_PREFIX}-select`).simulate('click', {});
  1031. select.find(`.${BASE_CLASS_PREFIX}-select .${BASE_CLASS_PREFIX}-input`).simulate('change', { target: { value: '1' } });
  1032. select.find(`.${BASE_CLASS_PREFIX}-select-option`).simulate('click', {});
  1033. expect(select.find(`.${BASE_CLASS_PREFIX}-select .semi-tag`).length).toBe(1);
  1034. select.find(`.${BASE_CLASS_PREFIX}-select .${BASE_CLASS_PREFIX}-input`).simulate('keydown', { keyCode: keyCode.BACKSPACE });
  1035. expect(select.find(`.${BASE_CLASS_PREFIX}-select .semi-tag`).length).toBe(0);
  1036. });
  1037. it('【onMouseEnter/onMouseLeave】', () => {
  1038. let spyEnter = sinon.spy((e) => {
  1039. });
  1040. let spyLeave = sinon.spy((e) => {
  1041. });
  1042. let props = {
  1043. onMouseEnter: spyEnter,
  1044. onMouseLeave: spyLeave,
  1045. };
  1046. let select = getSelect(props);
  1047. let trigger = select.find('.semi-select');
  1048. const nativeEvent = { nativeEvent: { stopImmediatePropagation: noop } };
  1049. trigger.simulate('mouseenter', nativeEvent);
  1050. expect(spyEnter.callCount).toEqual(1);
  1051. trigger.simulate('mouseleave', nativeEvent);
  1052. expect(spyLeave.callCount).toEqual(1);
  1053. select.unmount();
  1054. });
  1055. it('ref method', () => {
  1056. let r;
  1057. let props = {
  1058. ref: (ref) => { r = ref },
  1059. filter: true,
  1060. multiple: true,
  1061. optionList: defaultList,
  1062. };
  1063. let select = getSelect(props);
  1064. r.open();
  1065. expect(select.state().isOpen).toEqual(true);
  1066. r.close();
  1067. expect(select.state().isOpen).toEqual(false);
  1068. r.selectAll();
  1069. select.update();
  1070. expect(select.state().selections.size).toEqual(4);
  1071. r.deselectAll();
  1072. expect(select.state().selections.size).toEqual(0);
  1073. r.focus();
  1074. expect(document.activeElement.tagName).toEqual('INPUT');
  1075. select.unmount();
  1076. // selectAll not work when multiple is false
  1077. let r2;
  1078. let props2 = {
  1079. ref: (ref) => { r2 = ref },
  1080. filter: true,
  1081. optionList: defaultList,
  1082. };
  1083. let singleSelect = getSelect(props2);
  1084. r2.selectAll();
  1085. expect(singleSelect.state().selections.size).toEqual(0);
  1086. });
  1087. it('props optionList update after choose some option, uncontrolled mode', () => {
  1088. let props = {
  1089. defaultActiveFirstOption: true,
  1090. optionList: [
  1091. { value: 'abc', label: 'Abc' },
  1092. { value: 'hotsoon', label: 'Hotsoon' }
  1093. ],
  1094. defaultOpen: true,
  1095. multiple: true,
  1096. filter: true,
  1097. };
  1098. let select = getSelect(props);
  1099. let options = select.find(`.${BASE_CLASS_PREFIX}-select-option-list`).children();
  1100. const nativeEvent = { nativeEvent: { stopImmediatePropagation: noop } };
  1101. options.at(0).simulate('click', nativeEvent);
  1102. options.at(1).simulate('click', nativeEvent);
  1103. let newList = [
  1104. { value: 'pipixia', label: 'Pipixia' },
  1105. { value: 'toutiao', label: 'TopBuzz' },
  1106. ];
  1107. select.setProps({ optionList: newList });
  1108. select.update();
  1109. let selections = Array.from(select.state().selections);
  1110. expect(selections[0][0]).toEqual('Abc');
  1111. expect(selections[1][0]).toEqual('Hotsoon');
  1112. select.unmount();
  1113. let singleProps = {
  1114. defaultActiveFirstOption: true,
  1115. optionList: [
  1116. { value: 'abc', label: 'Abc' },
  1117. { value: 'hotsoon', label: 'Hotsoon' },
  1118. ],
  1119. defaultOpen: true,
  1120. };
  1121. let singleSelect = getSelect(singleProps);
  1122. let options2 = singleSelect.find(`.${BASE_CLASS_PREFIX}-select-option-list`).children();
  1123. options2.at(0).simulate('click', nativeEvent);
  1124. singleSelect.setProps({ optionList: newList });
  1125. singleSelect.update();
  1126. let selections2 = Array.from(singleSelect.state().selections);
  1127. expect(selections2[0][0]).toEqual('abc');
  1128. });
  1129. it('click tag close when multiple, controlled mode', () => {
  1130. let spyOnChange = sinon.spy((value) => {
  1131. });
  1132. let spyOnDeselect = sinon.spy((option) => {
  1133. });
  1134. let props = {
  1135. optionList: [
  1136. { value: 'abc', label: 'Abc' },
  1137. { value: 'hotsoon', label: 'Hotsoon' },
  1138. ],
  1139. multiple: true,
  1140. value: ['abc', 'hotsoon'],
  1141. onChange: spyOnChange,
  1142. onDeselect: spyOnDeselect,
  1143. };
  1144. let select = getSelect(props);
  1145. let tagClose = select.find('.semi-tag-close').children();
  1146. const nativeEvent = { nativeEvent: { stopImmediatePropagation: noop } };
  1147. tagClose.at(0).simulate('click', nativeEvent);
  1148. expect(spyOnDeselect.calledWith('abc'));
  1149. expect(spyOnChange.calledWith(['hotsoon']));
  1150. });
  1151. it('autoClearSearchValue', () => {
  1152. // default usage
  1153. let optionList = Array.from({ length: 100 }, (v, i) => ({ label: `option-${i}`, value: i }));
  1154. let props = {
  1155. multiple: true,
  1156. optionList: optionList,
  1157. defaultOpen: true,
  1158. filter: true,
  1159. };
  1160. let select = getSelect(props);
  1161. select.find(`.${BASE_CLASS_PREFIX}-select`).simulate('click', {});
  1162. let keyword = 'option';
  1163. let event = { target: { value: keyword } };
  1164. select.find('input').simulate('change', event);
  1165. let options = select.find(`.${BASE_CLASS_PREFIX}-select-option-list`).children();
  1166. const nativeEvent = { nativeEvent: { stopImmediatePropagation: noop } };
  1167. options.at(0).simulate('click', nativeEvent);
  1168. let inputValue = select.find('input').getDOMNode().value;
  1169. expect(inputValue).toEqual('');
  1170. });
  1171. it('autoClearSearchValue = false', () => {
  1172. let optionList = Array.from({ length: 100 }, (v, i) => ({ label: `option-${i}`, value: i }));
  1173. let props = {
  1174. multiple: true,
  1175. optionList: optionList,
  1176. defaultOpen: true,
  1177. autoClearSearchValue: false,
  1178. filter: true,
  1179. };
  1180. let select = getSelect(props);
  1181. select.find(`.${BASE_CLASS_PREFIX}-select`).simulate('click', {});
  1182. let keyword = 'option';
  1183. let event = { target: { value: keyword } };
  1184. select.find('input').simulate('change', event);
  1185. let options = select.find(`.${BASE_CLASS_PREFIX}-select-option-list`).children();
  1186. const nativeEvent = { nativeEvent: { stopImmediatePropagation: noop } };
  1187. options.at(0).simulate('click', nativeEvent);
  1188. let inputValue = select.find('input').getDOMNode().value;
  1189. expect(inputValue).toEqual(keyword);
  1190. });
  1191. // TODO ref selectAll \deselectAll when onChangeWithObject is true
  1192. // TODO when loading is true, do not response any keyboard event
  1193. // TODO can't remove tag when option is disabled
  1194. // it('allowCreate-renderCreateItem', ()=>{})
  1195. // it('autoAdjustOverflow', ()=>{})
  1196. // it('remote', ()=>{})
  1197. // it('【data】updateOptionList when data change', () => {
  1198. // let props = {
  1199. // defaultOpen: true,
  1200. // data: ['semi'],
  1201. // ...commonProps
  1202. // };
  1203. // let ac = getAc(props);
  1204. // let candidate = ac.find(`.${BASE_CLASS_PREFIX}-autocomplete-option-list`).children();
  1205. // expect(candidate.length).toEqual(1);
  1206. // expect(candidate.at(0).getDOMNode().textContent).toEqual('${BASE_CLASS_PREFIX}');
  1207. // ac.setProps({ data: ['ies', 'design']});
  1208. // ac.update();
  1209. // candidate = ac.find(`.${BASE_CLASS_PREFIX}-autocomplete-option-list`).children();
  1210. // expect(candidate.length).toEqual(2);
  1211. // expect(candidate.at(0).getDOMNode().textContent).toEqual('ies');
  1212. // expect(candidate.at(1).getDOMNode().textContent).toEqual('design');
  1213. // })
  1214. });