table.test.js 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933
  1. import BaseTable from '../Table';
  2. import Table from '../index';
  3. import Column from '../Column';
  4. import {Tag, Tooltip} from '../../index';
  5. import {
  6. getColumns,
  7. getData,
  8. getGroupColumns,
  9. getGroupData,
  10. getJSXColumns,
  11. getNestColumns,
  12. getNestData,
  13. getRandomNumber,
  14. getTreeData,
  15. mount,
  16. sleep,
  17. } from '../../_test_/utils';
  18. import {BASE_CLASS_PREFIX} from '../../../semi-foundation/base/constants';
  19. import {IconStar} from '@douyinfe/semi-icons';
  20. /**
  21. * Table 需要测试的使用场景
  22. * ✅ JSX 写法
  23. * ✅ 行选择
  24. * ✅ 分页
  25. * ✅ 固定列
  26. * ✅ 排序
  27. * ✅ 筛选
  28. * ✅ 展开
  29. * ✅ 树形展示
  30. * ✅ 自定义行或单元格
  31. * ✅ 分组
  32. * ✅ 表头合并
  33. * ✅ 更新数据
  34. * ✅ 完全自定义渲染
  35. * ✅ 行列合并
  36. * ❌ 可伸缩列(不好测试)
  37. * ❌ 拖拽排序(不好测试)
  38. * ❌ 虚拟化
  39. */
  40. const dataTotalSize = 46;
  41. const data = getData(dataTotalSize);
  42. const columns = getColumns();
  43. function testAppearance(demo, params) {
  44. const {onRow, myCls, myClsIndex} = params; // check table wrapper
  45. expect(demo.find(`.${BASE_CLASS_PREFIX}-table-wrapper`).length).toBe(1); // check if has header
  46. expect(demo.find(`.${BASE_CLASS_PREFIX}-table-title`).length).toBe(1); // check if has footer
  47. expect(demo.find(`.${BASE_CLASS_PREFIX}-table-footer`).length).toBe(1); // check onRow
  48. expect(onRow.called).toBe(true);
  49. expect(
  50. demo
  51. .find(`.${BASE_CLASS_PREFIX}-table-tbody .${BASE_CLASS_PREFIX}-table-row`)
  52. .at(0)
  53. .hasClass(myCls)
  54. ).toBe(false);
  55. expect(
  56. demo.find(`.${BASE_CLASS_PREFIX}-table-tbody .${BASE_CLASS_PREFIX}-table-row`).reduce((prev, item, index) => {
  57. if (item.hasClass(myCls)) {
  58. return index;
  59. } else {
  60. return prev;
  61. }
  62. }, -1)
  63. ).toBe(myClsIndex); // check pagination
  64. expect(demo.find(`.${BASE_CLASS_PREFIX}-table-pagination-outer`).length).toBe(1); // click page 2
  65. const curPage = 2;
  66. demo.find(`.${BASE_CLASS_PREFIX}-page li`)
  67. .at(curPage)
  68. .simulate('click');
  69. expect(demo.find(BaseTable).state('pagination')).toHaveProperty('currentPage', curPage);
  70. }
  71. describe(`Table`, () => {
  72. it(`test jsx columns appearance`, async () => {
  73. const myCls = `my-tr-class`;
  74. const myClsIndex = 2;
  75. const onRow = sinon.spy((record, index) => ({
  76. className: index === myClsIndex ? myCls : '',
  77. }));
  78. const columns = getJSXColumns();
  79. const demo = mount(
  80. <Table
  81. dataSource={data}
  82. onRow={onRow}
  83. title={() => 'Table Title'}
  84. footer={() => 'Table Footer'}
  85. children={columns}
  86. />
  87. );
  88. testAppearance(demo, {
  89. onRow,
  90. myCls,
  91. myClsIndex,
  92. });
  93. const newColumns = getJSXColumns();
  94. demo.setProps({
  95. children: newColumns,
  96. });
  97. testAppearance(demo, {
  98. onRow,
  99. myCls,
  100. myClsIndex,
  101. });
  102. });
  103. it(`test object columns appearance`, async () => {
  104. const myCls = `my-tr-class`;
  105. const myClsIndex = 2;
  106. const onRow = sinon.spy((record, index) => ({
  107. className: index === myClsIndex ? myCls : '',
  108. }));
  109. const columns = getColumns();
  110. const demo = mount(
  111. <Table
  112. dataSource={data}
  113. onRow={onRow}
  114. title={() => 'Table Title'}
  115. footer={() => 'Table Footer'}
  116. columns={columns}
  117. />
  118. );
  119. testAppearance(demo, {
  120. onRow,
  121. myCls,
  122. myClsIndex,
  123. });
  124. const newColumns = getColumns();
  125. demo.setProps({
  126. columns: newColumns,
  127. });
  128. testAppearance(demo, {
  129. onRow,
  130. myCls,
  131. myClsIndex,
  132. });
  133. });
  134. it(`test dataSource change appearance`, async () => {
  135. const myCls = `my-tr-class`;
  136. const myClsIndex = 2;
  137. const onRow = sinon.spy((record, index) => ({
  138. className: index === myClsIndex ? myCls : '',
  139. }));
  140. const columns = getColumns();
  141. const data = getData(25);
  142. const demo = mount(
  143. <Table
  144. dataSource={data}
  145. onRow={onRow}
  146. title={() => 'Table Title'}
  147. footer={() => 'Table Footer'}
  148. columns={columns}
  149. />
  150. );
  151. testAppearance(demo, {
  152. onRow,
  153. myCls,
  154. myClsIndex,
  155. });
  156. const dataNum = getRandomNumber(100, 40);
  157. const newData = getData(dataNum);
  158. demo.setProps({
  159. dataSource: newData,
  160. });
  161. testAppearance(demo, {
  162. onRow,
  163. myCls,
  164. myClsIndex,
  165. });
  166. });
  167. it(`test selection`, async () => {
  168. const columns = getColumns();
  169. const selectedRowKeys = ['0'];
  170. const rowKey = 'key';
  171. const disabledKeys = ['4'];
  172. let rowSelection = {
  173. onChange: sinon.spy((selectedRowKeys, selectedRows) => {
  174. rowSelection = {...rowSelection, selectedRowKeys: [...selectedRowKeys]};
  175. demo.setProps({
  176. rowSelection,
  177. });
  178. }),
  179. onSelectAll: sinon.spy((selected, selectedRows, changedRows) => {
  180. rowSelection = {...rowSelection, selectedRowKeys: selected ? [...selectedRowKeys] : []};
  181. demo.setProps({
  182. rowSelection,
  183. });
  184. }),
  185. getCheckboxProps: sinon.spy(record => ({
  186. disabled: disabledKeys.includes(record[rowKey]),
  187. // Column configuration not to be checked
  188. name: record.name,
  189. })),
  190. onSelect: sinon.spy((record, selected) => {
  191. }),
  192. selectedRowKeys: ['3'],
  193. defaultSelectedRowKeys: ['0', '1'],
  194. };
  195. const demo = mount(<Table rowKey={rowKey} dataSource={data} columns={columns} rowSelection={rowSelection}/>);
  196. expect(demo.find(`.${BASE_CLASS_PREFIX}-checkbox`).length).toBeGreaterThan(0);
  197. expect(demo.find(`.${BASE_CLASS_PREFIX}-table-row .${BASE_CLASS_PREFIX}-checkbox-inner-checked`).length).toBe(
  198. rowSelection.selectedRowKeys.length
  199. );
  200. expect(demo.find(`.${BASE_CLASS_PREFIX}-table-thead .${BASE_CLASS_PREFIX}-checkbox-indeterminate`).length).toBe(
  201. 1
  202. );
  203. expect(
  204. demo
  205. .find(`.${BASE_CLASS_PREFIX}-table-tbody .${BASE_CLASS_PREFIX}-checkbox`)
  206. .map(el => el)
  207. .every((el, index) =>
  208. disabledKeys.includes(index.toString())
  209. ? el.hasClass(`${BASE_CLASS_PREFIX}-checkbox-disabled`)
  210. : true
  211. )
  212. ).toBeTruthy(); // click disabled checkbox
  213. demo.find(`.${BASE_CLASS_PREFIX}-table-row .${BASE_CLASS_PREFIX}-checkbox-disabled`).simulate('click');
  214. expect(rowSelection.onSelect.notCalled).toBeTruthy(); // click first row checkbox
  215. demo.find(`.${BASE_CLASS_PREFIX}-table-tbody .${BASE_CLASS_PREFIX}-table-row .${BASE_CLASS_PREFIX}-checkbox`)
  216. .at(0)
  217. .simulate('click');
  218. expect(rowSelection.onSelect.calledOnce).toBeTruthy(); // click table header checkbox and select all
  219. demo.find(
  220. `.${BASE_CLASS_PREFIX}-table-thead .${BASE_CLASS_PREFIX}-table-column-selection .${BASE_CLASS_PREFIX}-checkbox`
  221. ).simulate('click');
  222. expect(rowSelection.onSelectAll.calledOnce).toBeTruthy();
  223. expect(demo.find(BaseTable).state('rowSelection')).toHaveProperty(
  224. 'selectedRowKeys',
  225. rowSelection.selectedRowKeys
  226. );
  227. expect(demo.find(`.${BASE_CLASS_PREFIX}-table-thead .${BASE_CLASS_PREFIX}-checkbox-inner-checked`).length).toBe(
  228. 1
  229. ); // click table header checkbox and deselect all
  230. demo.find(
  231. `.${BASE_CLASS_PREFIX}-table-thead .${BASE_CLASS_PREFIX}-table-column-selection .${BASE_CLASS_PREFIX}-checkbox`
  232. ).simulate('click');
  233. expect(rowSelection.onSelectAll.calledTwice).toBeTruthy();
  234. expect(demo.find(BaseTable).state('rowSelection')).toHaveProperty('selectedRowKeys', []);
  235. expect(demo.find(`.${BASE_CLASS_PREFIX}-table-thead .${BASE_CLASS_PREFIX}-checkbox-inner-checked`).length).toBe(
  236. 0
  237. );
  238. expect(demo.find(`.${BASE_CLASS_PREFIX}-table-thead .${BASE_CLASS_PREFIX}-checkbox-indeterminate`).length).toBe(
  239. 0
  240. );
  241. });
  242. it(`test selection after data change`, async () => {
  243. const columns = getColumns();
  244. const selectedRowKeys = ['0'];
  245. const rowKey = 'key';
  246. const disabledKeys = ['4'];
  247. let rowSelection = {
  248. onSelectAll: sinon.spy((selected, selectedRows, changedRows) => {
  249. rowSelection = {...rowSelection, selectedRowKeys: selected ? [...selectedRowKeys] : []};
  250. demo.setProps({
  251. rowSelection,
  252. });
  253. }),
  254. getCheckboxProps: sinon.spy(record => ({
  255. disabled: disabledKeys.includes(record[rowKey]),
  256. // Column configuration not to be checked
  257. name: record.name,
  258. })),
  259. onSelect: sinon.spy((record, selected) => {
  260. }),
  261. };
  262. const demo = mount(<Table rowKey={rowKey} dataSource={data} columns={columns} rowSelection={rowSelection}/>);
  263. const newData = getData(50);
  264. demo.setProps({
  265. dataSource: newData,
  266. }); // click disabled checkbox
  267. demo.find(`.${BASE_CLASS_PREFIX}-table-row .${BASE_CLASS_PREFIX}-checkbox-disabled`).simulate('click');
  268. expect(rowSelection.onSelect.notCalled).toBeTruthy(); // click first row checkbox
  269. demo.find(`.${BASE_CLASS_PREFIX}-table-tbody .${BASE_CLASS_PREFIX}-table-row .${BASE_CLASS_PREFIX}-checkbox`)
  270. .at(0)
  271. .simulate('click');
  272. expect(rowSelection.onSelect.calledOnce).toBeTruthy(); // click table header checkbox and select all
  273. demo.find(
  274. `.${BASE_CLASS_PREFIX}-table-thead .${BASE_CLASS_PREFIX}-table-column-selection .${BASE_CLASS_PREFIX}-checkbox`
  275. ).simulate('click');
  276. expect(rowSelection.onSelectAll.calledOnce).toBeTruthy(); // click table header checkbox and deselect all
  277. demo.find(
  278. `.${BASE_CLASS_PREFIX}-table-thead .${BASE_CLASS_PREFIX}-table-column-selection .${BASE_CLASS_PREFIX}-checkbox`
  279. ).simulate('click');
  280. expect(rowSelection.onSelectAll.calledTwice).toBeTruthy();
  281. });
  282. it(`test expandedRowRender and fixed`, async () => {
  283. const columns = getColumns();
  284. const rowKey = 'key';
  285. const expandedRowRender = (record, index, expanded) => (
  286. <article
  287. style={{
  288. margin: 0,
  289. }}
  290. >
  291. <p>{record.description}</p>
  292. </article>
  293. );
  294. const unexpandableKey = '2';
  295. const demo = mount(
  296. <Table
  297. scroll={{
  298. x: '160%',
  299. y: 320,
  300. }}
  301. defaultExpandAllRows
  302. rowKey={rowKey}
  303. expandedRowRender={expandedRowRender}
  304. hideExpandedColumn={false}
  305. expandCellFixed={true}
  306. dataSource={data}
  307. columns={columns}
  308. rowExpandable={record => record[rowKey] !== unexpandableKey}
  309. />
  310. );
  311. const leftFixedCells = demo.find(`.${BASE_CLASS_PREFIX}-table-cell-fixed-left`);
  312. const rightFixedCells = demo.find(`.${BASE_CLASS_PREFIX}-table-cell-fixed-right`);
  313. expect(leftFixedCells.length).toBeGreaterThan(0);
  314. expect(rightFixedCells.length).toBeGreaterThan(0);
  315. expect(
  316. demo.find(`.${BASE_CLASS_PREFIX}-table-cell-fixed-left.${BASE_CLASS_PREFIX}-table-column-expand`).length
  317. ).toBeGreaterThan(0);
  318. const newData = getData(50);
  319. demo.setProps({
  320. dataSource: newData,
  321. });
  322. expect(
  323. demo.find(`.${BASE_CLASS_PREFIX}-table-cell-fixed-left.${BASE_CLASS_PREFIX}-table-column-expand`).length
  324. ).toBeGreaterThan(0);
  325. });
  326. it(`test tree data`, async () => {
  327. const rowKey = 'key';
  328. const childrenRecordName = 'children';
  329. const childrenData = getTreeData();
  330. let expandedRowKeys = [1];
  331. const onExpand = sinon.spy((expanded, record) => {
  332. const keySet = new Set([...expandedRowKeys]);
  333. if (expanded) {
  334. keySet.add(record[rowKey]);
  335. } else {
  336. keySet.delete(record[rowKey]);
  337. }
  338. expandedRowKeys = Array.from(keySet);
  339. demo.setProps({
  340. expandedRowKeys,
  341. });
  342. });
  343. const demo = mount(
  344. <Table
  345. rowKey={rowKey}
  346. childrenRecordName={childrenRecordName}
  347. expandedRowKeys={expandedRowKeys}
  348. columns={columns}
  349. dataSource={childrenData}
  350. onExpand={onExpand}
  351. />
  352. );
  353. const table = demo.find(BaseTable);
  354. expect(demo.find(`.${BASE_CLASS_PREFIX}-table-tbody .${BASE_CLASS_PREFIX}-table-row`).length).toBe(
  355. childrenData[0].children.length + childrenData.length
  356. ); // click 3rd row to expand row
  357. table
  358. .find(
  359. `.${BASE_CLASS_PREFIX}-table-body .${BASE_CLASS_PREFIX}-table-row .${BASE_CLASS_PREFIX}-table-expand-icon`
  360. )
  361. .at(1)
  362. .simulate('click');
  363. expect(onExpand.calledOnce).toBeTruthy();
  364. expect(table.state(`expandedRowKeys`)).toEqual(expandedRowKeys); // click first expand button to collapse row
  365. table
  366. .find(
  367. `.${BASE_CLASS_PREFIX}-table-body .${BASE_CLASS_PREFIX}-table-row .${BASE_CLASS_PREFIX}-table-expand-icon`
  368. )
  369. .at(0)
  370. .simulate('click');
  371. expect(table.state('expandedRowKeys')).toEqual(expandedRowKeys);
  372. });
  373. it(`test grouped data`, async () => {
  374. const rowKey = record =>
  375. `${record.city && record.city.toLowerCase()}-${record.job && record.job.toLowerCase()}`;
  376. const data = [
  377. {
  378. city: 'Beijing',
  379. job: 'FE',
  380. department: 'IES',
  381. },
  382. {
  383. city: 'Beijing',
  384. job: 'BE',
  385. department: 'IES',
  386. },
  387. {
  388. city: 'Shanghai',
  389. job: 'Android',
  390. department: 'IES',
  391. },
  392. {
  393. city: 'Tokyo',
  394. job: 'Android',
  395. department: 'IES',
  396. },
  397. {
  398. city: 'Shanghai',
  399. job: 'IOS',
  400. department: 'EE',
  401. },
  402. {
  403. city: 'LA',
  404. job: 'SE',
  405. department: 'EE',
  406. },
  407. {
  408. city: 'Beijing',
  409. job: 'Android',
  410. department: 'EE',
  411. },
  412. {
  413. city: 'Tokyo',
  414. job: 'IOS',
  415. department: 'EE',
  416. },
  417. {
  418. city: 'Tokyo',
  419. job: 'SE',
  420. department: 'DATA',
  421. },
  422. {
  423. city: 'Shanghai',
  424. job: 'BE',
  425. department: 'DATA',
  426. },
  427. {
  428. city: 'LA',
  429. job: 'Android',
  430. department: 'DATA',
  431. },
  432. {
  433. city: 'LA',
  434. job: 'IOS',
  435. department: 'DATA',
  436. },
  437. ];
  438. const columns = [
  439. {
  440. dataIndex: 'city',
  441. title: 'City',
  442. width: 400,
  443. sorter: (a, b) => (a.city > b.city ? 1 : -1),
  444. },
  445. {
  446. dataIndex: 'job',
  447. title: 'Job',
  448. width: 200,
  449. filters: [
  450. {
  451. text: 'IOS',
  452. value: 'IOS',
  453. },
  454. {
  455. text: 'Android',
  456. value: 'Android',
  457. },
  458. ],
  459. onFilter: (value, record) => record.job && record.job.indexOf(value) === 0,
  460. },
  461. {
  462. dataIndex: 'department',
  463. title: 'Department',
  464. },
  465. ];
  466. const groupedRowClick = sinon.spy();
  467. const demo = mount(
  468. <Table
  469. dataSource={data}
  470. rowKey={rowKey}
  471. groupBy={'city'}
  472. columns={columns}
  473. renderGroupSection={groupKey => <strong>Jobs in {groupKey}:</strong>}
  474. onGroupedRow={(group, index) => ({
  475. onClick: groupedRowClick,
  476. })}
  477. clickGroupedRowToExpand
  478. scroll={{
  479. y: 480,
  480. }}
  481. />
  482. );
  483. expect(demo.find(`.${BASE_CLASS_PREFIX}-table-row-section`).length).toBe(4);
  484. expect(demo.find(`.${BASE_CLASS_PREFIX}-table-body .${BASE_CLASS_PREFIX}-table-row`).length).toBe(0);
  485. demo.find(`.${BASE_CLASS_PREFIX}-table-row-section`)
  486. .at(0)
  487. .simulate('click');
  488. expect(demo.find(`.${BASE_CLASS_PREFIX}-table-row`).length).toBeGreaterThan(0);
  489. });
  490. it(`test events: page, filter, sort`, async () => {
  491. const columns = [
  492. {
  493. title: 'Name',
  494. dataIndex: 'name',
  495. width: 150,
  496. filters: [
  497. {
  498. text: '名字包含"1"',
  499. value: '1',
  500. },
  501. {
  502. text: '名字包含"2"',
  503. value: '2',
  504. },
  505. ],
  506. onFilter: (value, record) => record.name.indexOf(value) > -1,
  507. },
  508. {
  509. title: 'Age',
  510. dataIndex: 'age',
  511. width: 150,
  512. sorter: (a, b) => (a.age - b.age > 0 ? 1 : -1),
  513. },
  514. {
  515. title: 'Address',
  516. dataIndex: 'address',
  517. },
  518. {
  519. render: (text, record) => (
  520. <Tooltip content={record.description}>
  521. <Tag color="green">Show Info</Tag>
  522. </Tooltip>
  523. ),
  524. width: 150,
  525. },
  526. ];
  527. let onChangeCalledCount = 0;
  528. const onChange = sinon.spy();
  529. const demo = mount(<Table dataSource={data} columns={columns} onChange={onChange}/>);
  530. demo.find(`.${BASE_CLASS_PREFIX}-table-thead .${BASE_CLASS_PREFIX}-table-column-filter`).simulate('click');
  531. expect(demo.find(`.${BASE_CLASS_PREFIX}-dropdown`).length).toBe(1);
  532. expect(onChange.callCount).toBe(onChangeCalledCount); // click first filter
  533. demo.find(`.${BASE_CLASS_PREFIX}-dropdown .${BASE_CLASS_PREFIX}-dropdown-item`)
  534. .at(0)
  535. .find(`.${BASE_CLASS_PREFIX}-checkbox`)
  536. .simulate('click', {
  537. nativeEvent: null,
  538. });
  539. expect(onChange.callCount).toBe(++onChangeCalledCount); // click first filter again
  540. demo.find(`.${BASE_CLASS_PREFIX}-dropdown .${BASE_CLASS_PREFIX}-dropdown-item`)
  541. .at(0)
  542. .find(`.${BASE_CLASS_PREFIX}-checkbox`)
  543. .simulate('click', {
  544. nativeEvent: null,
  545. });
  546. expect(onChange.callCount).toBe(++onChangeCalledCount); // to page 2
  547. demo.find(`.${BASE_CLASS_PREFIX}-page .${BASE_CLASS_PREFIX}-page-item`)
  548. .at(2)
  549. .simulate('click', {
  550. nativeEvent: null,
  551. });
  552. expect(onChange.callCount).toBe(++onChangeCalledCount); // to page 1
  553. demo.find(`.${BASE_CLASS_PREFIX}-page .${BASE_CLASS_PREFIX}-page-item`)
  554. .at(1)
  555. .simulate('click', {
  556. nativeEvent: null,
  557. });
  558. expect(onChange.callCount).toBe(++onChangeCalledCount); // sort
  559. demo.find(`.${BASE_CLASS_PREFIX}-table-thead .${BASE_CLASS_PREFIX}-table-column-sorter`).simulate('click', {
  560. nativeEvent: null,
  561. });
  562. expect(onChange.callCount).toBe(++onChangeCalledCount);
  563. });
  564. it(`test filter, and update dataSource with exist filter`, () => {
  565. const columns = [
  566. {
  567. className: 'name-col',
  568. title: 'Name',
  569. dataIndex: 'name',
  570. width: 150,
  571. filters: [
  572. {
  573. text: '名字包含"1"',
  574. value: '1',
  575. },
  576. {
  577. text: '名字包含"2"',
  578. value: '2',
  579. },
  580. ],
  581. onFilter: (value, record) => record.name.indexOf(value) > -1,
  582. },
  583. {
  584. title: 'Age',
  585. dataIndex: 'age',
  586. width: 150,
  587. sorter: (a, b) => (a.age - b.age > 0 ? 1 : -1),
  588. },
  589. {
  590. title: 'Address',
  591. dataIndex: 'address',
  592. },
  593. ];
  594. const demo = mount(<Table dataSource={data} columns={columns} pagination={false}/>);
  595. demo.find(`.${BASE_CLASS_PREFIX}-table-thead .${BASE_CLASS_PREFIX}-table-column-filter`).simulate('click'); // click first filter
  596. demo.find(`.${BASE_CLASS_PREFIX}-dropdown .${BASE_CLASS_PREFIX}-dropdown-item`)
  597. .at(0)
  598. .find(`.${BASE_CLASS_PREFIX}-checkbox`)
  599. .simulate('click', {
  600. nativeEvent: null,
  601. });
  602. const nameColList = demo.find('.semi-table-tbody .name-col');
  603. const filteredData = data.filter(item => item.name.indexOf('1') > -1);
  604. expect(nameColList.length).toBe(filteredData.length); // update dataSource with exist filter
  605. const newData = getData(10);
  606. const newFilteredData = newData.filter(item => item.name.indexOf('1') > -1);
  607. demo.setProps({
  608. dataSource: newData,
  609. });
  610. demo.update();
  611. const newNameColList = demo.find('.semi-table-tbody .name-col');
  612. expect(newNameColList.length).toBe(newFilteredData.length);
  613. });
  614. it(`test controlled dataSource and columns`, async () => {
  615. let pagination = {
  616. currentPage: 1,
  617. total: data.length,
  618. pageSize: 10,
  619. };
  620. const getPageData = () =>
  621. data.slice(
  622. (pagination.currentPage - 1) * pagination.pageSize,
  623. pagination.currentPage * pagination.pageSize
  624. );
  625. let currentData = getPageData();
  626. let columns = [
  627. {
  628. title: 'Name',
  629. dataIndex: 'name',
  630. width: 150,
  631. filters: [
  632. {
  633. text: '名字包含"1"',
  634. value: '1',
  635. },
  636. {
  637. text: '名字包含"2"',
  638. value: '2',
  639. },
  640. ],
  641. onFilter: (value, record) => record.name.indexOf(value) > -1,
  642. },
  643. {
  644. title: 'Age',
  645. dataIndex: 'age',
  646. width: 150,
  647. sorter: true,
  648. },
  649. {
  650. title: 'Address',
  651. dataIndex: 'address',
  652. },
  653. {
  654. render: (text, record) => (
  655. <Tooltip content={record.description}>
  656. <Tag color="green">Show Info</Tag>
  657. </Tooltip>
  658. ),
  659. width: 150,
  660. },
  661. ];
  662. const onChange = sinon.spy(({pagination: tablePagination, filters: tableFilters, sorters: tableSorters}) => {
  663. if (tablePagination && tablePagination.currentPage !== pagination.currentPage) {
  664. pagination.currentPage = tablePagination.currentPage;
  665. currentData = getPageData();
  666. columns = [...columns];
  667. demo.setProps({
  668. dataSource: currentData,
  669. columns,
  670. pagination,
  671. });
  672. }
  673. });
  674. const demo = mount(
  675. <Table onChange={onChange} dataSource={currentData} pagination={pagination} columns={columns}/>
  676. );
  677. const table = demo.find(BaseTable);
  678. demo.find(`.${BASE_CLASS_PREFIX}-page .${BASE_CLASS_PREFIX}-page-item`)
  679. .at(2)
  680. .simulate('click', {
  681. nativeEvent: null,
  682. });
  683. expect(onChange.called).toBe(true);
  684. expect(
  685. demo
  686. .find(`.${BASE_CLASS_PREFIX}-page .${BASE_CLASS_PREFIX}-page-item`)
  687. .at(2)
  688. .hasClass(`${BASE_CLASS_PREFIX}-page-item-active`)
  689. ).toBe(true);
  690. });
  691. it(`test resizable table`, async () => {
  692. let columns = [
  693. {
  694. title: 'Name',
  695. dataIndex: 'name',
  696. width: 150,
  697. filters: [
  698. {
  699. text: '名字包含"1"',
  700. value: '1',
  701. },
  702. {
  703. text: '名字包含"2"',
  704. value: '2',
  705. },
  706. ],
  707. onFilter: (value, record) => record.name.indexOf(value) > -1,
  708. },
  709. {
  710. title: 'Age',
  711. dataIndex: 'age',
  712. width: 150,
  713. sorter: true,
  714. },
  715. {
  716. title: 'Address',
  717. dataIndex: 'address',
  718. width: 300,
  719. },
  720. {
  721. render: (text, record) => (
  722. <Tooltip content={record.description}>
  723. <Tag color="green">Show Info</Tag>
  724. </Tooltip>
  725. ),
  726. },
  727. ];
  728. const expandedRowRender = (record, index, expanded) => (
  729. <article
  730. style={{
  731. margin: 0,
  732. }}
  733. >
  734. <p>{record.description}</p>
  735. </article>
  736. );
  737. const demo = mount(
  738. <Table columns={columns} dataSource={data} resizable expandedRowRender={expandedRowRender}/>
  739. );
  740. expect(demo.find(`.${BASE_CLASS_PREFIX}-table-thead .react-resizable-handle`).length).toBe(
  741. columns.reduce((count, col) => (typeof col.width === 'number' ? count + 1 : count), 0)
  742. );
  743. });
  744. it('test table sortOrder descend', async () => {
  745. const sortColumns = [
  746. {
  747. title: 'Name',
  748. dataIndex: 'name',
  749. sorter: (a, b) => a.name.length - b.name.length,
  750. sortOrder: 'descend',
  751. },
  752. {
  753. title: 'Age',
  754. dataIndex: 'age',
  755. },
  756. {
  757. title: 'Address',
  758. dataIndex: 'address',
  759. },
  760. ];
  761. const sortData = [
  762. {
  763. key: '1',
  764. name: 'John Brown',
  765. age: 32,
  766. address: 'New York No. 1 Lake Park',
  767. },
  768. {
  769. key: '2',
  770. name: 'Jim Green',
  771. age: 42,
  772. address: 'London No. 1 Lake Park',
  773. },
  774. {
  775. key: '3',
  776. name: 'Joe Black',
  777. age: 32,
  778. address: 'Sidney No. 1 Lake Park',
  779. },
  780. {
  781. key: '4',
  782. name: 'Jim Red',
  783. age: 32,
  784. address: 'London No. 2 Lake Park',
  785. },
  786. ];
  787. const targetData = sortData.reduce((a, b) => (a.name.length > b.name.length ? a : b));
  788. const {name} = targetData;
  789. const sortTable = mount(<Table columns={sortColumns} dataSource={sortData}/>);
  790. const firstCell = sortTable.find('.semi-table-tbody .semi-table-row .semi-table-row-cell').at(0); // console.log(sortTable.debug({ ignoreProps: true }));
  791. expect(firstCell.text()).toBe(name);
  792. });
  793. it('test table sortOrder ascend', async () => {
  794. const sortColumns = [
  795. {
  796. title: 'Name',
  797. dataIndex: 'name',
  798. sorter: (a, b) => a.name.length - b.name.length,
  799. sortOrder: 'ascend',
  800. },
  801. {
  802. title: 'Age',
  803. dataIndex: 'age',
  804. },
  805. {
  806. title: 'Address',
  807. dataIndex: 'address',
  808. },
  809. ];
  810. const sortData = [
  811. {
  812. key: '1',
  813. name: 'John Brown',
  814. age: 32,
  815. address: 'New York No. 1 Lake Park',
  816. },
  817. {
  818. key: '2',
  819. name: 'Jim Green',
  820. age: 42,
  821. address: 'London No. 1 Lake Park',
  822. },
  823. {
  824. key: '3',
  825. name: 'Joe Black',
  826. age: 32,
  827. address: 'Sidney No. 1 Lake Park',
  828. },
  829. {
  830. key: '4',
  831. name: 'Jim Red',
  832. age: 32,
  833. address: 'London No. 2 Lake Park',
  834. },
  835. ];
  836. const targetData = sortData.reduce((a, b) => (a.name.length < b.name.length ? a : b));
  837. const {name} = targetData;
  838. const sortTable = mount(<Table columns={sortColumns} dataSource={sortData}/>);
  839. const firstCell = sortTable.find('.semi-table-tbody .semi-table-row .semi-table-row-cell').at(0);
  840. expect(firstCell.text()).toBe(name);
  841. });
  842. it('test controlled sortOrder', async () => {
  843. const sortColumns = [
  844. {
  845. title: 'Name',
  846. dataIndex: 'name',
  847. sorter: (a, b) => a.name.length - b.name.length,
  848. sortOrder: 'descend',
  849. },
  850. {
  851. title: 'Age',
  852. dataIndex: 'age',
  853. },
  854. {
  855. title: 'Address',
  856. dataIndex: 'address',
  857. },
  858. ];
  859. const sortData = [
  860. {
  861. key: '1',
  862. name: 'long name',
  863. age: 32,
  864. address: 'New York No. 1 Lake Park',
  865. },
  866. {
  867. key: '2',
  868. name: 'longest name',
  869. age: 42,
  870. address: 'London No. 1 Lake Park',
  871. },
  872. {
  873. key: '3',
  874. name: 'longer name',
  875. age: 32,
  876. address: 'Sidney No. 1 Lake Park',
  877. },
  878. {
  879. key: '4',
  880. name: 'short',
  881. age: 32,
  882. address: 'London No. 2 Lake Park',
  883. },
  884. ];
  885. const targetData = sortData.reduce((a, b) => (a.name.length > b.name.length ? a : b));
  886. const {name} = targetData;
  887. const sortTable = mount(<Table columns={sortColumns} dataSource={sortData}/>);
  888. const firstCell = sortTable.find('.semi-table-tbody .semi-table-row .semi-table-row-cell').at(0);
  889. expect(firstCell.text()).toBe(name);
  890. const ascendOrderColumns = [
  891. {
  892. title: 'Name',
  893. dataIndex: 'name',
  894. sorter: (a, b) => a.name.length - b.name.length,
  895. sortOrder: 'ascend',
  896. },
  897. {
  898. title: 'Age',
  899. dataIndex: 'age',
  900. },
  901. {
  902. title: 'Address',
  903. dataIndex: 'address',
  904. },
  905. ]; // test sortOrder: ascend
  906. sortTable.setProps({
  907. columns: ascendOrderColumns,
  908. });
  909. sortTable.update();
  910. const ascendTargetData = sortData.reduce((a, b) => (a.name.length < b.name.length ? a : b));
  911. const {name: ascendTargetName} = ascendTargetData;
  912. const ascendFirstCell = sortTable.find('.semi-table-tbody .semi-table-row .semi-table-row-cell').at(0);
  913. expect(ascendFirstCell.text()).toBe(ascendTargetName); // test sortOrder: false, expect order as given data
  914. const defaultOrderColumns = [
  915. {
  916. title: 'Name',
  917. dataIndex: 'name',
  918. sorter: (a, b) => a.name.length - b.name.length,
  919. sortOrder: false,
  920. },
  921. {
  922. title: 'Age',
  923. dataIndex: 'age',
  924. },
  925. {
  926. title: 'Address',
  927. dataIndex: 'address',
  928. },
  929. ];
  930. sortTable.setProps({
  931. columns: defaultOrderColumns,
  932. });
  933. sortTable.update();
  934. const defaultTargetData = sortData[0];
  935. const {name: defaultTargetName} = defaultTargetData;
  936. const defaultFirstCell = sortTable.find('.semi-table-tbody .semi-table-row .semi-table-row-cell').at(0);
  937. expect(defaultFirstCell.text()).toBe(defaultTargetName);
  938. });
  939. it('test table select all when disabled all rows', async () => {
  940. const columns = [
  941. {
  942. title: 'Name',
  943. dataIndex: 'name',
  944. width: 150,
  945. },
  946. {
  947. title: 'Age',
  948. dataIndex: 'age',
  949. width: 150,
  950. },
  951. {
  952. title: 'Address',
  953. dataIndex: 'address',
  954. },
  955. ];
  956. const rowSelection = {
  957. onSelectAll: sinon.spy(() => {
  958. }),
  959. getCheckboxProps: sinon.spy(record => ({
  960. disabled: true,
  961. // disabled all
  962. name: record.name,
  963. })),
  964. };
  965. const demo = mount(<Table columns={columns} dataSource={data} rowSelection={rowSelection}/>);
  966. demo.find(
  967. `.${BASE_CLASS_PREFIX}-table-thead .${BASE_CLASS_PREFIX}-table-column-selection .${BASE_CLASS_PREFIX}-checkbox`
  968. ).simulate('click'); // should select 0 rows
  969. expect(rowSelection.onSelectAll.getCall(0).args[1].length).toBe(0);
  970. });
  971. it('test jsx async data', async () => {
  972. const asyncData = [
  973. {
  974. key: '1',
  975. name: 'John Brown',
  976. age: 32,
  977. address: 'New York No. 1 Lake Park, New York No. 1 Lake Park',
  978. },
  979. {
  980. key: '2',
  981. name: 'Jim Green',
  982. age: 42,
  983. address: 'London No. 1 Lake Park',
  984. },
  985. {
  986. key: '3',
  987. name: 'Joe Black',
  988. age: 32,
  989. address: 'Sidney No. 1 Lake Park',
  990. },
  991. {
  992. key: '4',
  993. name: 'Michael James',
  994. age: 99,
  995. address: 'Sidney No. 1 Lake Park',
  996. },
  997. ];
  998. const demo = mount(
  999. <Table dataSource={[]}>
  1000. <Column title="Name" dataIndex="name" key="name" render={(text, record, index) => <a>{text}</a>}/>
  1001. <Column title="Age" dataIndex="age" key="age"/>
  1002. <Column title="Address" dataIndex="address" key="address"/>
  1003. </Table>
  1004. );
  1005. setTimeout(() => {
  1006. demo.setProps({
  1007. dataSource: asyncData,
  1008. });
  1009. }, 2000);
  1010. await sleep(3000);
  1011. const baseTable = demo.find(BaseTable);
  1012. expect(baseTable.state('cachedColumns').length).toEqual(3);
  1013. expect(baseTable.state('dataSource').length).toEqual(4);
  1014. });
  1015. it('test cell align', async () => {
  1016. const columns = getColumns();
  1017. const alignRightColumns = columns.map(column => {
  1018. column.align = 'right';
  1019. return column;
  1020. });
  1021. const demo = mount(<Table dataSource={data} columns={alignRightColumns}/>);
  1022. const titleAlignCells = demo.find(`.${BASE_CLASS_PREFIX}-table-align-right`);
  1023. expect(titleAlignCells.length).toBeGreaterThan(0);
  1024. const bodyAlignCells = document.querySelectorAll(`.${BASE_CLASS_PREFIX}-table-row-cell`);
  1025. bodyAlignCells.forEach(cell => {
  1026. expect(cell.style.textAlign).toEqual('right');
  1027. });
  1028. });
  1029. it('test column className', async () => {
  1030. const columns = getColumns();
  1031. const alignRightColumns = columns.map(column => {
  1032. column.className = 'test';
  1033. return column;
  1034. });
  1035. const demo = mount(<Table dataSource={data} columns={alignRightColumns}/>);
  1036. const titleAlignCells = demo.find(`.${BASE_CLASS_PREFIX}-table-row-head`);
  1037. const bodyAlignCells = demo.find(`.${BASE_CLASS_PREFIX}-table-row-cell`);
  1038. titleAlignCells.forEach(cell => {
  1039. expect(cell.hasClass('test')).toBeTruthy();
  1040. });
  1041. bodyAlignCells.forEach(cell => {
  1042. expect(cell.hasClass('test')).toBeTruthy();
  1043. });
  1044. });
  1045. it('test header appearance', async () => {
  1046. // object column
  1047. const columns = getColumns();
  1048. const demo = mount(
  1049. <Table
  1050. dataSource={data}
  1051. columns={columns}
  1052. scroll={{
  1053. y: 500,
  1054. }}
  1055. />
  1056. );
  1057. expect(demo.find('.semi-table-thead').length).toEqual(1);
  1058. demo.setProps({
  1059. showHeader: false,
  1060. });
  1061. expect(demo.find('.semi-table-thead').length).toEqual(0); // jsx column
  1062. const jsxColumns = getJSXColumns();
  1063. const demo1 = mount(<Table dataSource={data}>{jsxColumns}</Table>);
  1064. expect(demo1.find('.semi-table-thead').length).toEqual(1);
  1065. demo1.setProps({
  1066. showHeader: false,
  1067. });
  1068. expect(demo1.find('.semi-table-thead').length).toEqual(0);
  1069. });
  1070. it('test ref getCurrentPageData', async () => {
  1071. // object column
  1072. const pageSize = 10;
  1073. const refObj = {
  1074. current: null,
  1075. };
  1076. const columns = getColumns();
  1077. const demo = mount(
  1078. <Table
  1079. dataSource={data}
  1080. ref={refObj}
  1081. columns={columns}
  1082. pagination={{
  1083. pageSize,
  1084. }}
  1085. />
  1086. );
  1087. const {dataSource} = refObj.current.getCurrentPageData();
  1088. expect(dataSource.length).toEqual(pageSize); // jsx column
  1089. const jsxColumns = getJSXColumns();
  1090. const jsxRefObj = {
  1091. current: null,
  1092. };
  1093. const jsxDemo = mount(
  1094. <Table
  1095. dataSource={data}
  1096. ref={jsxRefObj}
  1097. pagination={{
  1098. pageSize,
  1099. }}
  1100. >
  1101. {jsxColumns}
  1102. </Table>
  1103. );
  1104. const {dataSource: jsxData} = jsxRefObj.current.getCurrentPageData();
  1105. expect(jsxData.length).toEqual(pageSize);
  1106. });
  1107. it('test render expandIcon', async () => {
  1108. const expandedRowRender = () => <div>Semi Design</div>;
  1109. const columns = getColumns();
  1110. const demo = mount(
  1111. <Table
  1112. dataSource={data}
  1113. columns={columns}
  1114. expandedRowRender={expandedRowRender}
  1115. expandIcon={<IconStar size="small"/>}
  1116. />
  1117. );
  1118. expect(demo.find('.semi-icon-star').length).toBeGreaterThan(0);
  1119. const demo2 = mount(
  1120. <Table
  1121. dataSource={data}
  1122. columns={columns}
  1123. expandedRowRender={expandedRowRender}
  1124. defaultExpandAllRows
  1125. expandIcon={expanded => <div>{expanded && <IconStar size="small"/>}</div>}
  1126. />
  1127. );
  1128. expect(demo2.find('.semi-icon-star').length).toBeGreaterThan(0);
  1129. });
  1130. it(`test onRow/onCell`, async () => {
  1131. const onRowClick = sinon.spy(() => {
  1132. });
  1133. const onHeaderRowClick = sinon.spy(() => {
  1134. });
  1135. const onCellClick = sinon.spy(() => {
  1136. });
  1137. const onHeaderCellClick = sinon.spy(() => {
  1138. });
  1139. const onRow = sinon.spy((record, index) => ({
  1140. className: 'test-row',
  1141. onClick: onRowClick,
  1142. }));
  1143. const onHeaderRow = sinon.spy((record, index) => ({
  1144. className: 'test-row',
  1145. onClick: onHeaderRowClick,
  1146. }));
  1147. const columns = getColumns();
  1148. const onCellColumns = columns.map((column, index) => {
  1149. const style =
  1150. index === 0
  1151. ? {
  1152. width: 200,
  1153. height: 60,
  1154. }
  1155. : {};
  1156. column.onCell = () => ({
  1157. onClick: onCellClick,
  1158. onCellClick,
  1159. style,
  1160. });
  1161. column.onHeaderCell = () => ({
  1162. onClick: onHeaderCellClick,
  1163. style,
  1164. });
  1165. return column;
  1166. });
  1167. const demo = mount(<Table dataSource={data} onRow={onRow} onHeaderRow={onHeaderRow} columns={onCellColumns}/>);
  1168. const tableCells = demo.find('.semi-table-body .semi-table-row-cell');
  1169. const tableHeaderCells = demo.find('.semi-table-thead .semi-table-row-head'); // cell style
  1170. expect(tableCells.at(0).instance().style.width).toEqual('200px');
  1171. expect(tableCells.at(0).instance().style.height).toEqual('60px');
  1172. expect(tableHeaderCells.at(0).instance().style.width).toEqual('200px');
  1173. expect(tableHeaderCells.at(0).instance().style.height).toEqual('60px'); // body click
  1174. tableCells.at(0).simulate('click');
  1175. expect(onCellClick.called).toBeTruthy();
  1176. const tableRows = demo.find('.semi-table-body .semi-table-row');
  1177. tableRows.at(0).simulate('click');
  1178. expect(onRowClick.called).toBeTruthy(); // header click
  1179. tableHeaderCells.at(0).simulate('click');
  1180. expect(onHeaderCellClick.called).toBeTruthy();
  1181. const tableHeaderRows = demo.find('.semi-table-thead .semi-table-row');
  1182. tableHeaderRows.at(0).simulate('click');
  1183. expect(onHeaderRowClick.called).toBeTruthy();
  1184. });
  1185. it('test header merge', async () => {
  1186. // 测试头部合并
  1187. function testHeaderMerge(demo, params) {
  1188. const rows = demo.find('.semi-table-thead .semi-table-row');
  1189. expect(rows.length).toEqual(2);
  1190. const firstRow = rows.at(0);
  1191. const secondRow = rows.at(1);
  1192. expect(firstRow.childAt(0).instance().colSpan).toEqual(2);
  1193. expect(firstRow.childAt(1).instance().colSpan).toEqual(2);
  1194. expect(firstRow.childAt(2).instance().colSpan).toEqual(1);
  1195. expect(firstRow.childAt(2).instance().rowSpan).toEqual(2);
  1196. expect(secondRow.childAt(0).instance().colSpan).toEqual(1);
  1197. expect(secondRow.childAt(1).instance().colSpan).toEqual(1);
  1198. expect(secondRow.childAt(2).instance().colSpan).toEqual(1);
  1199. expect(secondRow.childAt(3).instance().colSpan).toEqual(1);
  1200. }
  1201. const nestData = getNestData();
  1202. const filters = [
  1203. {
  1204. text: 'Code 45',
  1205. value: '45',
  1206. },
  1207. {
  1208. text: 'King 4',
  1209. value: 'King 4',
  1210. },
  1211. ];
  1212. const jsxDemo = mount(
  1213. <Table dataSource={nestData}>
  1214. <Column title={'Base Information'} dataIndex={'base'} fixed="left">
  1215. <Column
  1216. title={'Name'}
  1217. dataIndex={'name'}
  1218. fixed="left"
  1219. width={200}
  1220. filters={filters}
  1221. onFilter={(value, record) => record.name.includes(value)}
  1222. />
  1223. <Column
  1224. title={'Age'}
  1225. dataIndex={'age'}
  1226. width={100}
  1227. fixed="left"
  1228. sorter={(a, b) => (a.age - b.age > 0 ? 1 : -1)}
  1229. />
  1230. </Column>
  1231. <Column title={'Company Information'} dataIndex={'company'}>
  1232. <Column title={'Company Name'} dataIndex={'company.name'}/>
  1233. <Column title={'Company Address'} dataIndex={'company.address'}/>
  1234. </Column>
  1235. <Column title={'Address'} dataIndex={'address'} width={250} fixed="right"/>
  1236. </Table>
  1237. );
  1238. const columns = getNestColumns();
  1239. const demo = mount(<Table dataSource={nestData} columns={columns}/>);
  1240. testHeaderMerge(jsxDemo);
  1241. testHeaderMerge(demo);
  1242. });
  1243. it('test without columns', async () => {
  1244. const jsxDemo = mount(<Table dataSource={data}></Table>);
  1245. const demo = mount(<Table dataSource={data} columns={[]}/>);
  1246. expect(jsxDemo.find('.semi-table-row-head').length).toBe(0);
  1247. expect(jsxDemo.find('.semi-table-row-cell').length).toBe(0);
  1248. expect(demo.find('.semi-table-row-head').length).toBe(0);
  1249. expect(demo.find('.semi-table-row-cell').length).toBe(0);
  1250. });
  1251. it('test useFullRender', async () => {
  1252. const columns = [
  1253. {
  1254. dataIndex: 'name',
  1255. width: 250,
  1256. filters: [
  1257. {
  1258. text: '名字包含"1"',
  1259. value: '1',
  1260. },
  1261. {
  1262. text: '名字包含"2"',
  1263. value: '2',
  1264. },
  1265. ],
  1266. onFilter: (value, record) => record.name.indexOf(value) > -1,
  1267. sorter: (a, b) => a.name.length - b.name.length,
  1268. // 此处将useFullRender设置为true开启完全自定义渲染
  1269. useFullRender: true,
  1270. // 此处从render的第四个形参中解构出 展开按钮、选择按钮、文本等内容
  1271. render: (text, record, index, {expandIcon, selection, indentText}) => {
  1272. return (
  1273. <span
  1274. className="custom-render"
  1275. style={{
  1276. display: 'inline-flex',
  1277. alignItems: 'center',
  1278. justifyContent: 'center',
  1279. }}
  1280. >
  1281. {indentText}
  1282. {expandIcon}
  1283. {selection}
  1284. <span
  1285. style={{
  1286. marginLeft: 8,
  1287. }}
  1288. >
  1289. {text}
  1290. </span>
  1291. </span>
  1292. );
  1293. },
  1294. title: ({sorter, filter, selection}) => (
  1295. <span
  1296. className="custom-title"
  1297. style={{
  1298. display: 'inline-flex',
  1299. alignItems: 'center',
  1300. paddingLeft: 20,
  1301. }}
  1302. >
  1303. {selection}
  1304. <span
  1305. style={{
  1306. marginLeft: 8,
  1307. }}
  1308. >
  1309. Name
  1310. </span>
  1311. {sorter}
  1312. {filter}
  1313. </span>
  1314. ),
  1315. },
  1316. {
  1317. title: 'Age',
  1318. dataIndex: 'age',
  1319. },
  1320. {
  1321. title: 'Address',
  1322. dataIndex: 'address',
  1323. },
  1324. ];
  1325. const rowSelection = {
  1326. hidden: true,
  1327. fixed: 'left',
  1328. };
  1329. const demo = mount(
  1330. <Table
  1331. rowSelection={rowSelection}
  1332. columns={columns}
  1333. dataSource={data}
  1334. expandedRowRender={record => <article>{record.description}</article>}
  1335. />
  1336. );
  1337. const node = demo.find('.custom-render').at(0);
  1338. expect(node.children().length).toEqual(3);
  1339. expect(node.childAt(0).exists('.semi-table-expand-icon')).toBeTruthy();
  1340. expect(node.childAt(1).exists('.semi-table-selection-wrap')).toBeTruthy();
  1341. const title = demo.find('.custom-title').at(0);
  1342. expect(title.children().length).toEqual(4);
  1343. expect(title.childAt(0).exists('.semi-table-selection-wrap')).toBeTruthy();
  1344. expect(title.childAt(1).text()).toEqual('Name');
  1345. expect(title.childAt(2).exists('.semi-table-column-sorter')).toBeTruthy();
  1346. expect(title.childAt(3).exists('.semi-table-column-filter')).toBeTruthy();
  1347. });
  1348. it('test defaultExpandedRowKeys changed', async () => {
  1349. const expandedRowRender = (record, index) => <div>{`Semi Design ${index}`}</div>;
  1350. const pageSize = 20;
  1351. const defaultExpandedRowKeys = Array.from({
  1352. length: getRandomNumber(pageSize),
  1353. }).map((_, i) => String(i));
  1354. const newDefaultExpandedRowKeys = Array.from({
  1355. length: getRandomNumber(pageSize),
  1356. }).map((_, i) => String(i));
  1357. const columns = getColumns();
  1358. const demo = mount(
  1359. <Table
  1360. dataSource={data}
  1361. columns={columns}
  1362. expandedRowRender={expandedRowRender}
  1363. defaultExpandedRowKeys={defaultExpandedRowKeys}
  1364. pagination={{
  1365. pageSize,
  1366. }}
  1367. />
  1368. );
  1369. expect(demo.find(BaseTable).state('expandedRowKeys').length).toEqual(defaultExpandedRowKeys.length);
  1370. demo.setProps({
  1371. defaultExpandedRowKeys: newDefaultExpandedRowKeys,
  1372. });
  1373. // 2.x defaultExpandedRowKeys 不再响应变化
  1374. expect(demo.find(BaseTable).state('expandedRowKeys').length).toEqual(defaultExpandedRowKeys.length);
  1375. });
  1376. it('test getCheckboxProps changed', async () => {
  1377. const defaultGetCheckboxProps = record => ({
  1378. disabled: record.key === '1',
  1379. });
  1380. const newGetCheckboxProps = record => ({
  1381. disabled: ['0', '1'].includes(record.key),
  1382. });
  1383. const columns = getColumns();
  1384. const demo = mount(
  1385. <Table
  1386. dataSource={getData(20)}
  1387. columns={columns}
  1388. pagination={false}
  1389. rowSelection={{
  1390. getCheckboxProps: defaultGetCheckboxProps,
  1391. }}
  1392. />
  1393. );
  1394. expect(demo.find(BaseTable).state('disabledRowKeys').length).toEqual(1);
  1395. demo.setProps({
  1396. rowSelection: {
  1397. getCheckboxProps: newGetCheckboxProps,
  1398. },
  1399. });
  1400. expect(demo.find(BaseTable).state('disabledRowKeys').length).toEqual(2);
  1401. });
  1402. /**
  1403. * 分页受控场景,更新数据后查看分页器是否保持当前页
  1404. */
  1405. it('test controlled pagination reset when dataSource change', async () => {
  1406. const total = 100;
  1407. const pagination = {
  1408. pageSize: 10,
  1409. currentPage: 2,
  1410. };
  1411. const columns = getColumns();
  1412. const demo = mount(<Table dataSource={getData(total)} columns={columns} pagination={pagination}/>);
  1413. const dataNum = getRandomNumber(100, 40);
  1414. const newData = getData(dataNum);
  1415. demo.setProps({
  1416. dataSource: newData,
  1417. });
  1418. await sleep(2000);
  1419. expect(
  1420. demo
  1421. .find(`.${BASE_CLASS_PREFIX}-page .${BASE_CLASS_PREFIX}-page-item-active`)
  1422. .getDOMNode().innerHTML
  1423. ).toBe('2');
  1424. });
  1425. /**
  1426. * 分页非受控场景,更新数据后查看分页器是否重置
  1427. */
  1428. it('test uncontrolled pagination reset when dataSource change', async () => {
  1429. const total = 100;
  1430. const columns = getColumns();
  1431. const demo = mount(<Table dataSource={getData(total)} columns={columns}/>);
  1432. demo.find(`.${BASE_CLASS_PREFIX}-page .${BASE_CLASS_PREFIX}-page-item`)
  1433. .at(2)
  1434. .simulate('click');
  1435. const dataNum = getRandomNumber(100, 40);
  1436. const newData = getData(dataNum);
  1437. demo.setProps({
  1438. dataSource: newData,
  1439. });
  1440. await sleep(2000);
  1441. demo.update();
  1442. expect(demo.find(`.semi-page .semi-page-item-active`).getDOMNode().innerHTML).toBe('1');
  1443. expect(demo.find(BaseTable).state('pagination')).toHaveProperty('currentPage', 1);
  1444. });
  1445. it('test pagination changed', async () => {
  1446. const total = 100;
  1447. const pagination = {
  1448. pageSize: 10,
  1449. currentPage: 2,
  1450. };
  1451. const newPagination = {
  1452. pageSize: 5,
  1453. currentPage: 1,
  1454. };
  1455. const columns = getColumns();
  1456. const demo = mount(<Table dataSource={getData(total)} columns={columns} pagination={pagination}/>);
  1457. const paginationProps = demo.find(BaseTable).state('pagination');
  1458. expect(paginationProps.total).toEqual(total);
  1459. expect(paginationProps.pageSize).toEqual(pagination.pageSize);
  1460. expect(paginationProps.currentPage).toEqual(pagination.currentPage);
  1461. demo.setProps({
  1462. pagination: newPagination,
  1463. });
  1464. await sleep(2000);
  1465. const newPaginationProps = demo.find(BaseTable).state('pagination');
  1466. expect(newPaginationProps.pageSize).toEqual(newPagination.pageSize);
  1467. expect(newPaginationProps.currentPage).toEqual(newPagination.currentPage);
  1468. });
  1469. it(`test grouped data change dataSource`, async () => {
  1470. const data = getGroupData();
  1471. const columns = getGroupColumns();
  1472. const groupedRowClick = sinon.spy();
  1473. const rowKey = record =>
  1474. `${record.city && record.city.toLowerCase()}-${record.job && record.job.toLowerCase()}`;
  1475. const demo = mount(
  1476. <Table
  1477. dataSource={data}
  1478. rowKey={rowKey}
  1479. groupBy={'city'}
  1480. columns={columns}
  1481. renderGroupSection={groupKey => <strong>Jobs in {groupKey}:</strong>}
  1482. onGroupedRow={(group, index) => ({
  1483. onClick: groupedRowClick,
  1484. })}
  1485. clickGroupedRowToExpand
  1486. scroll={{
  1487. y: 480,
  1488. }}
  1489. />
  1490. );
  1491. const newData = [
  1492. {
  1493. city: 'Tianjin',
  1494. job: 'FE',
  1495. department: 'IES',
  1496. },
  1497. {
  1498. city: 'Tianjin',
  1499. job: 'Android',
  1500. department: 'IES',
  1501. },
  1502. {
  1503. city: 'Wuhan',
  1504. job: 'Android',
  1505. department: 'IES',
  1506. },
  1507. {
  1508. city: 'Wuhan',
  1509. job: 'SE',
  1510. department: 'EE',
  1511. },
  1512. {
  1513. city: 'Chengdu',
  1514. job: 'Android',
  1515. department: 'EE',
  1516. },
  1517. {
  1518. city: 'Chengdu',
  1519. job: 'IOS',
  1520. department: 'EE',
  1521. },
  1522. {
  1523. city: 'Xiamen',
  1524. job: 'SE',
  1525. department: 'DATA',
  1526. },
  1527. {
  1528. city: 'Xiamen',
  1529. job: 'IOS',
  1530. department: 'DATA',
  1531. },
  1532. ];
  1533. demo.setProps({
  1534. dataSource: newData,
  1535. });
  1536. expect(demo.find(`.${BASE_CLASS_PREFIX}-table-row-section`).length).toBe(4);
  1537. expect(demo.find(`.${BASE_CLASS_PREFIX}-table-body .${BASE_CLASS_PREFIX}-table-row`).length).toBe(0);
  1538. demo.find(`.${BASE_CLASS_PREFIX}-table-row-section`)
  1539. .at(0)
  1540. .simulate('click');
  1541. expect(demo.find(`.${BASE_CLASS_PREFIX}-table-row`).length).toBeGreaterThan(0);
  1542. });
  1543. it(`test given object columns and children`, async () => {
  1544. const demo = mount(
  1545. <Table dataSource={data} columns={columns}>
  1546. <Table.Column title="Age" dataIndex="age" key="age"/>
  1547. </Table>
  1548. );
  1549. const test = demo.find(BaseTable); // 同时传columns与children时,columns优先
  1550. expect(test.state('cachedColumns')).toEqual(columns);
  1551. expect(test.state('queries').length).toEqual(columns.length);
  1552. });
  1553. it('test header colSpan', async () => {
  1554. const columns = [
  1555. {
  1556. title: 'Name',
  1557. dataIndex: 'name',
  1558. },
  1559. {
  1560. title: 'Age',
  1561. dataIndex: 'age',
  1562. },
  1563. {
  1564. title: 'Home phone',
  1565. colSpan: 2,
  1566. dataIndex: 'tel',
  1567. },
  1568. {
  1569. title: 'Phone',
  1570. colSpan: 0,
  1571. dataIndex: 'phone',
  1572. },
  1573. {
  1574. title: 'Address',
  1575. dataIndex: 'address',
  1576. },
  1577. ];
  1578. const data = [
  1579. {
  1580. key: '1',
  1581. name: 'ZhangSan',
  1582. age: 50,
  1583. tel: '010-20000000',
  1584. phone: 10010,
  1585. address: 'BeiJing No.1 High School',
  1586. },
  1587. {
  1588. key: '2',
  1589. name: 'LiSi',
  1590. tel: '010-30000000',
  1591. phone: 10086,
  1592. age: 40,
  1593. address: 'ShangHai No. 1 High School',
  1594. },
  1595. {
  1596. key: '3',
  1597. name: 'WangWu',
  1598. age: 60,
  1599. tel: '010-40000000',
  1600. phone: 10011,
  1601. address: 'NaiJing No.1 High School',
  1602. },
  1603. {
  1604. key: '4',
  1605. name: 'XiaoMing',
  1606. age: 20,
  1607. tel: '010-50000000',
  1608. phone: 12580,
  1609. address: 'ShiJiaZhuang No.1 High School',
  1610. },
  1611. {
  1612. key: '5',
  1613. name: 'XiaoHong',
  1614. age: 40,
  1615. tel: '010-60000000',
  1616. phone: 12530,
  1617. address: 'TaiBei No.2 High School',
  1618. },
  1619. ];
  1620. const demo = mount(<Table dataSource={data} columns={columns}/>);
  1621. expect(demo.find('.semi-table-row .semi-table-row-head').length).toBe(columns.length - 1);
  1622. });
  1623. it('test expandAllRows', async () => {
  1624. const expandedRowRender = () => <div>Semi Design</div>
  1625. const initData = [];
  1626. const columns = getColumns();
  1627. const demo = mount(<Table
  1628. dataSource={initData}
  1629. columns={columns}
  1630. expandedRowRender={expandedRowRender}
  1631. pagination={false}
  1632. expandAllRows
  1633. />);
  1634. const newData = getData(20);
  1635. demo.setProps({dataSource: newData});
  1636. demo.update();
  1637. const expandedRows = demo.find(`.${BASE_CLASS_PREFIX}-table-tbody .${BASE_CLASS_PREFIX}-table-row-expanded`)
  1638. expect(expandedRows.length).toEqual(newData.length);
  1639. // 动态切换 expandAllRows
  1640. demo.setProps({expandAllRows: false});
  1641. demo.update();
  1642. const newExpandedRows = demo.find(`.${BASE_CLASS_PREFIX}-table-tbody .${BASE_CLASS_PREFIX}-table-row-expanded`)
  1643. expect(newExpandedRows.length).toEqual(0);
  1644. });
  1645. it(`test defaultExpandAllGroupRows`, async () => {
  1646. const data = getGroupData();
  1647. const columns = getGroupColumns();
  1648. const rowKey = record => `${record.city && record.city.toLowerCase()}-${record.job && record.job.toLowerCase()}`;
  1649. const groupSize = new Set(data.map(item => item.city)).size;
  1650. const demo = mount(
  1651. <Table
  1652. dataSource={data}
  1653. rowKey={rowKey}
  1654. groupBy={'city'}
  1655. columns={columns}
  1656. renderGroupSection={groupKey => <strong>Jobs in {groupKey}:</strong>}
  1657. scroll={{y: 480}}
  1658. defaultExpandAllGroupRows
  1659. />
  1660. );
  1661. expect(demo.find(`.${BASE_CLASS_PREFIX}-table-tbody .${BASE_CLASS_PREFIX}-table-row-section.on`).length).toBe(groupSize);
  1662. });
  1663. it(`test expandAllGroupRows`, async () => {
  1664. const data = getGroupData();
  1665. const groupSize = new Set(data.map(item => item.city)).size;
  1666. const columns = getGroupColumns();
  1667. const rowKey = record => `${record.city && record.city.toLowerCase()}-${record.job && record.job.toLowerCase()}`;
  1668. const demo = mount(
  1669. <Table
  1670. dataSource={[]}
  1671. rowKey={rowKey}
  1672. groupBy={'city'}
  1673. columns={columns}
  1674. renderGroupSection={groupKey => <strong>Jobs in {groupKey}:</strong>}
  1675. scroll={{y: 480}}
  1676. expandAllGroupRows={true}
  1677. />
  1678. );
  1679. demo.setProps({dataSource: data});
  1680. demo.update();
  1681. expect(demo.find(`.${BASE_CLASS_PREFIX}-table-tbody .${BASE_CLASS_PREFIX}-table-row-section.on`).length).toBe(groupSize);
  1682. demo.setProps({expandAllGroupRows: false});
  1683. demo.update();
  1684. expect(demo.find(`.${BASE_CLASS_PREFIX}-table-tbody .${BASE_CLASS_PREFIX}-table-row-section.on`).length).toBe(0);
  1685. });
  1686. it('test defaultSortOrder', async () => {
  1687. const sortColumns = [
  1688. {
  1689. title: 'Name',
  1690. dataIndex: 'name',
  1691. sorter: (a, b) => a.name.length - b.name.length,
  1692. defaultSortOrder: 'descend',
  1693. },
  1694. {
  1695. title: 'Age',
  1696. dataIndex: 'age',
  1697. },
  1698. {
  1699. title: 'Address',
  1700. dataIndex: 'address',
  1701. }
  1702. ];
  1703. const sortData = [
  1704. {
  1705. key: '1',
  1706. name: 'long name',
  1707. age: 32,
  1708. address: 'New York No. 1 Lake Park',
  1709. },
  1710. {
  1711. key: '2',
  1712. name: 'longest name',
  1713. age: 42,
  1714. address: 'London No. 1 Lake Park',
  1715. },
  1716. {
  1717. key: '3',
  1718. name: 'longer name',
  1719. age: 32,
  1720. address: 'Sidney No. 1 Lake Park',
  1721. },
  1722. {
  1723. key: '4',
  1724. name: 'short',
  1725. age: 32,
  1726. address: 'London No. 2 Lake Park',
  1727. },
  1728. ];
  1729. const onChange = sinon.spy(() => {
  1730. });
  1731. // test default descend
  1732. const targetData = sortData.reduce((a, b) => a.name.length > b.name.length ? a : b);
  1733. const {name} = targetData;
  1734. const sortTable = mount(<Table columns={sortColumns} dataSource={sortData} onChange={onChange}/>);
  1735. const firstCell = sortTable.find('.semi-table-tbody .semi-table-row .semi-table-row-cell').at(0);
  1736. expect(firstCell.text()).toBe(name);
  1737. // test default ascend
  1738. const ascendOrderColumns = [
  1739. {
  1740. title: 'Name',
  1741. dataIndex: 'name',
  1742. sorter: (a, b) => a.name.length - b.name.length,
  1743. defaultSortOrder: 'ascend',
  1744. },
  1745. {
  1746. title: 'Age',
  1747. dataIndex: 'age',
  1748. },
  1749. {
  1750. title: 'Address',
  1751. dataIndex: 'address',
  1752. }
  1753. ];
  1754. sortTable.setProps({columns: ascendOrderColumns})
  1755. sortTable.update()
  1756. const ascendTargetData = sortData.reduce((a, b) => a.name.length < b.name.length ? a : b);
  1757. const {name: ascendTargetName} = ascendTargetData;
  1758. const ascendFirstCell = sortTable.find('.semi-table-tbody .semi-table-row .semi-table-row-cell').at(0);
  1759. expect(ascendFirstCell.text()).toBe(ascendTargetName);
  1760. // test default false
  1761. const defaultOrderColumns = [
  1762. {
  1763. title: 'Name',
  1764. dataIndex: 'name',
  1765. sorter: (a, b) => a.name.length - b.name.length,
  1766. defaultSortOrder: false,
  1767. },
  1768. {
  1769. title: 'Age',
  1770. dataIndex: 'age',
  1771. },
  1772. {
  1773. title: 'Address',
  1774. dataIndex: 'address',
  1775. }
  1776. ]
  1777. sortTable.setProps({columns: defaultOrderColumns})
  1778. sortTable.update()
  1779. const defaultTargetData = sortData[0]
  1780. const {name: defaultTargetName} = defaultTargetData;
  1781. const defaultFirstCell = sortTable.find('.semi-table-tbody .semi-table-row .semi-table-row-cell').at(0);
  1782. expect(defaultFirstCell.text()).toBe(defaultTargetName);
  1783. // test click sorter
  1784. sortTable.find(`.${BASE_CLASS_PREFIX}-table-thead .${BASE_CLASS_PREFIX}-table-column-sorter`).at(0).simulate('click', {nativeEvent: null});
  1785. expect(onChange.callCount).toBe(1);
  1786. expect(onChange.getCall(0).args[0].sorter.sortOrder).toBe('ascend');
  1787. sortTable.find(`.${BASE_CLASS_PREFIX}-table-thead .${BASE_CLASS_PREFIX}-table-column-sorter`).at(0).simulate('click', {nativeEvent: null});
  1788. expect(onChange.callCount).toBe(2);
  1789. expect(onChange.getCall(1).args[0].sorter.sortOrder).toBe('descend');
  1790. // test change data
  1791. const newData = [
  1792. {
  1793. key: '1',
  1794. name: 'longest name',
  1795. age: 18,
  1796. address: 'New York No. 1 Lake Park',
  1797. },
  1798. {
  1799. key: '2',
  1800. name: 'long name',
  1801. age: 20,
  1802. address: 'London No. 1 Lake Park',
  1803. },
  1804. {
  1805. key: '3',
  1806. name: 'longer name',
  1807. age: 16,
  1808. address: 'Sidney No. 1 Lake Park',
  1809. },
  1810. {
  1811. key: '4',
  1812. name: 'short',
  1813. age: 33,
  1814. address: 'London No. 2 Lake Park',
  1815. },
  1816. ];
  1817. sortTable.setProps({dataSource: newData});
  1818. sortTable.update()
  1819. const newDescendTargetData = newData.reduce((a, b) => a.name.length > b.name.length ? a : b);
  1820. const {name: newDescendTargetName} = newDescendTargetData;
  1821. const newDataFirstCell = sortTable.find('.semi-table-tbody .semi-table-row .semi-table-row-cell').at(0);
  1822. expect(newDataFirstCell.text()).toBe(newDescendTargetName);
  1823. });
  1824. it(`test expandRowByClick`, async () => {
  1825. const onExpand = sinon.spy();
  1826. const onExpandedRowsChange = sinon.spy();
  1827. const demo = mount(
  1828. <Table
  1829. columns={columns}
  1830. dataSource={data}
  1831. onExpand={onExpand}
  1832. onExpandedRowsChange={onExpandedRowsChange}
  1833. expandedRowRender={() => <div>Semi Design</div>}
  1834. expandRowByClick
  1835. />
  1836. );
  1837. const table = demo.find(BaseTable);
  1838. const rows = demo.find(`.${BASE_CLASS_PREFIX}-table-tbody .${BASE_CLASS_PREFIX}-table-row`);
  1839. rows.at(0).simulate('click');
  1840. rows.at(1).simulate('click');
  1841. expect(onExpand.calledTwice).toBeTruthy();
  1842. expect(onExpandedRowsChange.calledTwice).toBeTruthy();
  1843. expect(onExpand.getCall(1).args[0]).toEqual(true);
  1844. expect(onExpand.getCall(1).args[1]).toEqual(data[1]);
  1845. expect(onExpand.getCall(1).args[2].constructor.name).toBe('SyntheticEvent');
  1846. expect(onExpandedRowsChange.getCall(1).args[0].length).toEqual(2);
  1847. expect(table.state(`expandedRowKeys`)).toEqual(['0', '1']);
  1848. const expandedRows = demo.find(`.${BASE_CLASS_PREFIX}-table-tbody .${BASE_CLASS_PREFIX}-table-row-expanded`);
  1849. expandedRows.at(0).simulate('click');
  1850. // 查看点击第二次是否折叠
  1851. const newExpandedRows = demo.find(`.${BASE_CLASS_PREFIX}-table-tbody .${BASE_CLASS_PREFIX}-table-row-expanded`)
  1852. expect(newExpandedRows.length).toEqual(1);
  1853. expect(table.state(`expandedRowKeys`)).toEqual(['1']);
  1854. });
  1855. });