33import {
44 ReadableStream ,
55 ReadableStreamDefaultReadResult ,
6- } from 'web-streams-polyfill/ponyfill ' ;
6+ } from 'web-streams-polyfill' ;
77import { renderHook , act } from '@testing-library/react-hooks' ;
88import useDownloader , { jsDownload } from '../index' ;
99import { WindowDownloaderEmbedded } from '../types' ;
@@ -21,7 +21,8 @@ const expectedKeys = [
2121beforeAll ( ( ) => {
2222 global . window . fetch = fetch as Extract < WindowOrWorkerGlobalScope , 'fetch' > ;
2323 global . Response = Response ;
24- global . ReadableStream = ReadableStream ;
24+ global . ReadableStream =
25+ ReadableStream as unknown as typeof global . ReadableStream ;
2526} ) ;
2627
2728describe ( 'useDownloader successes' , ( ) => {
@@ -240,10 +241,14 @@ describe('useDownloader failures', () => {
240241
241242 describe ( 'Tests without msSaveBlob' , ( ) => {
242243 beforeAll ( ( ) => {
243- ( window as unknown as WindowDownloaderEmbedded ) . navigator . msSaveBlob =
244- undefined ;
245- window . URL . createObjectURL = ( ) => null ;
246- window . URL . revokeObjectURL = ( ) => null ;
244+ const currentWindow = window as unknown as WindowDownloaderEmbedded ;
245+
246+ currentWindow . navigator . msSaveBlob = undefined ;
247+
248+ if ( currentWindow . URL ) {
249+ currentWindow . URL . createObjectURL = ( ) => null ;
250+ currentWindow . URL . revokeObjectURL = ( ) => null ;
251+ }
247252 } ) ;
248253
249254 it ( 'should test with URL and being revoked' , async ( ) => {
@@ -262,17 +267,22 @@ describe('useDownloader failures', () => {
262267 } ) ;
263268
264269 it ( 'should test with URL via webkitURL' , ( ) => {
265- window . URL = undefined ;
266- window . webkitURL . createObjectURL = ( ) => null ;
270+ const currentWindow = window as unknown as WindowDownloaderEmbedded ;
267271
268- const createObjectWebkitURLSpy = jest . spyOn (
269- window . webkitURL ,
270- 'createObjectURL'
271- ) ;
272+ currentWindow . URL = undefined ;
272273
273- jsDownload ( new Blob ( [ 'abcde' ] ) , 'test' ) ;
274+ if ( currentWindow . webkitURL ) {
275+ currentWindow . webkitURL . createObjectURL = ( ) => null ;
276+
277+ const createObjectWebkitURLSpy = jest . spyOn (
278+ window . webkitURL ,
279+ 'createObjectURL'
280+ ) ;
281+
282+ jsDownload ( new Blob ( [ 'abcde' ] ) , 'test' ) ;
274283
275- expect ( createObjectWebkitURLSpy ) . toHaveBeenCalled ( ) ;
284+ expect ( createObjectWebkitURLSpy ) . toHaveBeenCalled ( ) ;
285+ }
276286 } ) ;
277287 } ) ;
278288} ) ;
0 commit comments