1
- import { render , cleanup , fireEvent } from '@testing-library/react' ;
1
+ import { render , cleanup , fireEvent , act , waitFor } from '@testing-library/react' ;
2
2
import React from 'react' ;
3
3
import { sleep } from '../../../tests/utils' ;
4
4
import TestsEvent from '../../../tests/events' ;
@@ -7,6 +7,7 @@ import FloatingBubble from '../FloatingBubble';
7
7
8
8
describe ( 'FloatingBubble' , ( ) => {
9
9
beforeEach ( ( ) => {
10
+ jest . useFakeTimers ( ) ;
10
11
Object . defineProperty ( window , 'inBrowser' , { value : true } ) ;
11
12
} ) ;
12
13
afterEach ( ( ) => {
@@ -16,14 +17,14 @@ describe('FloatingBubble', () => {
16
17
it ( 'should render correctly when icon set' , async ( ) => {
17
18
const restore = mockGetBoundingClientRect ( { width : 48 , height : 48 } ) ;
18
19
const root = document . createElement ( 'div' ) ;
19
- const { container } = render ( < FloatingBubble icon = "chat-o" teleport = { root } /> ) ;
20
+ render ( < FloatingBubble icon = "chat-o" teleport = { root } /> ) ;
20
21
await sleep ( 10 ) ;
21
22
const floatingBubbleEl = root . querySelector < HTMLDivElement > ( '.rc-floating-bubble' ) ! ;
22
23
expect ( floatingBubbleEl . style . transform ) . toEqual (
23
24
`translate3d(${ window . innerWidth - 48 - 24 } px, ${ window . innerHeight - 48 - 24 } px, 0)` ,
24
25
) ;
25
26
expect ( floatingBubbleEl . querySelector ( '.van-icon-chat-o' ) ) . not . toBeNull ( ) ;
26
- expect ( container ) . toMatchSnapshot ( ) ;
27
+ expect ( floatingBubbleEl ) . toMatchSnapshot ( ) ;
27
28
restore ( ) ;
28
29
} ) ;
29
30
it ( 'should render correctly when offset set' , async ( ) => {
@@ -45,53 +46,93 @@ describe('FloatingBubble', () => {
45
46
const restore = mockGetBoundingClientRect ( { width : 48 , height : 48 } ) ;
46
47
const root = document . createElement ( 'div' ) ;
47
48
render ( < FloatingBubble icon = "chat-o" teleport = { root } /> ) ;
49
+ await act ( ( ) => {
50
+ jest . useRealTimers ( ) ;
51
+ } ) ;
48
52
const floatingBubbleEl = root . querySelector < HTMLDivElement > ( '.rc-floating-bubble' ) ! ;
49
- await TestsEvent . triggerDrag ( floatingBubbleEl , [ - 100 , - 100 ] ) ;
50
- expect ( floatingBubbleEl . style . transform ) . toEqual (
51
- `translate3d(${ window . innerWidth - 48 - 24 } px, 24px, 0)` ,
52
- ) ;
53
+ await act ( async ( ) => {
54
+ await TestsEvent . triggerDrag ( floatingBubbleEl , [ - 100 , - 100 ] ) ;
55
+ } ) ;
56
+ await sleep ( 1000 ) ;
57
+ await waitFor ( ( ) => {
58
+ expect ( floatingBubbleEl . style . transform ) . toEqual (
59
+ `translate3d(${ window . innerWidth - 48 - 24 } px, ${ window . innerHeight - 48 - 24 - 100 } px, 0)` ,
60
+ ) ;
61
+ } ) ;
53
62
restore ( ) ;
54
63
} ) ;
55
64
it ( 'should only x axis direction move when axis is x' , async ( ) => {
56
65
const restore = mockGetBoundingClientRect ( { width : 48 , height : 48 } ) ;
57
66
const root = document . createElement ( 'div' ) ;
58
- render ( < FloatingBubble icon = "chat-o" axis = "x" teleport = { root } /> ) ;
67
+ render ( < FloatingBubble icon = "chat-o" teleport = { root } axis = "x" /> ) ;
68
+ await act ( ( ) => {
69
+ jest . useRealTimers ( ) ;
70
+ } ) ;
59
71
const floatingBubbleEl = root . querySelector < HTMLDivElement > ( '.rc-floating-bubble' ) ! ;
60
- await TestsEvent . triggerDrag ( floatingBubbleEl , [ 2000 , - 100 ] ) ;
61
- expect ( floatingBubbleEl . style . transform ) . toEqual (
62
- `translate3d(${ window . innerWidth - 24 } px, ${ window . innerHeight - 48 - 24 } px, 0)` ,
63
- ) ;
72
+ await act ( async ( ) => {
73
+ await TestsEvent . triggerDrag ( floatingBubbleEl , [ 100 , - 100 ] ) ;
74
+ } ) ;
75
+
76
+ await sleep ( 1000 ) ;
77
+ await waitFor ( ( ) => {
78
+ expect ( floatingBubbleEl . style . transform ) . toEqual (
79
+ `translate3d(${ window . innerWidth - 48 - 24 } px, ${ window . innerHeight - 48 - 24 } px, 0)` ,
80
+ ) ;
81
+ } ) ;
64
82
restore ( ) ;
65
83
} ) ;
66
84
it ( 'should free direction move when axis is "xy"' , async ( ) => {
67
85
const restore = mockGetBoundingClientRect ( { width : 48 , height : 48 } ) ;
68
86
const root = document . createElement ( 'div' ) ;
69
87
render ( < FloatingBubble icon = "chat-o" axis = "xy" teleport = { root } /> ) ;
70
88
const floatingBubbleEl = root . querySelector < HTMLDivElement > ( '.rc-floating-bubble' ) ! ;
71
- await TestsEvent . triggerDrag ( floatingBubbleEl , [ - 1000 , 24 ] ) ;
89
+ await act ( async ( ) => {
90
+ await TestsEvent . triggerDrag ( floatingBubbleEl , [ - 100 , - 100 ] ) ;
91
+ } ) ;
72
92
expect ( floatingBubbleEl . style . transform ) . toEqual (
73
- `translate3d(${ window . innerWidth - 48 - 24 } px, ${ window . innerHeight - 48 - 24 } px, 0)` ,
93
+ `translate3d(${ window . innerWidth - 48 - 24 } px, ${ window . innerHeight - 48 - 24 - 100 } px, 0)` ,
74
94
) ;
75
95
restore ( ) ;
76
96
} ) ;
77
97
it ( 'should magnetic to x axios when magnetic is "x" ' , async ( ) => {
78
98
const restore = mockGetBoundingClientRect ( { width : 48 , height : 48 } ) ;
79
99
const root = document . createElement ( 'div' ) ;
80
100
render ( < FloatingBubble icon = "chat-o" axis = "xy" magnetic = "x" teleport = { root } /> ) ;
101
+ await act ( ( ) => {
102
+ jest . useRealTimers ( ) ;
103
+ } ) ;
81
104
const floatingBubbleEl = root . querySelector < HTMLDivElement > ( '.rc-floating-bubble' ) ! ;
82
- await TestsEvent . triggerDrag ( floatingBubbleEl , [ 100 , 100 ] ) ;
83
- await sleep ( 100 ) ;
84
- expect ( floatingBubbleEl . style . transform ) . toEqual ( `translate3d(24px, 100px, 0)` ) ;
105
+ await act ( async ( ) => {
106
+ await TestsEvent . triggerDrag ( floatingBubbleEl , [ - 100 , - 100 ] ) ;
107
+ } ) ;
108
+ await act ( ( ) => {
109
+ jest . useRealTimers ( ) ;
110
+ } ) ;
111
+ await sleep ( 400 ) ;
112
+ await waitFor ( ( ) => {
113
+ expect ( floatingBubbleEl . style . transform ) . toEqual (
114
+ `translate3d(${ window . innerWidth - 48 - 24 } px, ${ window . innerHeight - 48 - 24 - 100 } px, 0)` ,
115
+ ) ;
116
+ } ) ;
85
117
restore ( ) ;
86
118
} ) ;
87
119
it ( 'should magnetic to y axios when magnetic is "y" ' , async ( ) => {
88
120
const restore = mockGetBoundingClientRect ( { width : 48 , height : 48 } ) ;
89
121
const root = document . createElement ( 'div' ) ;
90
122
render ( < FloatingBubble icon = "chat-o" axis = "xy" magnetic = "y" teleport = { root } /> ) ;
91
123
const floatingBubbleEl = root . querySelector < HTMLDivElement > ( '.rc-floating-bubble' ) ! ;
92
- await TestsEvent . triggerDrag ( floatingBubbleEl , [ 100 , 100 ] ) ;
93
- await sleep ( 100 ) ;
94
- expect ( floatingBubbleEl . style . transform ) . toEqual ( `translate3d(100px, 24px, 0)` ) ;
124
+ await act ( async ( ) => {
125
+ await TestsEvent . triggerDrag ( floatingBubbleEl , [ - 100 , - 100 ] ) ;
126
+ } ) ;
127
+ await act ( ( ) => {
128
+ jest . useRealTimers ( ) ;
129
+ } ) ;
130
+ await sleep ( 1000 ) ;
131
+ await waitFor ( ( ) => {
132
+ expect ( floatingBubbleEl . style . transform ) . toEqual (
133
+ `translate3d(${ window . innerWidth - 48 - 24 } px, ${ window . innerHeight - 48 - 24 } px, 0)` ,
134
+ ) ;
135
+ } ) ;
95
136
restore ( ) ;
96
137
} ) ;
97
138
it ( 'should emit click when click wrapper' , async ( ) => {
@@ -121,18 +162,27 @@ describe('FloatingBubble', () => {
121
162
) ;
122
163
const floatingBubbleEl = root . querySelector < HTMLDivElement > ( '.rc-floating-bubble' ) ! ;
123
164
124
- await TestsEvent . triggerDrag ( floatingBubbleEl , [ 10 , 10 ] ) ;
165
+ // await act(async () => {
166
+ await TestsEvent . triggerDrag ( floatingBubbleEl , [ - 100 , - 100 ] ) ;
167
+ // });
125
168
expect ( onClick ) . not . toHaveBeenCalled ( ) ;
126
- await sleep ( 10 ) ;
169
+ // await act(() => {
170
+ // jest.useRealTimers();
171
+ // });
172
+ await sleep ( 400 ) ;
173
+ // await waitFor(() => {
127
174
expect ( onOffsetChange ) . toHaveBeenCalled ( ) ;
175
+ // });
128
176
restore ( ) ;
129
177
} ) ;
130
178
it ( 'should lock drag when axis is "lock"' , async ( ) => {
131
179
const restore = mockGetBoundingClientRect ( { width : 48 , height : 48 } ) ;
132
180
const root = document . createElement ( 'div' ) ;
133
181
render ( < FloatingBubble icon = "chat-o" axis = "lock" magnetic = "y" teleport = { root } /> ) ;
134
182
const floatingBubbleEl = root . querySelector < HTMLDivElement > ( '.rc-floating-bubble' ) ! ;
135
- await TestsEvent . triggerDrag ( floatingBubbleEl , [ 0 , 0 ] ) ;
183
+ await act ( async ( ) => {
184
+ await TestsEvent . triggerDrag ( floatingBubbleEl , [ 0 , 0 ] ) ;
185
+ } ) ;
136
186
expect ( floatingBubbleEl . style . transform ) . toEqual (
137
187
`translate3d(${ window . innerWidth - 48 - 24 } px, ${ window . innerHeight - 48 - 24 } px, 0)` ,
138
188
) ;
@@ -141,17 +191,19 @@ describe('FloatingBubble', () => {
141
191
it ( 'should not move when drag distance belove TAP_OFFSET"' , async ( ) => {
142
192
const restore = mockGetBoundingClientRect ( { width : 48 , height : 48 } ) ;
143
193
const root = document . createElement ( 'div' ) ;
144
- render ( < FloatingBubble icon = "chat-o" axis = "x" magnetic = "y " teleport = { root } /> ) ;
194
+ render ( < FloatingBubble icon = "chat-o" axis = "xy " teleport = { root } /> ) ;
145
195
const floatingBubbleEl = root . querySelector < HTMLDivElement > ( '.rc-floating-bubble' ) ! ;
146
- await TestsEvent . triggerDrag ( floatingBubbleEl , [ 4 , 4 ] ) ;
196
+ await act ( async ( ) => {
197
+ await TestsEvent . triggerDrag ( floatingBubbleEl , [ 4 , 4 ] ) ;
198
+ } ) ;
147
199
expect ( floatingBubbleEl . style . transform ) . toEqual (
148
200
`translate3d(${ window . innerWidth - 48 - 24 } px, ${ window . innerHeight - 48 - 24 } px, 0)` ,
149
201
) ;
150
202
restore ( ) ;
151
203
} ) ;
152
204
153
205
it ( 'should teleport body when default' , async ( ) => {
154
- const { container } = render ( < FloatingBubble icon = "chat-o" axis = "xy" magnetic = "y" /> ) ;
206
+ const { container } = render ( < FloatingBubble icon = "chat-o" axis = "xy" /> ) ;
155
207
expect ( container . parentNode ) . toEqual ( document . body ) ;
156
208
expect ( container ) . toMatchSnapshot ( ) ;
157
209
} ) ;
0 commit comments