table.test.js 75 KB

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