Skip to content

Commit ecbd578

Browse files
committed
test: wrap text with act
1 parent 7cfb93d commit ecbd578

File tree

2 files changed

+86
-58
lines changed

2 files changed

+86
-58
lines changed

tests/scroll.test.js

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ describe('List.Scroll', () => {
5252
jest.useFakeTimers();
5353
const listRef = React.createRef();
5454
const wrapper = genList({ itemHeight: 20, height: 100, data: genData(100), ref: listRef });
55-
jest.runAllTimers();
55+
act(() => {
56+
jest.runAllTimers();
57+
});
5658

5759
listRef.current.scrollTo(null);
5860
expect(wrapper.find('.rc-virtual-list-scrollbar-thumb').props().style.display).not.toEqual(
@@ -65,8 +67,10 @@ describe('List.Scroll', () => {
6567
it('value scroll', () => {
6668
const listRef = React.createRef();
6769
const wrapper = genList({ itemHeight: 20, height: 100, data: genData(100), ref: listRef });
68-
listRef.current.scrollTo(903);
69-
jest.runAllTimers();
70+
act(() => {
71+
listRef.current.scrollTo(903);
72+
jest.runAllTimers();
73+
});
7074
expect(wrapper.find('ul').instance().scrollTop).toEqual(903);
7175

7276
wrapper.unmount();
@@ -79,40 +83,56 @@ describe('List.Scroll', () => {
7983

8084
describe('index scroll', () => {
8185
it('work', () => {
82-
listRef.current.scrollTo({ index: 30, align: 'top' });
83-
jest.runAllTimers();
86+
act(() => {
87+
listRef.current.scrollTo({ index: 30, align: 'top' });
88+
jest.runAllTimers();
89+
});
8490
expect(wrapper.find('ul').instance().scrollTop).toEqual(600);
8591
});
8692

8793
it('out of range should not crash', () => {
8894
expect(() => {
89-
listRef.current.scrollTo({ index: 99999999999, align: 'top' });
90-
jest.runAllTimers();
95+
act(() => {
96+
listRef.current.scrollTo({ index: 99999999999, align: 'top' });
97+
jest.runAllTimers();
98+
});
9199
}).not.toThrow();
92100
});
93101
});
94102

95103
it('scroll top should not out of range', () => {
96-
listRef.current.scrollTo({ index: 0, align: 'bottom' });
97-
jest.runAllTimers();
104+
act(() => {
105+
listRef.current.scrollTo({ index: 0, align: 'bottom' });
106+
jest.runAllTimers();
107+
});
98108
expect(wrapper.find('ul').instance().scrollTop).toEqual(0);
99109
});
100110

101111
it('key scroll', () => {
102-
listRef.current.scrollTo({ key: '30', align: 'bottom' });
103-
jest.runAllTimers();
112+
act(() => {
113+
listRef.current.scrollTo({ key: '30', align: 'bottom' });
114+
jest.runAllTimers();
115+
});
104116
expect(wrapper.find('ul').instance().scrollTop).toEqual(520);
105117
});
106118

107119
it('smart', () => {
108-
listRef.current.scrollTo(0);
109-
listRef.current.scrollTo({ index: 30 });
110-
jest.runAllTimers();
120+
act(() => {
121+
listRef.current.scrollTo(0);
122+
});
123+
act(() => {
124+
listRef.current.scrollTo({ index: 30 });
125+
jest.runAllTimers();
126+
});
111127
expect(wrapper.find('ul').instance().scrollTop).toEqual(520);
112128

113-
listRef.current.scrollTo(800);
114-
listRef.current.scrollTo({ index: 30 });
115-
jest.runAllTimers();
129+
act(() => {
130+
listRef.current.scrollTo(800);
131+
});
132+
act(() => {
133+
listRef.current.scrollTo({ index: 30 });
134+
jest.runAllTimers();
135+
});
116136
expect(wrapper.find('ul').instance().scrollTop).toEqual(600);
117137
});
118138
});

tests/touch.test.js

Lines changed: 49 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React from 'react';
2+
import { act } from 'react-dom/test-utils';
23
import { mount } from 'enzyme';
34
import { spyElementPrototypes } from './utils/domHook';
45
import List from '../src';
@@ -55,22 +56,25 @@ describe('List.Touch', () => {
5556
return wrapper.find('.rc-virtual-list-holder').instance();
5657
}
5758

58-
// start
59-
const touchEvent = new Event('touchstart');
60-
touchEvent.touches = [{ pageY: 100 }];
61-
getElement().dispatchEvent(touchEvent);
59+
act(() => {
60+
// start
61+
const touchEvent = new Event('touchstart');
62+
touchEvent.touches = [{ pageY: 100 }];
63+
getElement().dispatchEvent(touchEvent);
64+
65+
// move
66+
const moveEvent = new Event('touchmove');
67+
moveEvent.touches = [{ pageY: 90 }];
68+
getElement().dispatchEvent(moveEvent);
6269

63-
// move
64-
const moveEvent = new Event('touchmove');
65-
moveEvent.touches = [{ pageY: 90 }];
66-
getElement().dispatchEvent(moveEvent);
70+
// end
71+
const endEvent = new Event('touchend');
72+
getElement().dispatchEvent(endEvent);
6773

68-
// end
69-
const endEvent = new Event('touchend');
70-
getElement().dispatchEvent(endEvent);
74+
// smooth
75+
jest.runAllTimers();
76+
});
7177

72-
// smooth
73-
jest.runAllTimers();
7478
expect(wrapper.find('ul').instance().scrollTop > 10).toBeTruthy();
7579

7680
wrapper.unmount();
@@ -83,35 +87,40 @@ describe('List.Touch', () => {
8387
return wrapper.find('.rc-virtual-list-holder').instance();
8488
}
8589

86-
// start
87-
const touchEvent = new Event('touchstart');
88-
touchEvent.touches = [{ pageY: 500 }];
89-
getElement().dispatchEvent(touchEvent);
90-
91-
// move
9290
const preventDefault = jest.fn();
93-
const moveEvent = new Event('touchmove');
94-
moveEvent.touches = [{ pageY: 0 }];
95-
moveEvent.preventDefault = preventDefault;
96-
getElement().dispatchEvent(moveEvent);
91+
92+
act(() => {
93+
// start
94+
const touchEvent = new Event('touchstart');
95+
touchEvent.touches = [{ pageY: 500 }];
96+
getElement().dispatchEvent(touchEvent);
97+
98+
// move
99+
const moveEvent = new Event('touchmove');
100+
moveEvent.touches = [{ pageY: 0 }];
101+
moveEvent.preventDefault = preventDefault;
102+
getElement().dispatchEvent(moveEvent);
103+
});
97104

98105
// Call preventDefault
99106
expect(preventDefault).toHaveBeenCalled();
100107

101-
// ======= Not call since scroll to the bottom =======
102-
jest.runAllTimers();
103-
preventDefault.mockReset();
108+
act(() => {
109+
// ======= Not call since scroll to the bottom =======
110+
jest.runAllTimers();
111+
preventDefault.mockReset();
104112

105-
// start
106-
const touchEvent2 = new Event('touchstart');
107-
touchEvent2.touches = [{ pageY: 500 }];
108-
getElement().dispatchEvent(touchEvent2);
113+
// start
114+
const touchEvent2 = new Event('touchstart');
115+
touchEvent2.touches = [{ pageY: 500 }];
116+
getElement().dispatchEvent(touchEvent2);
109117

110-
// move
111-
const moveEvent2 = new Event('touchmove');
112-
moveEvent2.touches = [{ pageY: 0 }];
113-
moveEvent2.preventDefault = preventDefault;
114-
getElement().dispatchEvent(moveEvent2);
118+
// move
119+
const moveEvent2 = new Event('touchmove');
120+
moveEvent2.touches = [{ pageY: 0 }];
121+
moveEvent2.preventDefault = preventDefault;
122+
getElement().dispatchEvent(moveEvent2);
123+
});
115124

116125
expect(preventDefault).not.toHaveBeenCalled();
117126
});
@@ -121,12 +130,11 @@ describe('List.Touch', () => {
121130
const preventDefault = jest.fn();
122131
const wrapper = genList({ itemHeight: 20, height: 100, data: genData(100) });
123132

124-
const touchEvent = new Event('touchstart');
125-
touchEvent.preventDefault = preventDefault;
126-
wrapper
127-
.find('.rc-virtual-list-scrollbar')
128-
.instance()
129-
.dispatchEvent(touchEvent);
133+
act(() => {
134+
const touchEvent = new Event('touchstart');
135+
touchEvent.preventDefault = preventDefault;
136+
wrapper.find('.rc-virtual-list-scrollbar').instance().dispatchEvent(touchEvent);
137+
});
130138

131139
expect(preventDefault).toHaveBeenCalled();
132140
});

0 commit comments

Comments
 (0)