-
Notifications
You must be signed in to change notification settings - Fork 169
Expand file tree
/
Copy pathscroll-Firefox.test.js
More file actions
144 lines (116 loc) · 4.02 KB
/
scroll-Firefox.test.js
File metadata and controls
144 lines (116 loc) · 4.02 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import { act } from '@testing-library/react';
import { mount } from 'enzyme';
import React from 'react';
import List from '../src';
import isFF from '../src/utils/isFirefox';
import { spyElementPrototypes } from './utils/domHook';
function genData(count) {
return new Array(count).fill(null).map((_, index) => ({ id: String(index) }));
}
jest.mock('../src/utils/isFirefox', () => true);
describe('List.Firefox-Scroll', () => {
let mockElement;
beforeAll(() => {
mockElement = spyElementPrototypes(HTMLElement, {
offsetHeight: {
get: () => 20,
},
clientHeight: {
get: () => 100,
},
});
});
afterAll(() => {
mockElement.mockRestore();
});
beforeEach(() => {
jest.useFakeTimers();
});
afterEach(() => {
jest.useRealTimers();
});
function genList(props) {
let node = (
<List component="ul" itemKey="id" {...props}>
{({ id }) => <li>{id}</li>}
</List>
);
if (props.ref) {
node = <div>{node}</div>;
}
return mount(node);
}
it('should be true', () => {
expect(isFF).toBe(true);
});
// https://github.com/ant-design/ant-design/issues/26372
it('FireFox should patch scroll speed', () => {
const wheelPreventDefault = jest.fn();
const firefoxPreventDefault = jest.fn();
const wrapper = genList({ itemHeight: 20, height: 100, data: genData(100) });
const ulElement = wrapper.find('ul').instance();
act(() => {
const wheelEvent = new Event('wheel');
wheelEvent.deltaY = 3;
wheelEvent.preventDefault = wheelPreventDefault;
ulElement.dispatchEvent(wheelEvent);
const firefoxPixelScrollEvent = new Event('MozMousePixelScroll');
firefoxPixelScrollEvent.detail = 6;
firefoxPixelScrollEvent.preventDefault = firefoxPreventDefault;
ulElement.dispatchEvent(firefoxPixelScrollEvent);
const firefoxScrollEvent = new Event('DOMMouseScroll');
firefoxScrollEvent.detail = 3;
firefoxScrollEvent.preventDefault = firefoxPreventDefault;
ulElement.dispatchEvent(firefoxScrollEvent);
jest.runAllTimers();
});
expect(wheelPreventDefault).not.toHaveBeenCalled();
expect(firefoxPreventDefault).toHaveBeenCalledTimes(1);
});
it('should call preventDefault on MozMousePixelScroll', () => {
const preventDefault = jest.fn();
const wrapper = genList({ itemHeight: 20, height: 100, data: genData(100) });
const ulElement = wrapper.find('ul').instance();
act(() => {
const event = new Event('MozMousePixelScroll');
event.detail = 6;
event.preventDefault = preventDefault;
ulElement.dispatchEvent(event);
jest.runAllTimers();
});
expect(preventDefault).toHaveBeenCalled();
});
it('should not call preventDefault on MozMousePixelScroll when scrolling up at top boundary', () => {
const preventDefault = jest.fn();
const wrapper = genList({ itemHeight: 20, height: 100, data: genData(100) });
const ulElement = wrapper.find('ul').instance();
act(() => {
const event = new Event('MozMousePixelScroll');
event.detail = -6;
event.preventDefault = preventDefault;
ulElement.dispatchEvent(event);
jest.runAllTimers();
});
expect(preventDefault).not.toHaveBeenCalled();
});
it('should not call preventDefault on MozMousePixelScroll when scrolling down at bottom boundary', () => {
const preventDefault = jest.fn();
const listRef = React.createRef();
const wrapper = genList({ itemHeight: 20, height: 100, data: genData(100), ref: listRef });
const ulElement = wrapper.find('ul').instance();
// scroll to bottom
act(() => {
listRef.current.scrollTo(99999);
jest.runAllTimers();
});
expect(wrapper.find('ul').instance().scrollTop).toEqual(1900);
act(() => {
const event = new Event('MozMousePixelScroll');
event.detail = 6;
event.preventDefault = preventDefault;
ulElement.dispatchEvent(event);
jest.runAllTimers();
});
expect(preventDefault).not.toHaveBeenCalled();
});
});