33import "jest-dom/extend-expect"
44import React from "react"
55import { render , fireEvent , cleanup , waitForElement } from "@testing-library/react"
6- import { useAsync , useFetch } from "./index"
6+ import { useAsync , useFetch , globalScope } from "./index"
77import {
88 sleep ,
99 resolveTo ,
@@ -17,13 +17,13 @@ import {
1717import "../../../jest.setup"
1818
1919const abortCtrl = { abort : jest . fn ( ) , signal : "SIGNAL" }
20- window . AbortController = jest . fn ( ( ) => abortCtrl )
20+ globalScope . AbortController = jest . fn ( ( ) => abortCtrl )
2121
2222const json = jest . fn ( ( ) => ( { } ) )
23- window . fetch = jest . fn ( ( ) => Promise . resolve ( { ok : true , json } ) )
23+ globalScope . fetch = jest . fn ( ( ) => Promise . resolve ( { ok : true , json } ) )
2424
2525beforeEach ( abortCtrl . abort . mockClear )
26- beforeEach ( window . fetch . mockClear )
26+ beforeEach ( globalScope . fetch . mockClear )
2727beforeEach ( json . mockClear )
2828afterEach ( cleanup )
2929
@@ -89,7 +89,7 @@ describe("useAsync", () => {
8989describe ( "useFetch" , ( ) => {
9090 test ( "sets up a fetch request" , ( ) => {
9191 render ( < Fetch input = "/test" /> )
92- expect ( window . fetch ) . toHaveBeenCalledWith (
92+ expect ( globalScope . fetch ) . toHaveBeenCalledWith (
9393 "/test" ,
9494 expect . objectContaining ( { signal : abortCtrl . signal } )
9595 )
@@ -103,9 +103,9 @@ describe("useFetch", () => {
103103 </ Fetch >
104104 )
105105 const { getByText } = render ( component )
106- expect ( window . fetch ) . not . toHaveBeenCalled ( )
106+ expect ( globalScope . fetch ) . not . toHaveBeenCalled ( )
107107 fireEvent . click ( getByText ( "run" ) )
108- expect ( window . fetch ) . toHaveBeenCalledWith (
108+ expect ( globalScope . fetch ) . toHaveBeenCalledWith (
109109 "/test" ,
110110 expect . objectContaining ( { method : "POST" , signal : abortCtrl . signal } )
111111 )
@@ -118,9 +118,9 @@ describe("useFetch", () => {
118118 </ Fetch >
119119 )
120120 const { getByText } = render ( component )
121- expect ( window . fetch ) . not . toHaveBeenCalled ( )
121+ expect ( globalScope . fetch ) . not . toHaveBeenCalled ( )
122122 fireEvent . click ( getByText ( "run" ) )
123- expect ( window . fetch ) . toHaveBeenCalledWith (
123+ expect ( globalScope . fetch ) . toHaveBeenCalledWith (
124124 "/test" ,
125125 expect . objectContaining ( { signal : abortCtrl . signal } )
126126 )
@@ -132,7 +132,7 @@ describe("useFetch", () => {
132132 { ( { run } ) => < button onClick = { run } > run</ button > }
133133 </ Fetch >
134134 )
135- expect ( window . fetch ) . toHaveBeenCalledWith (
135+ expect ( globalScope . fetch ) . toHaveBeenCalledWith (
136136 "/test" ,
137137 expect . objectContaining ( { method : "POST" , signal : abortCtrl . signal } )
138138 )
0 commit comments