table.test.js 68 KB

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