Skip to content

Commit b5c7129

Browse files
committed
完善单元测试
1 parent 0f3e597 commit b5c7129

File tree

1 file changed

+108
-3
lines changed

1 file changed

+108
-3
lines changed

tests/unit/specs/ve-table-virtual-scroll.spec.js

Lines changed: 108 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { mount } from "@vue/test-utils";
22
import veTable from "@/ve-table";
3-
import { later } from "../util";
3+
import { later, mockScrollTo } from "../util";
44

55
describe("veTable virtual scroll", () => {
66
// same row height
@@ -44,9 +44,14 @@ describe("veTable virtual scroll", () => {
4444
//
4545
const MAX_HEIGHT = 500;
4646
const MIN_ROW_HEIGHT = 40;
47+
48+
// 表格渲染的数量
4749
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+
}
5055

5156
it("render same row height", async () => {
5257
const wrapper = mount(veTable, {
@@ -543,4 +548,104 @@ describe("veTable virtual scroll", () => {
543548
TABLE_ROW_COUNT,
544549
);
545550
});
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+
});
546651
});

0 commit comments

Comments
 (0)