@@ -122,4 +122,85 @@ describe('polling tests', () => {
122
122
123
123
expect ( mockBaseQuery . mock . calls . length ) . toBeGreaterThanOrEqual ( 2 )
124
124
} )
125
+
126
+ it ( 'respects skipPollOnFocusLost' , async ( ) => {
127
+ storeRef . store . dispatch (
128
+ getPosts . initiate ( 1 , {
129
+ subscriptionOptions : { pollingInterval : 10 , skipPollOnFocusLost : true } ,
130
+ subscribe : true ,
131
+ } )
132
+ )
133
+
134
+ await delay ( 20 )
135
+ const callsWithSkip = mockBaseQuery . mock . calls . length
136
+
137
+ storeRef . store . dispatch (
138
+ getPosts . initiate ( 1 , {
139
+ subscriptionOptions : {
140
+ pollingInterval : 10 ,
141
+ skipPollOnFocusLost : false ,
142
+ } ,
143
+ subscribe : true ,
144
+ } )
145
+ )
146
+
147
+ await delay ( 30 )
148
+ const callsWithoutSkip = mockBaseQuery . mock . calls . length
149
+
150
+ expect ( callsWithSkip ) . toBe ( 1 )
151
+ expect ( callsWithoutSkip ) . toBeGreaterThan ( 2 )
152
+ } )
153
+
154
+ it ( 'replaces skipPollOnFocusLost with most recent mount' , async ( ) => {
155
+ storeRef . store . dispatch (
156
+ getPosts . initiate ( 1 , {
157
+ subscriptionOptions : {
158
+ pollingInterval : 10 ,
159
+ skipPollOnFocusLost : false ,
160
+ } ,
161
+ subscribe : true ,
162
+ } )
163
+ )
164
+
165
+ await delay ( 50 )
166
+ const callsWithSkip = mockBaseQuery . mock . calls . length
167
+
168
+ storeRef . store . dispatch (
169
+ getPosts . initiate ( 1 , {
170
+ subscriptionOptions : { pollingInterval : 15 , skipPollOnFocusLost : true } ,
171
+ subscribe : true ,
172
+ } )
173
+ )
174
+
175
+ await delay ( 50 )
176
+ const callsWithoutSkip = mockBaseQuery . mock . calls . length
177
+
178
+ expect ( callsWithSkip ) . toBeGreaterThan ( 2 )
179
+ expect ( callsWithoutSkip ) . toBe ( callsWithSkip + 1 )
180
+ } )
181
+
182
+ it ( 'replaces skipPollOnFocusLost when the subscription options are updated' , async ( ) => {
183
+ const { requestId, queryCacheKey, ...subscription } =
184
+ storeRef . store . dispatch (
185
+ getPosts . initiate ( 1 , {
186
+ subscriptionOptions : { pollingInterval : 10 } ,
187
+ subscribe : true ,
188
+ } )
189
+ )
190
+
191
+ const getSubs = createSubscriptionGetter ( queryCacheKey )
192
+
193
+ await delay ( 1 )
194
+ expect ( Object . keys ( getSubs ( ) ) ) . toHaveLength ( 1 )
195
+ expect ( getSubs ( ) [ requestId ] . skipPollOnFocusLost ) . toBe ( false )
196
+
197
+ subscription . updateSubscriptionOptions ( {
198
+ pollingInterval : 20 ,
199
+ skipPollOnFocusLost : true ,
200
+ } )
201
+
202
+ await delay ( 1 )
203
+ expect ( Object . keys ( getSubs ( ) ) ) . toHaveLength ( 1 )
204
+ expect ( getSubs ( ) [ requestId ] . skipPollOnFocusLost ) . toBe ( true )
205
+ } )
125
206
} )
0 commit comments