1
0

table.test.js 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174
  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. 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 descend', 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. sortTable.unmount();
  1738. });
  1739. it('test defaultSortOrder descend', async () => {
  1740. const ascendOrderColumns = [
  1741. {
  1742. title: 'Name',
  1743. dataIndex: 'name',
  1744. sorter: (a, b) => a.name.length - b.name.length,
  1745. defaultSortOrder: 'ascend',
  1746. },
  1747. {
  1748. title: 'Age',
  1749. dataIndex: 'age',
  1750. },
  1751. {
  1752. title: 'Address',
  1753. dataIndex: 'address',
  1754. }
  1755. ];
  1756. const sortData = [
  1757. {
  1758. key: '1',
  1759. name: 'long name',
  1760. age: 32,
  1761. address: 'New York No. 1 Lake Park',
  1762. },
  1763. {
  1764. key: '2',
  1765. name: 'longest name',
  1766. age: 42,
  1767. address: 'London No. 1 Lake Park',
  1768. },
  1769. {
  1770. key: '3',
  1771. name: 'longer name',
  1772. age: 32,
  1773. address: 'Sidney No. 1 Lake Park',
  1774. },
  1775. {
  1776. key: '4',
  1777. name: 'short',
  1778. age: 32,
  1779. address: 'London No. 2 Lake Park',
  1780. },
  1781. ];
  1782. const onChange = sinon.spy(() => {
  1783. });
  1784. const sortTable = mount(<Table columns={ascendOrderColumns} dataSource={sortData} onChange={onChange}/>);
  1785. const ascendTargetData = sortData.reduce((a, b) => a.name.length < b.name.length ? a : b);
  1786. const {name: ascendTargetName} = ascendTargetData;
  1787. const ascendFirstCell = sortTable.find('.semi-table-tbody .semi-table-row .semi-table-row-cell').at(0);
  1788. expect(ascendFirstCell.text()).toBe(ascendTargetName);
  1789. sortTable.unmount();
  1790. });
  1791. it('test defaultSortOrder false', async () => {
  1792. const sortColumns = [
  1793. {
  1794. title: 'Name',
  1795. dataIndex: 'name',
  1796. sorter: (a, b) => a.name.length - b.name.length,
  1797. defaultSortOrder: false,
  1798. },
  1799. {
  1800. title: 'Age',
  1801. dataIndex: 'age',
  1802. },
  1803. {
  1804. title: 'Address',
  1805. dataIndex: 'address',
  1806. }
  1807. ];
  1808. const sortData = [
  1809. {
  1810. key: '1',
  1811. name: 'long name',
  1812. age: 32,
  1813. address: 'New York No. 1 Lake Park',
  1814. },
  1815. {
  1816. key: '2',
  1817. name: 'longest name',
  1818. age: 42,
  1819. address: 'London No. 1 Lake Park',
  1820. },
  1821. {
  1822. key: '3',
  1823. name: 'longer name',
  1824. age: 32,
  1825. address: 'Sidney No. 1 Lake Park',
  1826. },
  1827. {
  1828. key: '4',
  1829. name: 'short',
  1830. age: 32,
  1831. address: 'London No. 2 Lake Park',
  1832. },
  1833. ];
  1834. const onChange = sinon.spy(() => {
  1835. });
  1836. const sortTable = mount(<Table columns={sortColumns} dataSource={sortData} onChange={onChange}/>);
  1837. // test default false
  1838. const defaultTargetData = sortData[0]
  1839. const {name: defaultTargetName} = defaultTargetData;
  1840. const defaultFirstCell = sortTable.find('.semi-table-tbody .semi-table-row .semi-table-row-cell').at(0);
  1841. expect(defaultFirstCell.text()).toBe(defaultTargetName);
  1842. // test click sorter
  1843. sortTable.find(`.${BASE_CLASS_PREFIX}-table-thead .${BASE_CLASS_PREFIX}-table-column-sorter`).at(0).simulate('click', {nativeEvent: null});
  1844. expect(onChange.callCount).toBe(1);
  1845. expect(onChange.getCall(0).args[0].sorter.sortOrder).toBe('ascend');
  1846. sortTable.find(`.${BASE_CLASS_PREFIX}-table-thead .${BASE_CLASS_PREFIX}-table-column-sorter`).at(0).simulate('click', {nativeEvent: null});
  1847. expect(onChange.callCount).toBe(2);
  1848. expect(onChange.getCall(1).args[0].sorter.sortOrder).toBe('descend');
  1849. // test change data
  1850. const newData = [
  1851. {
  1852. key: '1',
  1853. name: 'longest name',
  1854. age: 18,
  1855. address: 'New York No. 1 Lake Park',
  1856. },
  1857. {
  1858. key: '2',
  1859. name: 'long name',
  1860. age: 20,
  1861. address: 'London No. 1 Lake Park',
  1862. },
  1863. {
  1864. key: '3',
  1865. name: 'longer name',
  1866. age: 16,
  1867. address: 'Sidney No. 1 Lake Park',
  1868. },
  1869. {
  1870. key: '4',
  1871. name: 'short',
  1872. age: 33,
  1873. address: 'London No. 2 Lake Park',
  1874. },
  1875. ];
  1876. sortTable.setProps({dataSource: newData});
  1877. sortTable.update()
  1878. const newDescendTargetData = newData.reduce((a, b) => a.name.length > b.name.length ? a : b);
  1879. const {name: newDescendTargetName} = newDescendTargetData;
  1880. const newDataFirstCell = sortTable.find('.semi-table-tbody .semi-table-row .semi-table-row-cell').at(0);
  1881. expect(newDataFirstCell.text()).toBe(newDescendTargetName);
  1882. sortTable.unmount();
  1883. });
  1884. it(`test expandRowByClick`, async () => {
  1885. const onExpand = sinon.spy();
  1886. const onExpandedRowsChange = sinon.spy();
  1887. const demo = mount(
  1888. <Table
  1889. columns={columns}
  1890. dataSource={data}
  1891. onExpand={onExpand}
  1892. onExpandedRowsChange={onExpandedRowsChange}
  1893. expandedRowRender={() => <div>Semi Design</div>}
  1894. expandRowByClick
  1895. />
  1896. );
  1897. const table = demo.find(BaseTable);
  1898. const rows = demo.find(`.${BASE_CLASS_PREFIX}-table-tbody .${BASE_CLASS_PREFIX}-table-row`);
  1899. rows.at(0).simulate('click');
  1900. rows.at(1).simulate('click');
  1901. expect(onExpand.calledTwice).toBeTruthy();
  1902. expect(onExpandedRowsChange.calledTwice).toBeTruthy();
  1903. expect(onExpand.getCall(1).args[0]).toEqual(true);
  1904. expect(onExpand.getCall(1).args[1]).toEqual(data[1]);
  1905. expect(onExpand.getCall(1).args[2].constructor.name).toBe('SyntheticEvent');
  1906. expect(onExpandedRowsChange.getCall(1).args[0].length).toEqual(2);
  1907. expect(table.state(`expandedRowKeys`)).toEqual(['0', '1']);
  1908. const expandedRows = demo.find(`.${BASE_CLASS_PREFIX}-table-tbody .${BASE_CLASS_PREFIX}-table-row-expanded`);
  1909. expandedRows.at(0).simulate('click');
  1910. // 查看点击第二次是否折叠
  1911. const newExpandedRows = demo.find(`.${BASE_CLASS_PREFIX}-table-tbody .${BASE_CLASS_PREFIX}-table-row-expanded`)
  1912. expect(newExpandedRows.length).toEqual(1);
  1913. expect(table.state(`expandedRowKeys`)).toEqual(['1']);
  1914. });
  1915. it(`test expanded row re-render`, () => {
  1916. const expandedRowRender = sinon.spy(() => <div>Semi Design</div>);
  1917. const demo = mount(
  1918. <Table
  1919. columns={columns}
  1920. dataSource={data}
  1921. expandedRowRender={expandedRowRender}
  1922. />
  1923. );
  1924. const table = demo.find(BaseTable);
  1925. const expandIcons = demo.find(`.semi-table-tbody .semi-table-row .semi-table-expand-icon`);
  1926. expandIcons.at(0).simulate('click');
  1927. expandIcons.at(1).simulate('click');
  1928. expect(expandedRowRender.calledTwice).toBeTruthy();
  1929. });
  1930. it('test defaultFilteredValue is in onChange when click sorter', () => {
  1931. const columns = [
  1932. {
  1933. title: '标题',
  1934. dataIndex: 'name',
  1935. width: 400,
  1936. render: (text, record, index) => {
  1937. return (
  1938. <div>
  1939. {text}
  1940. </div>
  1941. );
  1942. },
  1943. filters: [
  1944. {
  1945. text: 'Semi Design 设计稿',
  1946. value: 'Semi Design 设计稿',
  1947. },
  1948. {
  1949. text: 'Semi Pro 设计稿',
  1950. value: 'Semi Pro 设计稿',
  1951. },
  1952. ],
  1953. onFilter: (value, record) => record.name.includes(value),
  1954. defaultFilteredValue: ['Semi Pro 设计稿'],
  1955. },
  1956. {
  1957. title: '大小',
  1958. dataIndex: 'size',
  1959. sorter: (a, b) => a.size - b.size > 0 ? 1 : -1,
  1960. defaultSortOrder: 'ascend',
  1961. render: (text) => `${text} KB`
  1962. },
  1963. {
  1964. title: '所有者',
  1965. dataIndex: 'owner',
  1966. render: (text, record, index) => {
  1967. return (
  1968. <div>
  1969. {text}
  1970. </div>
  1971. );
  1972. }
  1973. }
  1974. ];
  1975. const getData = (total) => {
  1976. const data = [];
  1977. for (let i = 0; i < total; i++) {
  1978. const isSemiDesign = i % 2 === 0;
  1979. const randomNumber = (i * 1000) % 199;
  1980. data.push({
  1981. key: '' + i,
  1982. name: isSemiDesign ? `Semi Design 设计稿${i}.fig` : `Semi Pro 设计稿${i}.fig`,
  1983. owner: isSemiDesign ? '姜鹏志' : '郝宣',
  1984. size: randomNumber,
  1985. avatarBg: isSemiDesign ? 'grey' : 'red'
  1986. });
  1987. }
  1988. return data;
  1989. };
  1990. const data = getData(25);
  1991. const onChange = sinon.spy(() => {
  1992. });
  1993. const tableNode = mount(<Table columns={columns} dataSource={data} onChange={onChange}/>);
  1994. tableNode.find('.semi-table-column-sorter').simulate('click');
  1995. expect(onChange.calledOnce).toBe(true);
  1996. const arg = onChange.getCall(0).args[0];
  1997. expect(arg.filters.length).toBe(1);
  1998. expect(arg.filters[0].defaultFilteredValue).toEqual(['Semi Pro 设计稿']);
  1999. expect(arg.filters[0].filteredValue).toEqual(['Semi Pro 设计稿']);
  2000. tableNode.unmount();
  2001. });
  2002. it('test defaultSortOrder is in onChange when click filter', () => {
  2003. const defaultSortOrder = 'ascend';
  2004. const columns = [
  2005. {
  2006. title: '标题',
  2007. dataIndex: 'name',
  2008. width: 400,
  2009. render: (text, record, index) => {
  2010. return (
  2011. <div>
  2012. {text}
  2013. </div>
  2014. );
  2015. },
  2016. filters: [
  2017. {
  2018. text: 'Semi Design 设计稿',
  2019. value: 'Semi Design 设计稿',
  2020. },
  2021. {
  2022. text: 'Semi Pro 设计稿',
  2023. value: 'Semi Pro 设计稿',
  2024. },
  2025. ],
  2026. onFilter: (value, record) => record.name.includes(value),
  2027. defaultFilteredValue: ['Semi Pro 设计稿'],
  2028. },
  2029. {
  2030. title: '大小',
  2031. dataIndex: 'size',
  2032. sorter: (a, b) => a.size - b.size > 0 ? 1 : -1,
  2033. defaultSortOrder: 'ascend',
  2034. render: (text) => `${text} KB`
  2035. },
  2036. {
  2037. title: '所有者',
  2038. dataIndex: 'owner',
  2039. render: (text, record, index) => {
  2040. return (
  2041. <div>
  2042. {text}
  2043. </div>
  2044. );
  2045. }
  2046. }
  2047. ];
  2048. const getData = (total) => {
  2049. const data = [];
  2050. for (let i = 0; i < total; i++) {
  2051. const isSemiDesign = i % 2 === 0;
  2052. const randomNumber = (i * 1000) % 199;
  2053. data.push({
  2054. key: '' + i,
  2055. name: isSemiDesign ? `Semi Design 设计稿${i}.fig` : `Semi Pro 设计稿${i}.fig`,
  2056. owner: isSemiDesign ? '姜鹏志' : '郝宣',
  2057. size: randomNumber,
  2058. avatarBg: isSemiDesign ? 'grey' : 'red'
  2059. });
  2060. }
  2061. return data;
  2062. };
  2063. const data = getData(25);
  2064. const onChange = sinon.spy(() => {
  2065. });
  2066. const tableNode = mount(<Table columns={columns} dataSource={data} onChange={onChange}/>);
  2067. tableNode.find('.semi-table-column-filter').simulate('click');
  2068. const filterNode = Array.from(document.querySelectorAll('.semi-checkbox-addon')).filter(node => node.textContent === 'Semi Design 设计稿');
  2069. filterNode[0].click();
  2070. expect(onChange.calledOnce).toBe(true);
  2071. const arg = onChange.getCall(0).args[0];
  2072. expect(arg.sorter.defaultSortOrder).toBe(defaultSortOrder);
  2073. expect(arg.sorter.sortOrder).toBe(defaultSortOrder);
  2074. tableNode.unmount();
  2075. });
  2076. });