select.test.js 51 KB

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