@@ -2,7 +2,7 @@ import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest'
2
2
import { fireEvent , waitFor } from '@testing-library/react'
3
3
import * as React from 'react'
4
4
import { ErrorBoundary } from 'react-error-boundary'
5
- import { keepPreviousData , useQuery } from '..'
5
+ import { QueryErrorResetBoundary , keepPreviousData , useQuery } from '..'
6
6
import { QueryCache } from '../index'
7
7
import { createQueryClient , queryKey , renderWithClient , sleep } from './utils'
8
8
@@ -433,22 +433,27 @@ describe('useQuery().promise', () => {
433
433
434
434
const rendered = renderWithClient (
435
435
queryClient ,
436
- < ErrorBoundary
437
- fallbackRender = { ( props ) => (
438
- < >
439
- error boundary{ ' ' }
440
- < button
441
- onClick = { ( ) => {
442
- props . resetErrorBoundary ( )
443
- } }
444
- >
445
- resetErrorBoundary
446
- </ button >
447
- </ >
436
+ < QueryErrorResetBoundary >
437
+ { ( { reset } ) => (
438
+ < ErrorBoundary
439
+ onReset = { reset }
440
+ fallbackRender = { ( { resetErrorBoundary } ) => (
441
+ < div >
442
+ < div > error boundary</ div >
443
+ < button
444
+ onClick = { ( ) => {
445
+ resetErrorBoundary ( )
446
+ } }
447
+ >
448
+ resetErrorBoundary
449
+ </ button >
450
+ </ div >
451
+ ) }
452
+ >
453
+ < Page />
454
+ </ ErrorBoundary >
448
455
) }
449
- >
450
- < Page />
451
- </ ErrorBoundary > ,
456
+ </ QueryErrorResetBoundary > ,
452
457
)
453
458
454
459
await waitFor ( ( ) => rendered . getByText ( 'loading..' ) )
@@ -464,6 +469,48 @@ describe('useQuery().promise', () => {
464
469
expect ( queryCount ) . toBe ( 2 )
465
470
} )
466
471
472
+ it ( 'should throw error if the promise fails (colocate suspense and promise)' , async ( ) => {
473
+ const consoleMock = vi
474
+ . spyOn ( console , 'error' )
475
+ . mockImplementation ( ( ) => undefined )
476
+
477
+ const key = queryKey ( )
478
+
479
+ function MyComponent ( ) {
480
+ const query = useQuery ( {
481
+ queryKey : key ,
482
+ queryFn : async ( ) => {
483
+ await sleep ( 1 )
484
+ throw new Error ( 'Error test' )
485
+ } ,
486
+ retry : false ,
487
+ } )
488
+ const data = React . use ( query . promise )
489
+
490
+ return < > { data } </ >
491
+ }
492
+
493
+ function Page ( ) {
494
+ return (
495
+ < React . Suspense fallback = "loading.." >
496
+ < MyComponent />
497
+ </ React . Suspense >
498
+ )
499
+ }
500
+
501
+ const rendered = renderWithClient (
502
+ queryClient ,
503
+ < ErrorBoundary fallbackRender = { ( ) => < div > error boundary</ div > } >
504
+ < Page />
505
+ </ ErrorBoundary > ,
506
+ )
507
+
508
+ await waitFor ( ( ) => rendered . getByText ( 'loading..' ) )
509
+ await waitFor ( ( ) => rendered . getByText ( 'error boundary' ) )
510
+
511
+ consoleMock . mockRestore ( )
512
+ } )
513
+
467
514
it ( 'should recreate promise with data changes' , async ( ) => {
468
515
const key = queryKey ( )
469
516
let suspenseRenderCount = 0
0 commit comments