-
-
Notifications
You must be signed in to change notification settings - Fork 604
/
Copy pathScroll.spec.jsx
127 lines (111 loc) · 3.62 KB
/
Scroll.spec.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import { mount } from 'enzyme';
import { spyElementPrototypes } from 'rc-util/lib/test/domHook';
import React from 'react';
import { act } from 'react-dom/test-utils';
import Table from '../src';
describe('Table.Scroll', () => {
const data = [
{ key: 'key0', name: 'Lucy' },
{ key: 'key1', name: 'Jack' },
];
const createTable = props => {
const columns = [{ title: 'Name', dataIndex: 'name', key: 'name' }];
return <Table columns={columns} data={data} {...props} />;
};
it('renders scroll.x is true', () => {
const wrapper = mount(createTable({ scroll: { x: true } }));
expect(wrapper.find('table').props().style.width).toEqual('auto');
expect(wrapper.find('.rc-table-content').props().style.overflowX).toEqual('auto');
expect(wrapper.find('.rc-table-content').props().style.overflowY).toEqual('hidden');
});
it('renders scroll.x is a number', () => {
const wrapper = mount(createTable({ scroll: { x: 200 } }));
expect(wrapper.find('table').props().style.width).toEqual(200);
});
it('renders scroll.y is a number', () => {
const wrapper = mount(createTable({ scroll: { y: 200 } }));
expect(wrapper.find('.rc-table-body').props().style.maxHeight).toEqual(200);
});
it('renders scroll.x and scroll.y are both true', () => {
const wrapper = mount(createTable({ scroll: { x: true, y: 200 } }));
expect(wrapper.find('.rc-table-body').props().style.overflowX).toEqual('auto');
expect(wrapper.find('.rc-table-body').props().style.overflowY).toEqual('auto');
});
it('fire scroll event', () => {
vi.useFakeTimers();
let scrollLeft = 0;
let scrollTop = 0;
const setScrollLeft = vi.fn((_, val) => {
scrollLeft = val;
});
const setScrollTop = vi.fn((_, val) => {
scrollTop = val;
});
const domSpy = spyElementPrototypes(HTMLDivElement, {
scrollLeft: {
get: () => scrollLeft,
set: setScrollLeft,
},
scrollTop: {
get: () => scrollTop,
set: setScrollTop,
},
scrollWidth: {
get: () => 200,
},
clientWidth: {
get: () => 100,
},
});
const newColumns = [
{ title: 'title1', dataIndex: 'a', key: 'a', width: 100, fixed: 'left' },
{ title: 'title2', dataIndex: 'b', key: 'b' },
{ title: 'title3', dataIndex: 'c', key: 'c' },
{ title: 'title4', dataIndex: 'd', key: 'd', width: 100, fixed: 'right' },
];
const newData = [
{ a: '123', b: 'xxxxxxxx', c: 3, d: 'hehe', key: '1' },
{ a: 'cdd', b: 'edd12221', c: 3, d: 'haha', key: '2' },
];
const wrapper = mount(
<Table
columns={newColumns}
data={newData}
scroll={{
x: 200,
y: 200,
}}
/>,
);
vi.runAllTimers();
// Use `onScroll` directly since simulate not support `currentTarget`
act(() => {
const headerDiv = wrapper.find('div.rc-table-header').instance();
const wheelEvent = new WheelEvent('wheel');
Object.defineProperty(wheelEvent, 'deltaX', {
get: () => 10,
});
headerDiv.dispatchEvent(wheelEvent);
vi.runAllTimers();
});
expect(setScrollLeft).toHaveBeenCalledWith(undefined, 10);
setScrollLeft.mockReset();
act(() => {
wrapper
.find('.rc-table-body')
.props()
.onScroll({
currentTarget: {
scrollLeft: 33,
scrollWidth: 200,
clientWidth: 100,
},
});
});
vi.runAllTimers();
expect(setScrollLeft).toHaveBeenCalledWith(undefined, 33);
setScrollLeft.mockReset();
domSpy.mockRestore();
vi.useRealTimers();
});
});