select.test.js 50 KB

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