treeNodeProps.test.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. import { Tree, Button } from '../../index';
  2. import { BASE_CLASS_PREFIX } from '../../../semi-foundation/base/constants';
  3. import { IconMapPin } from '@douyinfe/semi-icons';
  4. const treeChildren = [
  5. {
  6. label: '北京',
  7. value: 'beijing',
  8. key: 'beijing',
  9. },
  10. {
  11. label: '上海',
  12. value: 'shanghai',
  13. key: 'shanghai',
  14. },
  15. ];
  16. const treeDataDisabled = [
  17. {
  18. label: '亚洲',
  19. value: 'Yazhou',
  20. key: 'yazhou',
  21. children: [
  22. {
  23. label: '中国',
  24. value: 'Zhongguo',
  25. key: 'zhongguo',
  26. disabled: true,
  27. children: treeChildren,
  28. },
  29. {
  30. label: '日本',
  31. value: 'Riben',
  32. key: 'riben',
  33. },
  34. ],
  35. }
  36. ];
  37. const treeDataIcon = [
  38. {
  39. label: '亚洲',
  40. value: 'Yazhou',
  41. key: 'yazhou',
  42. children: [
  43. {
  44. label: '中国',
  45. value: 'Zhongguo',
  46. key: 'zhongguo',
  47. icon: (<IconMapPin />),
  48. children: treeChildren,
  49. },
  50. {
  51. label: '日本',
  52. value: 'Riben',
  53. key: 'riben',
  54. },
  55. ],
  56. }
  57. ];
  58. const treeDataExtra = [
  59. {
  60. label: '亚洲',
  61. value: 'Yazhou',
  62. key: 'yazhou',
  63. children: [
  64. {
  65. label: '中国',
  66. value: 'Zhongguo',
  67. key: 'zhongguo',
  68. info: 'extra',
  69. children: treeChildren,
  70. },
  71. {
  72. label: '日本',
  73. value: 'Riben',
  74. key: 'riben',
  75. },
  76. ],
  77. }
  78. ];
  79. function getTree(props) {
  80. return mount(
  81. <Tree
  82. defaultExpandAll
  83. // multiple
  84. {...props}
  85. />,
  86. {
  87. attachTo: document.getElementById('container')
  88. }
  89. );
  90. }
  91. describe('Tree', () => {
  92. beforeEach(() => {
  93. // Avoid `attachTo: document.body` Warning
  94. const div = document.createElement('div');
  95. div.setAttribute('id', 'container');
  96. document.body.appendChild(div);
  97. });
  98. afterEach(() => {
  99. const div = document.getElementById('container');
  100. if (div) {
  101. document.body.removeChild(div);
  102. }
  103. document.body.innerHTML = '';
  104. });
  105. it('disable', () => {
  106. let spyOnChange = sinon.spy(() => { });
  107. let tree = getTree({
  108. treeData: treeDataDisabled,
  109. onChange: spyOnChange,
  110. });
  111. let node = tree.find(`.${BASE_CLASS_PREFIX}-tree-option-disabled`);
  112. expect(node.instance().textContent).toEqual('中国');
  113. node.simulate('click');
  114. expect(spyOnChange.notCalled).toBe(true);
  115. expect(tree.find(`.${BASE_CLASS_PREFIX}-tree-option-selected`).exists()).toEqual(false);
  116. });
  117. it('disable + multiple', () => {
  118. let spyOnChange = sinon.spy(() => { });
  119. let tree = getTree({
  120. treeData: treeDataDisabled,
  121. onChange: spyOnChange,
  122. multiple: true
  123. });
  124. let node = tree.find(`.${BASE_CLASS_PREFIX}-tree-option-disabled`);
  125. expect(node.instance().textContent).toEqual('中国');
  126. node.simulate('click');
  127. expect(spyOnChange.notCalled).toBe(true);
  128. expect(node.find(`.${BASE_CLASS_PREFIX}-checkbox-disabled`).exists()).toEqual(true);
  129. });
  130. it('custom icon', () => {
  131. let props = {
  132. treeData: treeDataIcon,
  133. };
  134. let tree = getTree(props);
  135. expect(tree.find(`.${BASE_CLASS_PREFIX}-icon-map_pin`).length).toEqual(1);
  136. });
  137. it('custom props + onChangeWithObject', () => {
  138. let spyOnChange = sinon.spy(() => { });
  139. let tree = getTree({
  140. treeData: treeDataExtra,
  141. onChangeWithObject: true,
  142. onChange: spyOnChange,
  143. });
  144. // zhongguo
  145. let node = tree.find(`.${BASE_CLASS_PREFIX}-tree-option.${BASE_CLASS_PREFIX}-tree-option-level-2`).at(0);
  146. node.simulate('click');
  147. // onSelect & onChange
  148. expect(spyOnChange.calledOnce).toBe(true);
  149. expect(spyOnChange.calledWithMatch({ info: "extra" })).toEqual(true);
  150. })
  151. it('ReactNode label', () => {
  152. let spyOnClick = sinon.spy(() => { });
  153. const treeData = [
  154. {
  155. label: (
  156. <span>
  157. <span style={{ marginRight: 30 }}>亚洲</span>
  158. <Button
  159. style={{ zIndex: 2 }}
  160. onClick={e => { spyOnClick(); e.stopPropagation() }}
  161. >
  162. Click
  163. </Button>
  164. </span>
  165. ),
  166. value: 'yazhou',
  167. key: 'yazhou',
  168. }
  169. ];
  170. let tree = getTree({ treeData });
  171. let button = tree.find(`.${BASE_CLASS_PREFIX}-button`);
  172. button.simulate('click');
  173. expect(button.exists()).toEqual(true);
  174. expect(spyOnClick.calledOnce).toBe(true);
  175. // event stopPropagation
  176. expect(tree.find(`.${BASE_CLASS_PREFIX}-tree-option-selected`).exists()).toEqual(false);
  177. });
  178. })