@@ -139,7 +139,9 @@ test("script onerror should reject promise with multiple loaders", async () => {
139
139
expect ( loader [ "done" ] ) . toBeTruthy ( ) ;
140
140
expect ( loader [ "loading" ] ) . toBeFalsy ( ) ;
141
141
expect ( loader [ "onerrorEvent" ] ) . toBeInstanceOf ( ErrorEvent ) ;
142
+
142
143
rejection = expect ( extraLoader . load ( ) ) . rejects . toBeInstanceOf ( ErrorEvent ) ;
144
+ loader [ "loadErrorCallback" ] ( document . createEvent ( "ErrorEvent" ) ) ;
143
145
144
146
await rejection ;
145
147
expect ( extraLoader [ "done" ] ) . toBeTruthy ( ) ;
@@ -165,6 +167,52 @@ test("script onerror should retry", async () => {
165
167
expect ( console . log ) . toHaveBeenCalledTimes ( loader . retries ) ;
166
168
} ) ;
167
169
170
+ test ( "script onerror should reset retry mechanism with next loader" , async ( ) => {
171
+ const loader = new Loader ( { apiKey : "foo" , retries : 1 } ) ;
172
+ const deleteScript = jest . spyOn ( loader , "deleteScript" ) ;
173
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
174
+ console . log = jest . fn ( ) ;
175
+
176
+ let rejection = expect ( loader . load ( ) ) . rejects . toBeInstanceOf ( ErrorEvent ) ;
177
+ loader [ "loadErrorCallback" ] ( document . createEvent ( "ErrorEvent" ) ) ;
178
+ loader [ "loadErrorCallback" ] ( document . createEvent ( "ErrorEvent" ) ) ;
179
+ jest . runAllTimers ( ) ;
180
+ await rejection ;
181
+
182
+ rejection = expect ( loader . load ( ) ) . rejects . toBeInstanceOf ( ErrorEvent ) ;
183
+ expect ( loader [ "done" ] ) . toBeFalsy ( ) ;
184
+ expect ( loader [ "loading" ] ) . toBeTruthy ( ) ;
185
+ expect ( loader [ "errors" ] . length ) . toBe ( 0 ) ;
186
+
187
+ loader [ "loadErrorCallback" ] ( document . createEvent ( "ErrorEvent" ) ) ;
188
+ loader [ "loadErrorCallback" ] ( document . createEvent ( "ErrorEvent" ) ) ;
189
+ jest . runAllTimers ( ) ;
190
+
191
+ await rejection ;
192
+ expect ( deleteScript ) . toHaveBeenCalledTimes ( 3 ) ;
193
+ expect ( console . log ) . toHaveBeenCalledTimes ( loader . retries * 2 ) ;
194
+ } ) ;
195
+
196
+ test ( "script onerror should not reset retry mechanism with parallel loaders" , async ( ) => {
197
+ const loader = new Loader ( { apiKey : "foo" , retries : 1 } ) ;
198
+ const deleteScript = jest . spyOn ( loader , "deleteScript" ) ;
199
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
200
+ console . log = jest . fn ( ) ;
201
+
202
+ const rejection1 = expect ( loader . load ( ) ) . rejects . toBeInstanceOf ( ErrorEvent ) ;
203
+ const rejection2 = expect ( loader . load ( ) ) . rejects . toBeInstanceOf ( ErrorEvent ) ;
204
+ loader [ "loadErrorCallback" ] ( document . createEvent ( "ErrorEvent" ) ) ;
205
+ loader [ "loadErrorCallback" ] ( document . createEvent ( "ErrorEvent" ) ) ;
206
+ jest . runAllTimers ( ) ;
207
+
208
+ await Promise . all ( [ rejection1 , rejection2 ] ) ;
209
+ expect ( loader [ "done" ] ) . toBeTruthy ( ) ;
210
+ expect ( loader [ "loading" ] ) . toBeFalsy ( ) ;
211
+ expect ( loader [ "errors" ] . length ) . toBe ( 2 ) ;
212
+ expect ( deleteScript ) . toHaveBeenCalledTimes ( 1 ) ;
213
+ expect ( console . log ) . toHaveBeenCalledTimes ( loader . retries ) ;
214
+ } ) ;
215
+
168
216
test ( "singleton should be used" , ( ) => {
169
217
const loader = new Loader ( { apiKey : "foo" } ) ;
170
218
const extraLoader = new Loader ( { apiKey : "foo" } ) ;
0 commit comments