tree.test.js 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097
  1. import { isEqual } from 'lodash';
  2. import { IconMapPin } from '@douyinfe/semi-icons';
  3. import { BASE_CLASS_PREFIX } from '../../../semi-foundation/base/constants';
  4. import { Tree, Button } from '../../index';
  5. const treeChildren = [
  6. {
  7. label: '北京',
  8. value: 'Beijing',
  9. key: 'beijing',
  10. },
  11. {
  12. label: '上海',
  13. value: 'Shanghai',
  14. key: 'shanghai',
  15. },
  16. ];
  17. const treeData = [
  18. {
  19. label: '亚洲',
  20. value: 'Yazhou',
  21. key: 'yazhou',
  22. children: [
  23. {
  24. label: '中国',
  25. value: 'Zhongguo',
  26. key: 'zhongguo',
  27. children: treeChildren,
  28. },
  29. {
  30. label: '日本',
  31. value: 'Riben',
  32. key: 'riben',
  33. children: [
  34. {
  35. label: '东京',
  36. value: 'Dongjing',
  37. key: 'dongjing'
  38. },
  39. {
  40. label: '大阪',
  41. value: 'Daban',
  42. key: 'daban'
  43. }
  44. ]
  45. },
  46. ],
  47. },
  48. {
  49. label: '北美洲',
  50. value: 'Beimeizhou',
  51. key: 'beimeizhou',
  52. children: [
  53. {
  54. label: '美国',
  55. value: 'Meiguo',
  56. key: 'meiguo'
  57. },
  58. {
  59. label: '加拿大',
  60. value: 'Jianada',
  61. key: 'jianada'
  62. }
  63. ]
  64. },
  65. ];
  66. const treeData2 = [
  67. {
  68. label: '亚洲',
  69. value: 'Asia',
  70. key: '0',
  71. children: [
  72. {
  73. label: '中国',
  74. value: 'China',
  75. key: '0-0',
  76. children: [
  77. {
  78. label: '北京',
  79. value: 'Beijing',
  80. key: '0-0-0',
  81. },
  82. {
  83. label: '上海',
  84. value: 'Shanghai',
  85. key: '0-0-1',
  86. },
  87. ],
  88. },
  89. ],
  90. },
  91. {
  92. label: '北美洲',
  93. value: 'North America',
  94. key: '1',
  95. }
  96. ];
  97. const treeData3 = [
  98. {
  99. label: '亚洲',
  100. value: 'Asia',
  101. key: '0',
  102. newLabel: '亚洲#1#yazhou',
  103. children: [
  104. {
  105. label: '中国',
  106. value: 'China',
  107. key: '0-0',
  108. newLabel: '中国#2#zhongguo',
  109. children: [
  110. {
  111. label: '北京',
  112. value: 'Beijing',
  113. key: '0-0-0',
  114. newLabel: '北京#3#beijing',
  115. },
  116. {
  117. label: '上海',
  118. value: 'Shanghai',
  119. key: '0-0-1',
  120. newLabel: '上海#4#shanghai',
  121. },
  122. ],
  123. },
  124. ],
  125. },
  126. {
  127. label: '北美洲',
  128. value: 'North America',
  129. newLabel: '北美洲#5#beimeizhou',
  130. key: '1',
  131. }
  132. ];
  133. const specialTreeData = [
  134. {
  135. label1: '亚洲',
  136. value1: 'Yazhou',
  137. key1: 'yazhou',
  138. children1: [
  139. {
  140. label1: '中国',
  141. value1: 'Zhongguo',
  142. key1: 'zhongguo',
  143. disabled1: true,
  144. children1: [
  145. {
  146. label1: '北京',
  147. value1: 'Beijing',
  148. key1: 'beijing',
  149. },
  150. {
  151. label1: '上海',
  152. value1: 'Shanghai',
  153. key1: 'shanghai',
  154. },
  155. ],
  156. },
  157. {
  158. label1: '日本',
  159. value1: 'Riben',
  160. key1: 'riben',
  161. children1: [
  162. {
  163. label1: '东京',
  164. value1: 'Dongjing',
  165. key1: 'dongjing',
  166. },
  167. {
  168. label1: '大阪',
  169. value1: 'Daban',
  170. key1: 'daban',
  171. },
  172. ],
  173. },
  174. ],
  175. },
  176. {
  177. label1: '北美洲',
  178. value1: 'Beimeizhou',
  179. key1: 'beimeizhou',
  180. children1: [
  181. {
  182. label1: '美国',
  183. value1: 'Meiguo',
  184. key1: 'meiguo',
  185. },
  186. {
  187. label1: '加拿大',
  188. value1: 'Jianada',
  189. key1: 'jianada',
  190. },
  191. ],
  192. },
  193. ];
  194. const defaultKeyMaps = {
  195. value: 'value1',
  196. key: 'key1',
  197. label: 'label1',
  198. children: 'children1',
  199. disabled: 'disabled1'
  200. }
  201. const dragNodeData = {"label":"亚洲","value":"Yazhou","key":"yazhou","children":[{"label":"中国","value":"Zhongguo","key":"zhongguo","children":[{"label":"北京","value":"Beijing","key":"beijing"},{"label":"上海","value":"Shanghai","key":"shanghai"}]},{"label":"日本","value":"Riben","key":"riben","children":[{"label":"东京","value":"Dongjing","key":"dongjing"},{"label":"大阪","value":"Daban","key":"daban"}]}],"expanded":false,"pos":"0-0"}
  202. const dropNodeData = {"label":"北美洲","value":"Beimeizhou","key":"beimeizhou","children":[{"label":"美国","value":"Meiguo","key":"meiguo"},{"label":"加拿大","value":"Jianada","key":"jianada"}],"expanded":false,"pos":"0-1"}
  203. function getTree(props) {
  204. props = { treeData: treeData, ...props }
  205. return mount(
  206. <Tree
  207. {...props}
  208. motion={true}
  209. />,
  210. {
  211. attachTo: document.getElementById('container')
  212. }
  213. );
  214. }
  215. describe('Tree', () => {
  216. beforeEach(() => {
  217. // Avoid `attachTo: document.body` Warning
  218. const div = document.createElement('div');
  219. div.setAttribute('id', 'container');
  220. document.body.appendChild(div);
  221. });
  222. afterEach(() => {
  223. const div = document.getElementById('container');
  224. if (div) {
  225. document.body.removeChild(div);
  226. }
  227. document.body.innerHTML = '';
  228. });
  229. it('className / style', () => {
  230. let props = {
  231. className: 'test',
  232. style: { height: 420 },
  233. };
  234. let tree = getTree(props);
  235. expect(tree.hasClass('test')).toEqual(true);
  236. expect(tree.find('div.test')).toHaveStyle('height', 420);
  237. });
  238. it('blockNode', () => {
  239. let tree = getTree({
  240. defaultValue: 'Beijing'
  241. });
  242. expect(tree.find(`.${BASE_CLASS_PREFIX}-tree-option-list-block`).exists()).toEqual(true);
  243. tree.setProps({ blockNode: false });
  244. expect(tree.find(`.${BASE_CLASS_PREFIX}-tree-option-list-block`).exists()).toEqual(false);
  245. });
  246. it('empty data', () => {
  247. let tree = getTree({
  248. treeData: []
  249. });
  250. let node = tree.find(`.${BASE_CLASS_PREFIX}-tree-option`);
  251. expect(node.length).toEqual(1);
  252. expect(node.hasClass(`${BASE_CLASS_PREFIX}-tree-option-empty`)).toEqual(true);
  253. });
  254. it('defaultValue', () => {
  255. // auto expand parent, if node exist means parent is open
  256. let tree = getTree({
  257. defaultValue: 'Beijing'
  258. });
  259. let selectedNode = tree.find(`.${BASE_CLASS_PREFIX}-tree-option-selected`);
  260. expect(selectedNode.instance().textContent).toEqual('北京');
  261. tree.unmount();
  262. // array case only select first item
  263. let tree2 = getTree({
  264. defaultValue: ['Riben', 'Beijing']
  265. });
  266. let selectedNode2 = tree2.find(`.${BASE_CLASS_PREFIX}-tree-option-selected`);
  267. expect(selectedNode2.instance().textContent).toEqual('日本');
  268. expect(tree2.find(`.${BASE_CLASS_PREFIX}-tree-option.${BASE_CLASS_PREFIX}-tree-option-level-2`).at(0).hasClass(`${BASE_CLASS_PREFIX}-tree-option-collapsed`)).toEqual(true);
  269. })
  270. it('defaultExpandedKeys', () => {
  271. // auto expand parent
  272. let tree = getTree({
  273. defaultExpandedKeys: ['zhongguo', 'beimeizhou']
  274. });
  275. // yazhou beimeizhou
  276. let topNode = tree.find(`.${BASE_CLASS_PREFIX}-tree-option.${BASE_CLASS_PREFIX}-tree-option-level-1`);
  277. expect(topNode.at(0).hasClass(`${BASE_CLASS_PREFIX}-tree-option-collapsed`)).toEqual(false);
  278. expect(topNode.at(1).hasClass(`${BASE_CLASS_PREFIX}-tree-option-collapsed`)).toEqual(false);
  279. // zhongguo riben
  280. let children = tree.find(`.${BASE_CLASS_PREFIX}-tree-option.${BASE_CLASS_PREFIX}-tree-option-level-2`);
  281. expect(children.at(0).hasClass(`${BASE_CLASS_PREFIX}-tree-option-collapsed`)).toEqual(false);
  282. expect(children.at(1).hasClass(`${BASE_CLASS_PREFIX}-tree-option-collapsed`)).toEqual(true);
  283. })
  284. it('defaultExpandAll', () => {
  285. let tree = getTree({
  286. defaultExpandAll: true
  287. });
  288. let nodes = tree.find(`.${BASE_CLASS_PREFIX}-tree-option`);
  289. let collapsed = tree.find(`.${BASE_CLASS_PREFIX}-tree-option-collapsed`);
  290. expect(nodes.length).toEqual(10);
  291. expect(collapsed.length).toEqual(0);
  292. tree.setProps({ treeData: treeData2});
  293. tree.update();
  294. const nodes2 = tree.find(`.${BASE_CLASS_PREFIX}-tree-option`);
  295. const collapsed2 = tree.find(`.${BASE_CLASS_PREFIX}-tree-option-collapsed`);
  296. expect(nodes2.length).toEqual(2);
  297. expect(collapsed2.length).toEqual(2);
  298. })
  299. it('expandAll', () => {
  300. const tree = getTree({
  301. expandAll: true
  302. });
  303. const nodes = tree.find(`.${BASE_CLASS_PREFIX}-tree-option`);
  304. const collapsed = tree.find(`.${BASE_CLASS_PREFIX}-tree-option-collapsed`);
  305. expect(nodes.length).toEqual(10);
  306. expect(collapsed.length).toEqual(0);
  307. tree.setProps({ treeData: treeData2});
  308. tree.update();
  309. const nodes2 = tree.find(`.${BASE_CLASS_PREFIX}-tree-option`);
  310. const collapsed2 = tree.find(`.${BASE_CLASS_PREFIX}-tree-option-collapsed`);
  311. expect(nodes2.length).toEqual(5);
  312. expect(collapsed2.length).toEqual(0);
  313. })
  314. it('directory mode', () => {
  315. let props = {
  316. defaultExpandedKeys: ['beimeizhou'],
  317. directory: true,
  318. };
  319. let tree = getTree(props);
  320. let topNode = tree.find(`.${BASE_CLASS_PREFIX}-tree-option.${BASE_CLASS_PREFIX}-tree-option-level-1`);
  321. expect(topNode.at(1).find(`.${BASE_CLASS_PREFIX}-icon-folder_open`).exists()).toEqual(true);
  322. expect(topNode.at(0).find(`.${BASE_CLASS_PREFIX}-icon-folder`).exists()).toEqual(true);
  323. expect(tree.find(`.${BASE_CLASS_PREFIX}-tree-option.${BASE_CLASS_PREFIX}-tree-option-level-2`).at(0).find(`.${BASE_CLASS_PREFIX}-icon-file`).exists()).toEqual(true);
  324. });
  325. it('custom icon', () => {
  326. let props = {
  327. defaultExpandAll: true,
  328. icon: (<IconMapPin />)
  329. };
  330. let tree = getTree(props);
  331. expect(tree.find(`.${BASE_CLASS_PREFIX}-icon-map_pin`).length).toEqual(10);
  332. });
  333. it('if expandedKeys values work', () => {
  334. // auto expand parent
  335. let tree = getTree({
  336. expandedKeys: ['beimeizhou']
  337. });
  338. // yazhou beimeizhou
  339. let topNode = tree.find(`.${BASE_CLASS_PREFIX}-tree-option.${BASE_CLASS_PREFIX}-tree-option-level-1`);
  340. expect(topNode.at(0).hasClass(`${BASE_CLASS_PREFIX}-tree-option-collapsed`)).toEqual(true);
  341. expect(topNode.at(1).hasClass(`${BASE_CLASS_PREFIX}-tree-option-collapsed`)).toEqual(false);
  342. tree.setProps({ expandedKeys: ['yazhou', 'zhongguo', 'beimeizhou'] });
  343. tree.update();
  344. // zhongguo riben
  345. let children = tree.find(`.${BASE_CLASS_PREFIX}-tree-option.${BASE_CLASS_PREFIX}-tree-option-level-2`);
  346. expect(children.at(0).hasClass(`${BASE_CLASS_PREFIX}-tree-option-collapsed`)).toEqual(false);
  347. expect(children.at(1).hasClass(`${BASE_CLASS_PREFIX}-tree-option-collapsed`)).toEqual(true);
  348. });
  349. it('expandedKeys + autoExpandParent', () => {
  350. // auto expand parent is always true when mounted
  351. let tree = getTree({
  352. expandedKeys: ['beimeizhou']
  353. });
  354. // yazhou beimeizhou
  355. let topNode = tree.find(`.${BASE_CLASS_PREFIX}-tree-option.${BASE_CLASS_PREFIX}-tree-option-level-1`);
  356. expect(topNode.at(0).hasClass(`${BASE_CLASS_PREFIX}-tree-option-collapsed`)).toEqual(true);
  357. expect(topNode.at(1).hasClass(`${BASE_CLASS_PREFIX}-tree-option-collapsed`)).toEqual(false);
  358. tree.setProps({ expandedKeys: ['riben'] });
  359. tree.update();
  360. topNode = tree.find(`.${BASE_CLASS_PREFIX}-tree-option.${BASE_CLASS_PREFIX}-tree-option-level-1`);
  361. expect(topNode.at(0).hasClass(`${BASE_CLASS_PREFIX}-tree-option-collapsed`)).toEqual(true);
  362. expect(topNode.at(1).hasClass(`${BASE_CLASS_PREFIX}-tree-option-collapsed`)).toEqual(true);
  363. // autoExpandParent
  364. tree.setProps({ autoExpandParent: true });
  365. tree.update();
  366. topNode = tree.find(`.${BASE_CLASS_PREFIX}-tree-option.${BASE_CLASS_PREFIX}-tree-option-level-1`);
  367. expect(topNode.at(0).hasClass(`${BASE_CLASS_PREFIX}-tree-option-collapsed`)).toEqual(false);
  368. expect(topNode.at(1).hasClass(`${BASE_CLASS_PREFIX}-tree-option-collapsed`)).toEqual(true);
  369. });
  370. it('if expand behavior works / onExpand', () => {
  371. const nativeEvent = { nativeEvent: { stopImmediatePropagation: () => { } } }
  372. let spyOnExpand = sinon.spy(() => { });
  373. let tree = getTree({
  374. onExpand: spyOnExpand,
  375. });
  376. // yazhou
  377. let topNodeAsia = tree.find(`.${BASE_CLASS_PREFIX}-tree-option.${BASE_CLASS_PREFIX}-tree-option-level-1`).at(0);
  378. let expandIcon = topNodeAsia.find(`.${BASE_CLASS_PREFIX}-tree-option-expand-icon`).at(0);
  379. // expand yazhou
  380. expandIcon.simulate('click', nativeEvent);
  381. expect(spyOnExpand.calledOnce).toBe(true);
  382. expect(spyOnExpand.calledWithMatch(["yazhou"], { expanded: true, node: { key: 'yazhou' } })).toEqual(true);
  383. // yazhou beimeizhou
  384. let topNode = tree.find(`.${BASE_CLASS_PREFIX}-tree-option.${BASE_CLASS_PREFIX}-tree-option-level-1`);
  385. expect(topNode.at(0).hasClass(`${BASE_CLASS_PREFIX}-tree-option-collapsed`)).toEqual(false);
  386. expect(topNode.at(1).hasClass(`${BASE_CLASS_PREFIX}-tree-option-collapsed`)).toEqual(true);
  387. // collapse yazhou
  388. expandIcon.simulate('click', nativeEvent);
  389. expect(spyOnExpand.calledWithMatch([], { expanded: false, node: { key: 'yazhou' } })).toEqual(true);
  390. topNode = tree.find(`.${BASE_CLASS_PREFIX}-tree-option.${BASE_CLASS_PREFIX}-tree-option-level-1`);
  391. expect(topNode.at(0).hasClass(`${BASE_CLASS_PREFIX}-tree-option-collapsed`)).toEqual(true);
  392. });
  393. it('if expandedKeys controlled work / onExpand', () => {
  394. const nativeEvent = { nativeEvent: { stopImmediatePropagation: () => { } } }
  395. let spyOnExpand = sinon.spy(() => { });
  396. let tree = getTree({
  397. expandedKeys: ['beimeizhou'],
  398. onExpand: spyOnExpand,
  399. });
  400. // yazhou
  401. let topNodeAsia = tree.find(`.${BASE_CLASS_PREFIX}-tree-option.${BASE_CLASS_PREFIX}-tree-option-level-1`).at(0);
  402. let expandIcon = topNodeAsia.find(`.${BASE_CLASS_PREFIX}-tree-option-expand-icon`).at(0);
  403. // expand yazhou
  404. expandIcon.simulate('click', nativeEvent);
  405. expect(spyOnExpand.calledWithMatch(["beimeizhou", "yazhou"], { expanded: true, node: { key: 'yazhou' } })).toEqual(true);
  406. // zhongguo riben
  407. let topNode = tree.find(`.${BASE_CLASS_PREFIX}-tree-option.${BASE_CLASS_PREFIX}-tree-option-level-1`);
  408. expect(topNode.at(0).hasClass(`${BASE_CLASS_PREFIX}-tree-option-collapsed`)).toEqual(true);
  409. });
  410. it('select item / onSelect / onChange', () => {
  411. let spyOnSelect = sinon.spy(() => { });
  412. let spyOnChange = sinon.spy(() => { });
  413. let tree = getTree({
  414. defaultExpandAll: true,
  415. onSelect: spyOnSelect,
  416. onChange: spyOnChange,
  417. });
  418. let nodeBeijing = tree.find(`.${BASE_CLASS_PREFIX}-tree-option.${BASE_CLASS_PREFIX}-tree-option-level-3`).at(0);
  419. // select beijing
  420. nodeBeijing.simulate('click');
  421. // onSelect & onChange
  422. expect(spyOnSelect.calledOnce).toBe(true);
  423. expect(spyOnChange.calledOnce).toBe(true);
  424. expect(spyOnSelect.calledWithMatch('beijing', true, { key: "beijing" })).toEqual(true);
  425. expect(spyOnChange.calledWithMatch('Beijing')).toEqual(true);
  426. // classname
  427. nodeBeijing = tree.find(`.${BASE_CLASS_PREFIX}-tree-option.${BASE_CLASS_PREFIX}-tree-option-level-3`).at(0);
  428. expect(nodeBeijing.hasClass(`${BASE_CLASS_PREFIX}-tree-option-selected`)).toEqual(true);
  429. // change
  430. let nodeAsia = tree.find(`.${BASE_CLASS_PREFIX}-tree-option.${BASE_CLASS_PREFIX}-tree-option-level-1`).at(0);
  431. // select asia
  432. nodeAsia.simulate('click');
  433. // onSelect & onChange
  434. expect(spyOnSelect.calledWithMatch('yazhou', true, { key: "yazhou" })).toEqual(true);
  435. expect(spyOnChange.calledWithMatch('Yazhou')).toEqual(true);
  436. // classname
  437. nodeAsia = tree.find(`.${BASE_CLASS_PREFIX}-tree-option.${BASE_CLASS_PREFIX}-tree-option-level-1`).at(0);
  438. expect(nodeAsia.hasClass(`${BASE_CLASS_PREFIX}-tree-option-selected`)).toEqual(true);
  439. });
  440. it('onChange + onChangeWithObject', () => {
  441. let spyOnChange = sinon.spy(() => { });
  442. let tree = getTree({
  443. defaultExpandAll: true,
  444. onChangeWithObject: true,
  445. onChange: spyOnChange,
  446. });
  447. let nodeBeijing = tree.find(`.${BASE_CLASS_PREFIX}-tree-option.${BASE_CLASS_PREFIX}-tree-option-level-3`).at(0);
  448. // select beijing
  449. nodeBeijing.simulate('click');
  450. // onSelect & onChange
  451. expect(spyOnChange.calledOnce).toBe(true);
  452. expect(spyOnChange.calledWithMatch({ key: "beijing" })).toEqual(true);
  453. });
  454. it('filterTreeNode = true shows input box', () => {
  455. let tree = getTree({});
  456. let searchWrapper = tree.find(`.${BASE_CLASS_PREFIX}-tree-search-wrapper`)
  457. expect(searchWrapper.exists()).toEqual(false);
  458. tree.setProps({ filterTreeNode: true });
  459. tree.update();
  460. searchWrapper = tree.find(`.${BASE_CLASS_PREFIX}-tree-search-wrapper`)
  461. expect(searchWrapper.exists()).toEqual(true);
  462. });
  463. it('onSearch', () => {
  464. let onSearch = value => { };
  465. let spyOnSearch = sinon.spy(onSearch);
  466. let tree = getTree({
  467. filterTreeNode: true,
  468. onSearch: spyOnSearch,
  469. });
  470. const searchWrapper = tree.find(`.${BASE_CLASS_PREFIX}-tree-search-wrapper`);
  471. let searchValue = 'semi';
  472. let event = { target: { value: searchValue } };
  473. searchWrapper.find('input').simulate('change', event);
  474. expect(spyOnSearch.calledOnce).toBe(true);
  475. expect(spyOnSearch.calledWithMatch(searchValue)).toBe(true);
  476. });
  477. it('emptyContent', () => {
  478. let onSearch = value => { };
  479. let spyOnSearch = sinon.spy(onSearch);
  480. let tree = getTree({
  481. filterTreeNode: true,
  482. onSearch: spyOnSearch,
  483. emptyContent: 'diy empty',
  484. showFilteredOnly: true,
  485. });
  486. const searchWrapper = tree.find(`.${BASE_CLASS_PREFIX}-tree-search-wrapper`);
  487. let searchValue = 'asd';
  488. let event = { target: { value: searchValue } };
  489. searchWrapper.find('input').simulate('change', event);
  490. expect(tree.find(`.${BASE_CLASS_PREFIX}-tree-option-label-empty`).getDOMNode().textContent).toEqual('diy empty');
  491. });
  492. it('searchClassName / searchPlaceholder / searchStyle', () => {
  493. let tree = getTree({
  494. filterTreeNode: true,
  495. searchClassName: 'search',
  496. searchPlaceholder: 'placeholder',
  497. searchStyle: { padding: 16 },
  498. showClear: true,
  499. });
  500. const searchWrapper = tree.find(`.${BASE_CLASS_PREFIX}-tree-search-wrapper`)
  501. expect(searchWrapper.hasClass('search')).toEqual(true);
  502. expect(searchWrapper).toHaveStyle('padding', 16);
  503. expect(searchWrapper.find('input').instance().getAttribute('placeholder')).toEqual('placeholder');
  504. });
  505. it('filterTreeNode shows correct result', () => {
  506. let tree1 = getTree({
  507. filterTreeNode: true,
  508. });
  509. const searchWrapper = tree1.find(`.${BASE_CLASS_PREFIX}-tree-search-wrapper`);
  510. let searchValue = '北';
  511. let event = { target: { value: searchValue } };
  512. searchWrapper.find('input').simulate('change', event);
  513. expect(tree1.find(`.${BASE_CLASS_PREFIX}-tree-option`).length).toEqual(6);
  514. expect(tree1.find(`.${BASE_CLASS_PREFIX}-tree-option-highlight`).length).toEqual(2);
  515. expect(tree1.find(`.${BASE_CLASS_PREFIX}-tree-option-highlight`).at(0).instance().textContent).toEqual('北');
  516. expect(tree1.find(`.${BASE_CLASS_PREFIX}-tree-option-highlight`).at(1).instance().textContent).toEqual('北');
  517. tree1.unmount();
  518. let tree2 = getTree({
  519. filterTreeNode: true,
  520. treeNodeFilterProp: 'value',
  521. });
  522. const searchWrapper2 = tree2.find(`.${BASE_CLASS_PREFIX}-tree-search-wrapper`);
  523. let searchValue2 = 'an';
  524. let event2 = { target: { value: searchValue2 } };
  525. searchWrapper2.find('input').simulate('change', event2);
  526. expect(tree2.find(`.${BASE_CLASS_PREFIX}-tree-option`).length).toEqual(10);
  527. });
  528. it('filterTreeNode + no result', () => {
  529. let tree1 = getTree({
  530. filterTreeNode: true,
  531. });
  532. const searchWrapper = tree1.find(`.${BASE_CLASS_PREFIX}-tree-search-wrapper`);
  533. let searchValue = 'Bei';
  534. let event = { target: { value: searchValue } };
  535. searchWrapper.find('input').simulate('change', event);
  536. expect(tree1.find(`.${BASE_CLASS_PREFIX}-tree-option`).length).toEqual(2);
  537. expect(tree1.find(`.${BASE_CLASS_PREFIX}-tree-option-highlight`).length).toEqual(0);
  538. tree1.unmount();
  539. let tree2 = getTree({
  540. filterTreeNode: true,
  541. treeNodeFilterProp: 'value',
  542. });
  543. const searchWrapper2 = tree2.find(`.${BASE_CLASS_PREFIX}-tree-search-wrapper`);
  544. let searchValue2 = '北京';
  545. let event2 = { target: { value: searchValue2 } };
  546. searchWrapper2.find('input').simulate('change', event2);
  547. expect(tree2.find(`.${BASE_CLASS_PREFIX}-tree-option`).length).toEqual(2);
  548. });
  549. if('filterTreeNode + treeNodeFilterProp + hightLight right') {
  550. let tree = getTree({
  551. treeData: treeData3,
  552. filterTreeNode: true,
  553. treeNodeFilterProp: 'newLabel'
  554. });
  555. const searchWrapper = tree.find(`.${BASE_CLASS_PREFIX}-tree-search-wrapper`);
  556. let searchValue = '中';
  557. let event = { target: { value: searchValue } };
  558. searchWrapper.find('input').simulate('change', event);
  559. expect(tree.find(`.${BASE_CLASS_PREFIX}-tree-option`).length).toEqual(3);
  560. expect(tree.find(`.${BASE_CLASS_PREFIX}-tree-option-highlight`).length).toEqual(1);
  561. expect(tree.find(`.${BASE_CLASS_PREFIX}-tree-option-highlight`).instance().textContent).toEqual("中");
  562. tree.unmount();
  563. }
  564. it('filterTreeNode + showFilteredOnly + no result', () => {
  565. let tree = getTree({
  566. filterTreeNode: true,
  567. showFilteredOnly: true,
  568. });
  569. const searchWrapper = tree.find(`.${BASE_CLASS_PREFIX}-tree-search-wrapper`);
  570. let searchValue = 'Bei';
  571. let event = { target: { value: searchValue } };
  572. searchWrapper.find('input').simulate('change', event);
  573. let node = tree.find(`.${BASE_CLASS_PREFIX}-tree-option`);
  574. expect(node.length).toEqual(1);
  575. expect(node.hasClass(`${BASE_CLASS_PREFIX}-tree-option-empty`)).toEqual(true);
  576. });
  577. it('filterTreeNode + showFilteredOnly shows correct result', () => {
  578. let tree = getTree({
  579. filterTreeNode: true,
  580. showFilteredOnly: true,
  581. });
  582. const searchWrapper = tree.find(`.${BASE_CLASS_PREFIX}-tree-search-wrapper`);
  583. let searchValue = '北';
  584. let event = { target: { value: searchValue } };
  585. searchWrapper.find('input').simulate('change', event);
  586. expect(tree.find(`.${BASE_CLASS_PREFIX}-tree-option`).length).toEqual(4);
  587. expect(tree.find(`.${BASE_CLASS_PREFIX}-tree-option-highlight`).length).toEqual(2);
  588. });
  589. it('filterTreeNode as a func', () => {
  590. let tree = getTree({
  591. filterTreeNode: (inputValue, treeNode) => treeNode === inputValue,
  592. });
  593. const searchWrapper = tree.find(`.${BASE_CLASS_PREFIX}-tree-search-wrapper`);
  594. searchWrapper.find('input').simulate('change', { target: { value: '北' } });
  595. expect(tree.find(`.${BASE_CLASS_PREFIX}-tree-option-highlight`).length).toEqual(0);
  596. // update
  597. searchWrapper.find('input').simulate('change', { target: { value: '北京' } });
  598. expect(tree.find(`.${BASE_CLASS_PREFIX}-tree-option-highlight`).length).toEqual(1);
  599. expect(tree.find(`.${BASE_CLASS_PREFIX}-tree-option-highlight`).instance().textContent).toEqual('北京');
  600. });
  601. it('controlled: value shows correct', () => {
  602. let tree = getTree({
  603. value: 'Beijing'
  604. });
  605. let selectedNode = tree.find(`.${BASE_CLASS_PREFIX}-tree-option-selected`);
  606. expect(selectedNode.instance().textContent).toEqual('北京');
  607. tree.unmount();
  608. // array case only select first item
  609. let tree2 = getTree({
  610. value: ['Riben', 'Beijing']
  611. });
  612. let selectedNode2 = tree2.find(`.${BASE_CLASS_PREFIX}-tree-option-selected`);
  613. expect(selectedNode2.instance().textContent).toEqual('日本');
  614. expect(tree2.find(`.${BASE_CLASS_PREFIX}-tree-option.${BASE_CLASS_PREFIX}-tree-option-level-2`).at(0).hasClass(`${BASE_CLASS_PREFIX}-tree-option-collapsed`)).toEqual(true);
  615. });
  616. it('controlled: fire onChange and ui not update', () => {
  617. let spyOnChange = sinon.spy(() => { });
  618. let tree = getTree({
  619. value: '',
  620. defaultExpandAll: true,
  621. onChange: spyOnChange,
  622. });
  623. expect(tree.find(`.${BASE_CLASS_PREFIX}-tree-option-selected`).exists()).toEqual(false);
  624. let nodeBeijing = tree.find(`.${BASE_CLASS_PREFIX}-tree-option.${BASE_CLASS_PREFIX}-tree-option-level-3`).at(0);
  625. // select beijing
  626. nodeBeijing.simulate('click');
  627. // onSelect & onChange
  628. expect(spyOnChange.calledOnce).toBe(true);
  629. expect(spyOnChange.calledWithMatch("Beijing")).toEqual(true);
  630. expect(tree.find(`.${BASE_CLASS_PREFIX}-tree-option-selected`).exists()).toEqual(false);
  631. });
  632. it('controlled: value + onChangeWithObject', () => {
  633. let spyOnChange = sinon.spy(() => { });
  634. let tree = getTree({
  635. value: {
  636. label: '北京',
  637. value: 'Beijing',
  638. key: 'beijing',
  639. },
  640. defaultExpandAll: true,
  641. onChange: spyOnChange,
  642. onChangeWithObject: true,
  643. });
  644. expect(tree.find(`.${BASE_CLASS_PREFIX}-tree-option-selected`).exists()).toEqual(true);
  645. });
  646. it('virtualized: fixed height', () => {
  647. let tree = getTree({
  648. defaultExpandAll: true,
  649. virtualize: {
  650. itemSize: 28,
  651. height: 84,
  652. },
  653. });
  654. // virtual list
  655. debugger
  656. expect(tree.find(`.${BASE_CLASS_PREFIX}-tree-virtual-list`).exists()).toEqual(true);
  657. // fewer nodes
  658. let nodes = tree.find(`.${BASE_CLASS_PREFIX}-tree-option`);
  659. expect(nodes.length).toBeLessThan(10);
  660. });
  661. it('disabled', () => {
  662. let spyOnChange = sinon.spy(() => { });
  663. let tree = getTree({
  664. defaultExpandAll: true,
  665. disabled: true,
  666. onChange: spyOnChange,
  667. });
  668. let nodes = tree.find(`.${BASE_CLASS_PREFIX}-tree-option-disabled`);
  669. expect(nodes.length).toEqual(10);
  670. // cannot select
  671. let nodeBeijing = tree.find(`.${BASE_CLASS_PREFIX}-tree-option.${BASE_CLASS_PREFIX}-tree-option-level-3`).at(0);
  672. nodeBeijing.simulate('click');
  673. expect(spyOnChange.notCalled).toBe(true);
  674. expect(tree.find(`.${BASE_CLASS_PREFIX}-tree-option-selected`).exists()).toEqual(false);
  675. });
  676. it('treedata in json format', () => {
  677. let spyOnChange = sinon.spy(() => { });
  678. let tree = mount(
  679. <Tree
  680. defaultExpandAll={true}
  681. onChange={spyOnChange}
  682. treeDataSimpleJson={{
  683. "Node1": {
  684. "Child1": '0-0-1',
  685. "Child2": '0-0-2',
  686. },
  687. "Node2": "0-1"
  688. }}
  689. />,
  690. {
  691. attachTo: document.getElementById('container')
  692. }
  693. );
  694. let nodes = tree.find(`.${BASE_CLASS_PREFIX}-tree-option`);
  695. expect(nodes.length).toEqual(4);
  696. // select
  697. let child1 = tree.find(`.${BASE_CLASS_PREFIX}-tree-option.${BASE_CLASS_PREFIX}-tree-option-level-2`).at(0);
  698. child1.simulate('click');
  699. expect(spyOnChange.calledOnce).toBe(true);
  700. expect(spyOnChange.calledWithMatch({
  701. "Node1": {
  702. "Child1": '0-0-1',
  703. },
  704. })).toEqual(true);
  705. expect(tree.find(`.${BASE_CLASS_PREFIX}-tree-option-selected`).exists()).toEqual(true);
  706. });
  707. it('onContextMenu', () => {
  708. const nativeEvent = { nativeEvent: { stopImmediatePropagation: () => { } } }
  709. let spyOnContextMenu = sinon.spy(() => { });
  710. let tree = getTree({
  711. onContextMenu: spyOnContextMenu,
  712. });
  713. let event = {};
  714. // yazhou
  715. let topNodeAsia = tree.find(`.${BASE_CLASS_PREFIX}-tree-option.${BASE_CLASS_PREFIX}-tree-option-level-1`).at(0);
  716. // right click on yazhou
  717. topNodeAsia.simulate('contextmenu', nativeEvent);
  718. expect(spyOnContextMenu.calledWithMatch(event, { key: 'yazhou' })).toEqual(true);
  719. // should not select item
  720. expect(topNodeAsia.hasClass(`${BASE_CLASS_PREFIX}-tree-option-selected`)).toEqual(false);
  721. expect(tree.state().selectedKeys.length).toEqual(0);
  722. });
  723. it('onDoubleClick', () => {
  724. const nativeEvent = { nativeEvent: { stopImmediatePropagation: () => { } } }
  725. let spyOnDoubleClick = sinon.spy(() => { });
  726. let tree = getTree({
  727. onDoubleClick: spyOnDoubleClick,
  728. });
  729. let event = {};
  730. // yazhou
  731. let topNodeAsia = tree.find('.semi-tree-option.semi-tree-option-level-1').at(0);
  732. // double click on yazhou
  733. topNodeAsia.simulate('doubleClick', nativeEvent);
  734. expect(spyOnDoubleClick.calledWithMatch(event, { key: 'yazhou' })).toEqual(true);
  735. // should not select item
  736. expect(topNodeAsia.hasClass(`${BASE_CLASS_PREFIX}-tree-option-selected`)).toEqual(false);
  737. expect(tree.state().selectedKeys.length).toEqual(0);
  738. });
  739. /**
  740. * Detect whether the expanded item will be expanded according to the value when the value
  741. * or treeData is changed, when expandedKeys is not controlled
  742. */
  743. const treeJsonData1 = {
  744. "Node0": {
  745. "Child Node0-0": '0-0',
  746. "Child Node0-1": '0-1',
  747. },
  748. "Node1": {
  749. "Child Node1-0": '1-0',
  750. "Child Node1-1": '1-1',
  751. }
  752. };
  753. const treeJsonData2 = {
  754. "Updated Node0": {
  755. "Updated Child Node0-0": {
  756. 'Updated Child Node0-0-0':'0-0'
  757. },
  758. "Updated Child Node0-1": '0-1',
  759. },
  760. "Updated Node1": {
  761. "Updated Child Node1-0": '1-0',
  762. "Updated Child Node1-1": '1-1',
  763. }
  764. };
  765. it('expandedKeys when treeDataSimpleJson update', () => {
  766. const tree = mount(
  767. <Tree
  768. value='0-0'
  769. multiple
  770. treeDataSimpleJson={treeJsonData1}
  771. />,
  772. {
  773. attachTo: document.getElementById('container')
  774. }
  775. );
  776. const treeDataButton = mount(
  777. <Button
  778. onClick={() => {
  779. if (isEqual(tree.props().treeDataSimpleJson, treeJsonData1)) {
  780. tree.setProps({ treeDataSimpleJson: treeJsonData2 });
  781. tree.update();
  782. } else {
  783. tree.setProps({ treeDataSimpleJson: treeJsonData1 });
  784. tree.update();
  785. }
  786. }}
  787. >
  788. update treeData
  789. </Button>
  790. );
  791. expect(
  792. tree
  793. .find(`.${BASE_CLASS_PREFIX}-tree-option.${BASE_CLASS_PREFIX}-tree-option-level-1`)
  794. .at(0)
  795. .hasClass(`${BASE_CLASS_PREFIX}-tree-option-collapsed`)
  796. ).toEqual(false);
  797. expect(
  798. tree
  799. .find(`.${BASE_CLASS_PREFIX}-tree-option.${BASE_CLASS_PREFIX}-tree-option-level-2`)
  800. .at(0)
  801. .hasClass(`${BASE_CLASS_PREFIX}-tree-option-collapsed`)
  802. ).toEqual(true);
  803. expect(
  804. tree
  805. .find(`.${BASE_CLASS_PREFIX}-tree-option.${BASE_CLASS_PREFIX}-tree-option-level-1`)
  806. .at(1)
  807. .hasClass(`${BASE_CLASS_PREFIX}-tree-option-collapsed`)
  808. ).toEqual(true);
  809. treeDataButton.simulate('click');
  810. expect(
  811. tree
  812. .find(`.${BASE_CLASS_PREFIX}-tree-option.${BASE_CLASS_PREFIX}-tree-option-level-2`)
  813. .at(0)
  814. .hasClass(`${BASE_CLASS_PREFIX}-tree-option-collapsed`)
  815. ).toEqual(false);
  816. treeDataButton.simulate('click');
  817. tree.unmount();
  818. treeDataButton.unmount();
  819. });
  820. it('expandAction = false / default behavior', () => {
  821. const nativeEvent = { nativeEvent: { stopImmediatePropagation: () => { } } }
  822. let spyOnExpand = sinon.spy(() => { });
  823. let tree = getTree({
  824. onExpand: spyOnExpand,
  825. });
  826. // yazhou
  827. let topNodeAsia = tree.find(`.${BASE_CLASS_PREFIX}-tree-option.${BASE_CLASS_PREFIX}-tree-option-level-1`).at(0);
  828. // expand yazhou
  829. topNodeAsia.simulate('click', nativeEvent);
  830. expect(spyOnExpand.notCalled).toBe(true);
  831. expect(tree.state().expandedKeys.size).toEqual(0);
  832. });
  833. it('expandAction = click', () => {
  834. const nativeEvent = { nativeEvent: { stopImmediatePropagation: () => { } } }
  835. let spyOnExpand = sinon.spy(() => { });
  836. let tree = getTree({
  837. onExpand: spyOnExpand,
  838. expandAction: 'click',
  839. });
  840. // yazhou
  841. let topNodeAsia = tree.find(`.${BASE_CLASS_PREFIX}-tree-option.${BASE_CLASS_PREFIX}-tree-option-level-1`).at(0);
  842. // expand yazhou
  843. topNodeAsia.simulate('click', nativeEvent);
  844. expect(spyOnExpand.calledOnce).toBe(true);
  845. expect(spyOnExpand.calledWithMatch(["yazhou"], { expanded: true, node: { key: 'yazhou' } })).toEqual(true);
  846. // yazhou beimeizhou
  847. let topNode = tree.find(`.${BASE_CLASS_PREFIX}-tree-option.${BASE_CLASS_PREFIX}-tree-option-level-1`);
  848. expect(topNode.at(0).hasClass(`${BASE_CLASS_PREFIX}-tree-option-collapsed`)).toEqual(false);
  849. expect(topNode.at(1).hasClass(`${BASE_CLASS_PREFIX}-tree-option-collapsed`)).toEqual(true);
  850. // collapse yazhou
  851. topNodeAsia.simulate('click', nativeEvent);
  852. expect(spyOnExpand.calledWithMatch([], { expanded: false, node: { key: 'yazhou' } })).toEqual(true);
  853. topNode = tree.find(`.${BASE_CLASS_PREFIX}-tree-option.${BASE_CLASS_PREFIX}-tree-option-level-1`);
  854. expect(topNode.at(0).hasClass(`${BASE_CLASS_PREFIX}-tree-option-collapsed`)).toEqual(true);
  855. });
  856. it('expandAction = doubleClick', () => {
  857. const nativeEvent = { nativeEvent: { stopImmediatePropagation: () => { } } }
  858. let spyOnExpand = sinon.spy(() => { });
  859. let tree = getTree({
  860. onExpand: spyOnExpand,
  861. expandAction: 'doubleClick',
  862. });
  863. // yazhou
  864. let topNodeAsia = tree.find(`.${BASE_CLASS_PREFIX}-tree-option.${BASE_CLASS_PREFIX}-tree-option-level-1`).at(0);
  865. // expand yazhou
  866. topNodeAsia.simulate('click', nativeEvent);
  867. expect(spyOnExpand.calledOnce).toBe(false);
  868. topNodeAsia.simulate('doubleClick', nativeEvent);
  869. expect(spyOnExpand.calledOnce).toBe(true);
  870. expect(spyOnExpand.calledWithMatch(["yazhou"], { expanded: true, node: { key: 'yazhou' } })).toEqual(true);
  871. // yazhou beimeizhou
  872. expect(tree.state().expandedKeys).toEqual(new Set(["yazhou"]));
  873. expect(tree.state().selectedKeys).toEqual(["yazhou"]);
  874. });
  875. it('async load data', () => {
  876. const nativeEvent = { nativeEvent: { stopImmediatePropagation: () => { } } }
  877. const then = jest.fn(() => Promise.resolve());
  878. const loadData = jest.fn(() => ({ then }));
  879. const data = {
  880. label: '亚洲',
  881. value: 'Asia',
  882. key: 'asia',
  883. };
  884. let tree = getTree({
  885. loadData,
  886. treeData: [data]
  887. });
  888. let topNodeAsia = tree.find(`.${BASE_CLASS_PREFIX}-tree-option.${BASE_CLASS_PREFIX}-tree-option-level-1`).at(0);
  889. let expandIcon = topNodeAsia.find(`.${BASE_CLASS_PREFIX}-tree-option-expand-icon`).at(0);
  890. // expand yazhou
  891. expandIcon.simulate('click', nativeEvent);
  892. expect(loadData).toHaveBeenCalledWith(data);
  893. expect(then).toHaveBeenCalled();
  894. });
  895. it('DND - dragStart event', () => {
  896. const spyOnDragStart = sinon.spy(() => { });
  897. let tree = getTree({
  898. draggable: true,
  899. onDragStart: spyOnDragStart
  900. });
  901. let dragNode = tree.find('.semi-tree-option.semi-tree-option-level-1').at(0);
  902. dragNode.simulate('dragStart');
  903. expect(spyOnDragStart.calledOnce).toBe(true);
  904. expect(spyOnDragStart.calledWithMatch({node: dragNodeData})).toEqual(true);
  905. });
  906. it('DND - dragEnter event', (done) => {
  907. const spyOnDragEnter = sinon.spy(() => { });
  908. let tree = getTree({
  909. draggable: true,
  910. // autoExpandWhenDragEnter: false,
  911. onDragEnter: spyOnDragEnter
  912. });
  913. let dragNode = tree.find('.semi-tree-option.semi-tree-option-level-1').at(0);
  914. dragNode.simulate('dragStart');
  915. dragNode.simulate('dragEnter');
  916. // not trigger on self
  917. expect(spyOnDragEnter.notCalled).toBe(true);
  918. let dropNode = tree.find('.semi-tree-option.semi-tree-option-level-1').at(1);
  919. dropNode.simulate('dragEnter');
  920. setTimeout(() => {
  921. expect(spyOnDragEnter.calledOnce).toBe(true);
  922. expect(spyOnDragEnter.calledWithMatch({node: dropNodeData, expandedKeys: ['beimeizhou']})).toEqual(true);
  923. done();
  924. }, 500);
  925. });
  926. it('DND - dragOver event', () => {
  927. const spyOnDragOver = sinon.spy(() => { });
  928. let tree = getTree({
  929. draggable: true,
  930. onDragOver: spyOnDragOver
  931. });
  932. let dropNode = tree.find('.semi-tree-option.semi-tree-option-level-1').at(1);
  933. dropNode.simulate('dragOver');
  934. expect(spyOnDragOver.calledOnce).toBe(true);
  935. expect(spyOnDragOver.calledWithMatch({node: dropNodeData})).toEqual(true);
  936. });
  937. it('DND - dragLeave event', () => {
  938. const spyOnDragLeave = sinon.spy(() => { });
  939. let tree = getTree({
  940. draggable: true,
  941. onDragLeave: spyOnDragLeave
  942. });
  943. let dropNode = tree.find('.semi-tree-option.semi-tree-option-level-1').at(1);
  944. dropNode.simulate('dragLeave');
  945. expect(spyOnDragLeave.calledOnce).toBe(true);
  946. expect(spyOnDragLeave.calledWithMatch({node: dropNodeData})).toEqual(true);
  947. });
  948. it('DND - drop event', () => {
  949. const spyOnDrop = sinon.spy(() => { });
  950. let tree = getTree({
  951. draggable: true,
  952. onDrop: spyOnDrop
  953. });
  954. let dragNode = tree.find('.semi-tree-option.semi-tree-option-level-1').at(0);
  955. dragNode.simulate('dragStart');
  956. let dropNode = tree.find('.semi-tree-option.semi-tree-option-level-1').at(1);
  957. dropNode.simulate('drop');
  958. expect(spyOnDrop.calledOnce).toBe(true);
  959. expect(spyOnDrop.calledWithMatch({
  960. dragNode: dragNodeData,
  961. dragNodesKeys: ["yazhou","zhongguo","beijing","shanghai","riben","dongjing","daban"],
  962. dropPosition: 1
  963. })).toEqual(true);
  964. });
  965. it('DND - dragEnd event', () => {
  966. const spyOnDragEnd = sinon.spy(() => { });
  967. let tree = getTree({
  968. draggable: true,
  969. onDragEnd: spyOnDragEnd
  970. });
  971. let dragNode = tree.find('.semi-tree-option.semi-tree-option-level-1').at(0);
  972. dragNode.simulate('dragEnd');
  973. expect(spyOnDragEnd.calledOnce).toBe(true);
  974. expect(spyOnDragEnd.calledWithMatch({node: dragNodeData})).toEqual(true);
  975. });
  976. it('keyMaps', () => {
  977. let spyOnChange = sinon.spy(() => { });
  978. let tree = getTree({
  979. treeData: specialTreeData,
  980. defaultValue: 'Beijing',
  981. onChange: spyOnChange,
  982. defaultExpandAll: true,
  983. motion: false,
  984. keyMaps: defaultKeyMaps
  985. });
  986. let disabledNode = tree.find(`.${BASE_CLASS_PREFIX}-tree-option-disabled`).at(0);
  987. expect(disabledNode.find(`.${BASE_CLASS_PREFIX}-tree-option-label-text`).instance().textContent).toEqual('中国');
  988. let selectedNode = tree.find(`.${BASE_CLASS_PREFIX}-tree-option-selected`);
  989. expect(selectedNode.instance().textContent).toEqual('北京');
  990. let nodeShanghai = tree.find(`.${BASE_CLASS_PREFIX}-tree-option.${BASE_CLASS_PREFIX}-tree-option-level-3`).at(1);
  991. // select beijing
  992. nodeShanghai.simulate('click');
  993. // onSelect & onChange
  994. expect(spyOnChange.calledOnce).toBe(true);
  995. expect(spyOnChange.calledWithMatch("Shanghai")).toEqual(true);
  996. });
  997. it('keyMaps + onChangeWithObject', () => {
  998. let spyOnChange = sinon.spy(() => { });
  999. let tree = getTree({
  1000. treeData: specialTreeData,
  1001. defaultValue: {
  1002. label1: '北京',
  1003. value1: 'Beijing',
  1004. key1: 'beijing',
  1005. },
  1006. onChangeWithObject: true,
  1007. onChange: spyOnChange,
  1008. defaultExpandAll: true,
  1009. keyMaps: defaultKeyMaps
  1010. });
  1011. let disabledNode = tree.find(`.${BASE_CLASS_PREFIX}-tree-option-disabled`).at(0);
  1012. expect(disabledNode.find(`.${BASE_CLASS_PREFIX}-tree-option-label-text`).instance().textContent).toEqual('中国');
  1013. let selectedNode = tree.find(`.${BASE_CLASS_PREFIX}-tree-option-selected`);
  1014. expect(selectedNode.instance().textContent).toEqual('北京');
  1015. let nodeShanghai = tree.find(`.${BASE_CLASS_PREFIX}-tree-option.${BASE_CLASS_PREFIX}-tree-option-level-3`).at(1);
  1016. // select beijing
  1017. nodeShanghai.simulate('click');
  1018. // onSelect & onChange
  1019. expect(spyOnChange.calledOnce).toBe(true);
  1020. expect(spyOnChange.calledWithMatch({
  1021. label1: '上海',
  1022. value1: 'Shanghai',
  1023. key1: 'shanghai',
  1024. })).toEqual(true);
  1025. });
  1026. })