|
1 | 1 | import { mount } from "@vue/test-utils"; |
2 | 2 | import veTable from "@/ve-table"; |
3 | | -import { later } from "../util"; |
| 3 | +import { later, mockScrollTo } from "../util"; |
4 | 4 |
|
5 | 5 | describe("veTable virtual scroll", () => { |
6 | 6 | // same row height |
@@ -44,9 +44,14 @@ describe("veTable virtual scroll", () => { |
44 | 44 | // |
45 | 45 | const MAX_HEIGHT = 500; |
46 | 46 | const MIN_ROW_HEIGHT = 40; |
| 47 | + |
| 48 | + // 表格渲染的数量 |
47 | 49 | const TABLE_ROW_COUNT = Math.ceil(MAX_HEIGHT / MIN_ROW_HEIGHT) + 1; |
48 | | - // const MIN_TABLE_ROW_COUNT = Math.floor(MAX_HEIGHT / MIN_ROW_HEIGHT); |
49 | | - // const MAX_TABLE_ROW_COUNT = Math.ceil(MAX_HEIGHT / MIN_ROW_HEIGHT) + 2; |
| 50 | + |
| 51 | + // get table rendered row count by row height |
| 52 | + function getTableRenderedRowCountByRowHeight(rowHeight) { |
| 53 | + return Math.ceil(MAX_HEIGHT / rowHeight) + 1; |
| 54 | + } |
50 | 55 |
|
51 | 56 | it("render same row height", async () => { |
52 | 57 | const wrapper = mount(veTable, { |
@@ -543,4 +548,104 @@ describe("veTable virtual scroll", () => { |
543 | 548 | TABLE_ROW_COUNT, |
544 | 549 | ); |
545 | 550 | }); |
| 551 | + |
| 552 | + it("scrolling callback", async () => { |
| 553 | + const mockFn = jest.fn(); |
| 554 | + const wrapper = mount(veTable, { |
| 555 | + propsData: { |
| 556 | + columns: [ |
| 557 | + { |
| 558 | + field: "name", |
| 559 | + key: "b", |
| 560 | + title: "Name", |
| 561 | + width: 200, |
| 562 | + align: "left", |
| 563 | + }, |
| 564 | + { |
| 565 | + field: "hobby", |
| 566 | + key: "c", |
| 567 | + title: "Hobby", |
| 568 | + width: 300, |
| 569 | + align: "left", |
| 570 | + }, |
| 571 | + { |
| 572 | + field: "address", |
| 573 | + key: "d", |
| 574 | + title: "Address", |
| 575 | + width: "", |
| 576 | + align: "left", |
| 577 | + }, |
| 578 | + ], |
| 579 | + tableData: TABLE_DATA_SAME_ROW_HEIGHT, |
| 580 | + virtualScrollOption: { |
| 581 | + // 是否开启 |
| 582 | + enable: true, |
| 583 | + scrolling: mockFn, |
| 584 | + }, |
| 585 | + maxHeight: MAX_HEIGHT, |
| 586 | + rowKeyFieldName: "rowKey", |
| 587 | + }, |
| 588 | + }); |
| 589 | + |
| 590 | + wrapper.triggerResizeObserver({ width: MAX_HEIGHT }); |
| 591 | + |
| 592 | + await later(); |
| 593 | + |
| 594 | + expect(mockFn).toHaveBeenCalledTimes(1); |
| 595 | + |
| 596 | + expect(mockFn).toHaveBeenCalledWith({ |
| 597 | + startRowIndex: 0, |
| 598 | + visibleStartIndex: 0, |
| 599 | + visibleEndIndex: TABLE_ROW_COUNT - 1, // ? |
| 600 | + visibleAboveCount: 0, |
| 601 | + visibleBelowCount: 1, |
| 602 | + }); |
| 603 | + }); |
| 604 | + |
| 605 | + it("minRowHeight prop", async () => { |
| 606 | + const mockFn = jest.fn(); |
| 607 | + const minRowHeight = 50; |
| 608 | + const wrapper = mount(veTable, { |
| 609 | + propsData: { |
| 610 | + columns: [ |
| 611 | + { |
| 612 | + field: "name", |
| 613 | + key: "b", |
| 614 | + title: "Name", |
| 615 | + width: 200, |
| 616 | + align: "left", |
| 617 | + }, |
| 618 | + { |
| 619 | + field: "hobby", |
| 620 | + key: "c", |
| 621 | + title: "Hobby", |
| 622 | + width: 300, |
| 623 | + align: "left", |
| 624 | + }, |
| 625 | + { |
| 626 | + field: "address", |
| 627 | + key: "d", |
| 628 | + title: "Address", |
| 629 | + width: "", |
| 630 | + align: "left", |
| 631 | + }, |
| 632 | + ], |
| 633 | + tableData: TABLE_DATA_SAME_ROW_HEIGHT, |
| 634 | + virtualScrollOption: { |
| 635 | + // 是否开启 |
| 636 | + enable: true, |
| 637 | + minRowHeight: minRowHeight, |
| 638 | + }, |
| 639 | + maxHeight: MAX_HEIGHT, |
| 640 | + rowKeyFieldName: "rowKey", |
| 641 | + }, |
| 642 | + }); |
| 643 | + |
| 644 | + wrapper.triggerResizeObserver({ width: MAX_HEIGHT }); |
| 645 | + |
| 646 | + await later(); |
| 647 | + |
| 648 | + const rowCount = getTableRenderedRowCountByRowHeight(minRowHeight); |
| 649 | + expect(wrapper.findAll(".ve-table-body-tr").length).toBe(rowCount); |
| 650 | + }); |
546 | 651 | }); |
0 commit comments