@@ -10,11 +10,15 @@ function mockFetchImpl(input: string | Request, init?: RequestInit): Promise<Res
10
10
( mockFetchImpl as MockFetch ) . calls . push ( { input, init } ) ;
11
11
return new Promise < any > ( ( resolve , reject ) => {
12
12
if ( init . signal ) {
13
+ if ( init . signal . aborted ) {
14
+ reject ( new MockDOMException ( ) ) ;
15
+ return ;
16
+ }
13
17
init . signal . addEventListener ( 'abort' , ( ) => {
14
18
reject ( new MockDOMException ( ) ) ;
15
19
} ) ;
16
20
}
17
- return Promise . resolve ( null ) . then ( ( ) => {
21
+ Promise . resolve ( null ) . then ( ( ) => {
18
22
resolve ( ( mockFetchImpl as any ) . respondWith ) ;
19
23
} ) ;
20
24
} ) ;
@@ -173,13 +177,23 @@ describe('fromFetch', () => {
173
177
} ) ;
174
178
175
179
it ( 'should allow passing of init object' , done => {
176
- const myInit = { } ;
177
- const fetch$ = fromFetch ( '/foo' , myInit ) ;
180
+ const fetch$ = fromFetch ( '/foo' , { method : 'HEAD' } ) ;
181
+ fetch$ . subscribe ( {
182
+ error : done ,
183
+ complete : done ,
184
+ } ) ;
185
+ expect ( mockFetch . calls [ 0 ] . init . method ) . to . equal ( 'HEAD' ) ;
186
+ } ) ;
187
+
188
+ it ( 'should pass in a signal with the init object without mutating the init' , done => {
189
+ const myInit = { method : 'DELETE' } ;
190
+ const fetch$ = fromFetch ( '/bar' , myInit ) ;
178
191
fetch$ . subscribe ( {
179
192
error : done ,
180
193
complete : done ,
181
194
} ) ;
182
- expect ( mockFetch . calls [ 0 ] . init ) . to . equal ( myInit ) ;
195
+ expect ( mockFetch . calls [ 0 ] . init . method ) . to . equal ( myInit . method ) ;
196
+ expect ( mockFetch . calls [ 0 ] . init ) . not . to . equal ( myInit ) ;
183
197
expect ( mockFetch . calls [ 0 ] . init . signal ) . not . to . be . undefined ;
184
198
} ) ;
185
199
@@ -198,4 +212,20 @@ describe('fromFetch', () => {
198
212
// The subscription will not be closed until the error fires when the promise resolves.
199
213
expect ( subscription . closed ) . to . be . false ;
200
214
} ) ;
215
+
216
+ it ( 'should treat passed already aborted signals as a cancellation token which triggers an error' , done => {
217
+ const controller = new MockAbortController ( ) ;
218
+ controller . abort ( ) ;
219
+ const signal = controller . signal as any ;
220
+ const fetch$ = fromFetch ( '/foo' , { signal } ) ;
221
+ const subscription = fetch$ . subscribe ( {
222
+ error : err => {
223
+ expect ( err ) . to . be . instanceof ( MockDOMException ) ;
224
+ done ( ) ;
225
+ }
226
+ } ) ;
227
+ expect ( mockFetch . calls [ 0 ] . init . signal . aborted ) . to . be . true ;
228
+ // The subscription will not be closed until the error fires when the promise resolves.
229
+ expect ( subscription . closed ) . to . be . false ;
230
+ } ) ;
201
231
} ) ;
0 commit comments