Skip to content

Commit e4b3fe5

Browse files
committed
添加滚动效果单元测试
1 parent c05b304 commit e4b3fe5

File tree

3 files changed

+192
-4
lines changed

3 files changed

+192
-4
lines changed

tests/unit/specs/ve-table-column-fixed.spec.js

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,21 @@ describe("veTable column fixed", () => {
184184
]),
185185
);
186186

187-
//await wrapper.setProps({ columns: COLUMNS_FIXED_CHANGE });
187+
await wrapper.setProps({ columns: COLUMNS_FIXED_CHANGE });
188+
189+
const thEls2 = wrapper.findAll(
190+
".ve-table-header .ve-table-header-tr .ve-table-header-th",
191+
);
192+
193+
expect(thEls2.at(0).classes()).toEqual(
194+
expect.arrayContaining([
195+
"ve-table-fixed-left",
196+
"ve-table-last-left-fixed-column",
197+
]),
198+
);
188199
});
189200

190-
it("header fixed right", () => {
201+
it("header fixed right", async () => {
191202
const wrapper = mount(veTable, {
192203
propsData: {
193204
columns: COLUMNS,
@@ -206,9 +217,22 @@ describe("veTable column fixed", () => {
206217
]),
207218
);
208219
expect(thEls.at(9 - 0).classes()).toContain("ve-table-fixed-right");
220+
221+
await wrapper.setProps({ columns: COLUMNS_FIXED_CHANGE });
222+
223+
const thEls2 = wrapper.findAll(
224+
".ve-table-header .ve-table-header-tr .ve-table-header-th",
225+
);
226+
227+
expect(thEls2.at(9 - 0).classes()).toEqual(
228+
expect.arrayContaining([
229+
"ve-table-first-right-fixed-column",
230+
"ve-table-fixed-right",
231+
]),
232+
);
209233
});
210234

211-
it("column fixed left", () => {
235+
it("column fixed left", async () => {
212236
const wrapper = mount(veTable, {
213237
propsData: {
214238
columns: COLUMNS,
@@ -228,9 +252,23 @@ describe("veTable column fixed", () => {
228252
"ve-table-fixed-left",
229253
]),
230254
);
255+
256+
await wrapper.setProps({ columns: COLUMNS_FIXED_CHANGE });
257+
258+
const tdEls2 = wrapper
259+
.findAll(".ve-table-body .ve-table-body-tr")
260+
.at(0)
261+
.findAll(".ve-table-body-td");
262+
263+
expect(tdEls2.at(0).classes()).toEqual(
264+
expect.arrayContaining([
265+
"ve-table-last-left-fixed-column",
266+
"ve-table-fixed-left",
267+
]),
268+
);
231269
});
232270

233-
it("column fixed right", () => {
271+
it("column fixed right", async () => {
234272
const wrapper = mount(veTable, {
235273
propsData: {
236274
columns: COLUMNS,
@@ -250,5 +288,19 @@ describe("veTable column fixed", () => {
250288
]),
251289
);
252290
expect(tdEls.at(9 - 0).classes()).toContain("ve-table-fixed-right");
291+
292+
await wrapper.setProps({ columns: COLUMNS_FIXED_CHANGE });
293+
294+
const tdEls2 = wrapper
295+
.findAll(".ve-table-body .ve-table-body-tr")
296+
.at(0)
297+
.findAll(".ve-table-body-td");
298+
299+
expect(tdEls.at(9 - 0).classes()).toEqual(
300+
expect.arrayContaining([
301+
"ve-table-first-right-fixed-column",
302+
"ve-table-fixed-right",
303+
]),
304+
);
253305
});
254306
});
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
表格通用单元测试
3+
*/
4+
5+
import { mount } from "@vue/test-utils";
6+
import veTable from "@/ve-table";
7+
import {
8+
later,
9+
mockElementMeasurement,
10+
clearMockElementMeasurement,
11+
} from "../util";
12+
13+
describe("veTable common", () => {
14+
afterEach(() => {
15+
clearMockElementMeasurement("scrollWidth");
16+
clearMockElementMeasurement("clientWidth");
17+
});
18+
19+
it("horizontal scroll state", async () => {
20+
mockElementMeasurement("scrollWidth", 1200);
21+
mockElementMeasurement("clientWidth", 900);
22+
23+
const COLUMNS = [
24+
{ field: "col1", key: "a", title: "Title1" },
25+
{ field: "col2", key: "b", title: "Title2" },
26+
{ field: "col3", key: "c", title: "Title3" },
27+
{ field: "col4", key: "d", title: "Title4" },
28+
{ field: "col5", key: "e", title: "Title5" },
29+
{ field: "col6", key: "f", title: "Title6" },
30+
{ field: "col7", key: "g", title: "Title7" },
31+
{ field: "col8", key: "h", title: "Title8" },
32+
{ field: "col9", key: "i", title: "Title9" },
33+
{ field: "col10", key: "j", title: "Title10" },
34+
];
35+
36+
const COLUMNS2 = [
37+
{ field: "col1", key: "a", title: "Title1", fixed: "left" },
38+
{ field: "col2", key: "b", title: "Title2", fixed: "left" },
39+
{ field: "col3", key: "c", title: "Title3" },
40+
{ field: "col4", key: "d", title: "Title4" },
41+
{ field: "col5", key: "e", title: "Title5" },
42+
{ field: "col6", key: "f", title: "Title6" },
43+
{ field: "col7", key: "g", title: "Title7" },
44+
{ field: "col8", key: "h", title: "Title8" },
45+
{ field: "col9", key: "i", title: "Title9" },
46+
{ field: "col10", key: "j", title: "Title10", fixed: "right" },
47+
];
48+
49+
const TABLE_DATA = [
50+
{
51+
col1: "1",
52+
col2: "2",
53+
col3: "3",
54+
col4: "4",
55+
col5: "5",
56+
col6: "6",
57+
col7: "7",
58+
col8: "8",
59+
col9: "9",
60+
col10: "10",
61+
},
62+
];
63+
64+
const ParentComp = {
65+
template: `
66+
<veTable
67+
style="width:900px"
68+
:scrollWidth="1200"
69+
:columns="columns"
70+
:tableData="tableData"
71+
/>
72+
`,
73+
props: {
74+
columns: {
75+
type: Array,
76+
required: true,
77+
},
78+
},
79+
data() {
80+
return {
81+
tableData: TABLE_DATA,
82+
};
83+
},
84+
components: {
85+
veTable,
86+
},
87+
};
88+
89+
await later();
90+
91+
const parentWrapper = mount(ParentComp, {
92+
propsData: {
93+
columns: COLUMNS,
94+
},
95+
});
96+
97+
const wrapper = parentWrapper.findComponent(veTable);
98+
99+
expect(wrapper.vm.isRightScrolling).toBe(false);
100+
101+
// 只能通过父组件设置
102+
await parentWrapper.setProps({ columns: COLUMNS2 });
103+
expect(wrapper.vm.isRightScrolling).toBe(true);
104+
});
105+
});

tests/unit/util.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,34 @@ export function mockScrollTo() {
2323
Element.prototype.scrollTo = fn;
2424
return fn;
2525
}
26+
27+
/*
28+
* @mockElementMeasurement
29+
* @desc mock element measurement
30+
*/
31+
export function mockElementMeasurement(key, value) {
32+
Object.defineProperty(HTMLElement.prototype, key, {
33+
configurable: true,
34+
value: value,
35+
});
36+
}
37+
38+
/*
39+
* @clearMockElementMeasurement
40+
* @desc clear mock element measurement
41+
*/
42+
export function clearMockElementMeasurement(key) {
43+
// const originalValue = Object.getOwnPropertyDescriptor(
44+
// HTMLElement.prototype,
45+
// key,
46+
// );
47+
48+
const originalValue = {
49+
value: 1200,
50+
writable: false,
51+
enumerable: false,
52+
configurable: true,
53+
};
54+
55+
Object.defineProperty(HTMLElement.prototype, key, originalValue);
56+
}

0 commit comments

Comments
 (0)