Skip to content

Commit c973f19

Browse files
committed
fix: some test error
1 parent 6b1ebb8 commit c973f19

File tree

4 files changed

+84
-59
lines changed

4 files changed

+84
-59
lines changed

tests/scroll-Firefox.test.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import React from 'react';
2-
import { act } from 'react-dom/test-utils';
1+
import { act } from '@testing-library/react';
32
import { mount } from 'enzyme';
4-
import { spyElementPrototypes } from './utils/domHook';
3+
import React from 'react';
54
import List from '../src';
65
import isFF from '../src/utils/isFirefox';
6+
import { spyElementPrototypes } from './utils/domHook';
77

88
function genData(count) {
99
return new Array(count).fill(null).map((_, index) => ({ id: String(index) }));
@@ -124,8 +124,10 @@ describe('List.Firefox-Scroll', () => {
124124
const wrapper = genList({ itemHeight: 20, height: 100, data: genData(100), ref: listRef });
125125
const ulElement = wrapper.find('ul').instance();
126126
// scroll to bottom
127-
listRef.current.scrollTo(99999);
128-
jest.runAllTimers();
127+
act(() => {
128+
listRef.current.scrollTo(99999);
129+
jest.runAllTimers();
130+
});
129131
expect(wrapper.find('ul').instance().scrollTop).toEqual(1900);
130132

131133
act(() => {

tests/scroll.test.js

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,13 @@ describe('List.Scroll', () => {
9494
jest.useFakeTimers();
9595
const listRef = React.createRef();
9696
const wrapper = genList({ itemHeight: 20, height: 100, data: genData(100), ref: listRef });
97-
jest.runAllTimers();
9897

99-
listRef.current.scrollTo(null);
98+
act(() => {
99+
jest.runAllTimers();
100+
101+
listRef.current.scrollTo(null);
102+
});
103+
100104
expect(wrapper.find('.rc-virtual-list-scrollbar-thumb').props().style.display).not.toEqual(
101105
'none',
102106
);
@@ -107,8 +111,10 @@ describe('List.Scroll', () => {
107111
it('value scroll', () => {
108112
const listRef = React.createRef();
109113
const wrapper = genList({ itemHeight: 20, height: 100, data: genData(100), ref: listRef });
110-
listRef.current.scrollTo(903);
111-
jest.runAllTimers();
114+
act(() => {
115+
listRef.current.scrollTo(903);
116+
jest.runAllTimers();
117+
});
112118
expect(wrapper.find('ul').instance().scrollTop).toEqual(903);
113119

114120
wrapper.unmount();
@@ -125,9 +131,8 @@ describe('List.Scroll', () => {
125131
...result,
126132
ref,
127133
scrollTo: (...args) => {
128-
ref.current.scrollTo(...args);
129-
130134
act(() => {
135+
ref.current.scrollTo(...args);
131136
jest.runAllTimers();
132137
});
133138
},
@@ -153,8 +158,10 @@ describe('List.Scroll', () => {
153158

154159
it('scroll top should not out of range', () => {
155160
const { scrollTo, container } = presetList();
156-
scrollTo({ index: 0, align: 'bottom' });
157-
jest.runAllTimers();
161+
act(() => {
162+
scrollTo({ index: 0, align: 'bottom' });
163+
jest.runAllTimers();
164+
});
158165
expect(container.querySelector('ul').scrollTop).toEqual(0);
159166
});
160167

@@ -389,9 +396,13 @@ describe('List.Scroll', () => {
389396
ref: listRef,
390397
direction: 'rtl',
391398
});
392-
jest.runAllTimers();
393399

394-
listRef.current.scrollTo(null);
400+
act(() => {
401+
jest.runAllTimers();
402+
403+
listRef.current.scrollTo(null);
404+
});
405+
395406
expect(wrapper.find('.rc-virtual-list-scrollbar-thumb').props().style.display).not.toEqual(
396407
'none',
397408
);

tests/scrollWidth.test.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,10 +230,14 @@ describe('List.scrollWidth', () => {
230230
ref: listRef,
231231
});
232232

233-
listRef.current.scrollTo({ left: 135 });
233+
act(() => {
234+
listRef.current.scrollTo({ left: 135 });
235+
});
234236
expect(listRef.current.getScrollInfo()).toEqual({ x: 135, y: 0 });
235237

236-
listRef.current.scrollTo({ left: -99 });
238+
act(() => {
239+
listRef.current.scrollTo({ left: -99 });
240+
});
237241
expect(listRef.current.getScrollInfo()).toEqual({ x: 0, y: 0 });
238242
});
239243

tests/touch.test.js

Lines changed: 50 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -71,22 +71,24 @@ describe('List.Touch', () => {
7171
return wrapper.find('.rc-virtual-list-holder').instance();
7272
}
7373

74-
// start
75-
const touchEvent = new Event('touchstart');
76-
touchEvent.touches = [{ pageY: 100 }];
77-
getElement().dispatchEvent(touchEvent);
78-
79-
// move
80-
const moveEvent = new Event('touchmove');
81-
moveEvent.touches = [{ pageY: 90 }];
82-
getElement().dispatchEvent(moveEvent);
83-
84-
// end
85-
const endEvent = new Event('touchend');
86-
getElement().dispatchEvent(endEvent);
87-
88-
// smooth
89-
jest.runAllTimers();
74+
act(() => {
75+
// start
76+
const touchEvent = new Event('touchstart');
77+
touchEvent.touches = [{ pageY: 100 }];
78+
getElement().dispatchEvent(touchEvent);
79+
80+
// move
81+
const moveEvent = new Event('touchmove');
82+
moveEvent.touches = [{ pageY: 90 }];
83+
getElement().dispatchEvent(moveEvent);
84+
85+
// end
86+
const endEvent = new Event('touchend');
87+
getElement().dispatchEvent(endEvent);
88+
89+
// smooth
90+
jest.runAllTimers();
91+
});
9092
expect(wrapper.find('ul').instance().scrollTop > 10).toBeTruthy();
9193

9294
wrapper.unmount();
@@ -99,35 +101,39 @@ describe('List.Touch', () => {
99101
return wrapper.find('.rc-virtual-list-holder').instance();
100102
}
101103

102-
// start
103-
const touchEvent = new Event('touchstart');
104-
touchEvent.touches = [{ pageY: 500 }];
105-
getElement().dispatchEvent(touchEvent);
106-
107-
// move
108104
const preventDefault = jest.fn();
109-
const moveEvent = new Event('touchmove');
110-
moveEvent.touches = [{ pageY: 0 }];
111-
moveEvent.preventDefault = preventDefault;
112-
getElement().dispatchEvent(moveEvent);
113105

106+
act(() => {
107+
// start
108+
const touchEvent = new Event('touchstart');
109+
touchEvent.touches = [{ pageY: 500 }];
110+
getElement().dispatchEvent(touchEvent);
111+
112+
// move
113+
const moveEvent = new Event('touchmove');
114+
moveEvent.touches = [{ pageY: 0 }];
115+
moveEvent.preventDefault = preventDefault;
116+
getElement().dispatchEvent(moveEvent);
117+
});
114118
// Call preventDefault
115119
expect(preventDefault).toHaveBeenCalled();
116120

117-
// ======= Not call since scroll to the bottom =======
118-
jest.runAllTimers();
119-
preventDefault.mockReset();
121+
act(() => {
122+
// ======= Not call since scroll to the bottom =======
123+
jest.runAllTimers();
124+
preventDefault.mockReset();
120125

121-
// start
122-
const touchEvent2 = new Event('touchstart');
123-
touchEvent2.touches = [{ pageY: 500 }];
124-
getElement().dispatchEvent(touchEvent2);
126+
// start
127+
const touchEvent2 = new Event('touchstart');
128+
touchEvent2.touches = [{ pageY: 500 }];
129+
getElement().dispatchEvent(touchEvent2);
125130

126-
// move
127-
const moveEvent2 = new Event('touchmove');
128-
moveEvent2.touches = [{ pageY: 0 }];
129-
moveEvent2.preventDefault = preventDefault;
130-
getElement().dispatchEvent(moveEvent2);
131+
// move
132+
const moveEvent2 = new Event('touchmove');
133+
moveEvent2.touches = [{ pageY: 0 }];
134+
moveEvent2.preventDefault = preventDefault;
135+
getElement().dispatchEvent(moveEvent2);
136+
});
131137

132138
expect(preventDefault).not.toHaveBeenCalled();
133139
});
@@ -137,16 +143,18 @@ describe('List.Touch', () => {
137143
const preventDefault = jest.fn();
138144
const wrapper = genList({ itemHeight: 20, height: 100, data: genData(100) });
139145

140-
const touchEvent = new Event('touchstart');
141-
touchEvent.preventDefault = preventDefault;
142-
wrapper.find('.rc-virtual-list-scrollbar').instance().dispatchEvent(touchEvent);
146+
act(() => {
147+
const touchEvent = new Event('touchstart');
148+
touchEvent.preventDefault = preventDefault;
149+
wrapper.find('.rc-virtual-list-scrollbar').instance().dispatchEvent(touchEvent);
150+
});
143151

144152
expect(preventDefault).toHaveBeenCalled();
145153
});
146154

147155
it('nest touch', async () => {
148156
const { container } = render(
149-
<List component="ul" itemHeight={20} height={100} data={genData(100)}>
157+
<List component="ul" itemHeight={20} height={100} data={genData(100)} itemKey="id">
150158
{({ id }) =>
151159
id === '0' ? (
152160
<li>

0 commit comments

Comments
 (0)