|
1 | 1 | import { mount } from '@vue/test-utils'; |
2 | 2 | import DTable from '../src/table'; |
3 | 3 | import DColumn from '../src/components/column/column'; |
| 4 | +import { Button } from '../../button'; |
4 | 5 | import { useNamespace } from '../../shared/hooks/use-namespace'; |
5 | 6 | import { nextTick, ref } from 'vue'; |
6 | 7 |
|
7 | 8 | let data: Array<Record<string, any>> = []; |
8 | 9 | const ns = useNamespace('table', true); |
9 | 10 | const noDotNs = useNamespace('table'); |
10 | 11 | const flexibleOverlayNs = useNamespace('flexible-overlay', true); |
11 | | -const tooltipNs = useNamespace('tooltip', true); |
12 | 12 |
|
13 | 13 | describe('d-table', () => { |
14 | 14 | beforeEach(() => { |
@@ -278,7 +278,7 @@ describe('d-table', () => { |
278 | 278 | const lastTd = tableBody.find('tr').findAll('td')[3]; |
279 | 279 | expect(lastTd.text()).toBe('1990/01/11'); |
280 | 280 |
|
281 | | - const sortIcon = lastTh.find('.sort-clickable'); |
| 281 | + const sortIcon = lastTh.find(ns.e('sort-clickable')); |
282 | 282 | await sortIcon.trigger('click'); |
283 | 283 | expect(lastTd.text()).toBe('1990/01/14'); |
284 | 284 | expect(handleSortChange).toBeCalled(); |
@@ -417,4 +417,74 @@ describe('d-table', () => { |
417 | 417 | expect(wrapper.find(ns.e('thead')).exists()).toBeFalsy(); |
418 | 418 | wrapper.unmount(); |
419 | 419 | }); |
| 420 | + |
| 421 | + it('table method test work', async () => { |
| 422 | + const wrapper = mount({ |
| 423 | + setup() { |
| 424 | + const tableRef = ref(); |
| 425 | + const checkedRows = ref([]); |
| 426 | + const getCheckedRow = () => { |
| 427 | + checkedRows.value = tableRef.value.store.getCheckedRows(); |
| 428 | + }; |
| 429 | + const toggleRowSelection = () => { |
| 430 | + tableRef.value.store.toggleRowSelection(data[1]); |
| 431 | + checkedRows.value = tableRef.value.store.getCheckedRows(); |
| 432 | + }; |
| 433 | + const toggleRowExpansion = () => { |
| 434 | + tableRef.value.store.toggleRowExpansion(data[2]); |
| 435 | + }; |
| 436 | + const checkable = (row) => { |
| 437 | + return row.lastName.endsWith('n'); |
| 438 | + }; |
| 439 | + return () => ( |
| 440 | + <div> |
| 441 | + <Button onClick={getCheckedRow} class="mr-m mb-m"> |
| 442 | + Get CheckedRows |
| 443 | + </Button> |
| 444 | + <Button onClick={toggleRowSelection} class="mr-m mb-m"> |
| 445 | + toggleRowSelection |
| 446 | + </Button> |
| 447 | + <Button onClick={toggleRowExpansion} class="mr-m mb-m"> |
| 448 | + toggleRowExpansion |
| 449 | + </Button> |
| 450 | + <DTable ref={tableRef} data={data} row-key="firstName" trackBy={(item) => item.firstName} expand-row-keys={['Jacob', 'Mark']}> |
| 451 | + <DColumn type="expand"></DColumn> |
| 452 | + <DColumn type="checkable" width="40" checkable={checkable} reserve-check></DColumn> |
| 453 | + <DColumn field="firstName" header="First Name"></DColumn> |
| 454 | + <DColumn field="lastName" header="Last Name"></DColumn> |
| 455 | + <DColumn field="gender" header="Gender"></DColumn> |
| 456 | + <DColumn field="date" header="Date of birth"></DColumn> |
| 457 | + </DTable> |
| 458 | + <p>{checkedRows.value.length}</p> |
| 459 | + </div> |
| 460 | + ); |
| 461 | + }, |
| 462 | + }); |
| 463 | + |
| 464 | + await nextTick(); |
| 465 | + |
| 466 | + // getCheckedRows |
| 467 | + const p = wrapper.find('p'); |
| 468 | + expect(p.element.textContent).toBe('0'); |
| 469 | + const buttons = wrapper.findAll('button'); |
| 470 | + await buttons[0].trigger('click'); |
| 471 | + await nextTick(); |
| 472 | + expect(p.element.textContent).toBe('2'); |
| 473 | + |
| 474 | + // toggleRowSelection |
| 475 | + await buttons[1].trigger('click'); |
| 476 | + expect(p.element.textContent).toBe('3'); |
| 477 | + await buttons[1].trigger('click'); |
| 478 | + expect(p.element.textContent).toBe('2'); |
| 479 | + |
| 480 | + // toggleRowExpansion |
| 481 | + let expandedList = wrapper.findAll('.expanded'); |
| 482 | + expect(expandedList.length).toBe(2); |
| 483 | + await buttons[2].trigger('click'); |
| 484 | + expandedList = wrapper.findAll('.expanded'); |
| 485 | + expect(expandedList.length).toBe(3); |
| 486 | + await buttons[2].trigger('click'); |
| 487 | + expandedList = wrapper.findAll('.expanded'); |
| 488 | + expect(expandedList.length).toBe(2); |
| 489 | + }); |
420 | 490 | }); |
0 commit comments