@tanstack/react-query 5.59.12 → 5.59.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,13 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
afterAll,
|
|
3
|
-
beforeAll,
|
|
4
|
-
describe,
|
|
5
|
-
expect,
|
|
6
|
-
expectTypeOf,
|
|
7
|
-
it,
|
|
8
|
-
test,
|
|
9
|
-
vi,
|
|
10
|
-
} from 'vitest'
|
|
1
|
+
import { describe, expect, expectTypeOf, it, test, vi } from 'vitest'
|
|
11
2
|
import { act, fireEvent, render, waitFor } from '@testing-library/react'
|
|
12
3
|
import * as React from 'react'
|
|
13
4
|
import { ErrorBoundary } from 'react-error-boundary'
|
|
@@ -6643,834 +6634,4 @@ describe('useQuery', () => {
|
|
|
6643
6634
|
|
|
6644
6635
|
consoleMock.mockRestore()
|
|
6645
6636
|
})
|
|
6646
|
-
|
|
6647
|
-
describe('useQuery().promise', () => {
|
|
6648
|
-
beforeAll(() => {
|
|
6649
|
-
queryClient.setDefaultOptions({
|
|
6650
|
-
queries: { experimental_prefetchInRender: true },
|
|
6651
|
-
})
|
|
6652
|
-
})
|
|
6653
|
-
afterAll(() => {
|
|
6654
|
-
queryClient.setDefaultOptions({
|
|
6655
|
-
queries: { experimental_prefetchInRender: false },
|
|
6656
|
-
})
|
|
6657
|
-
})
|
|
6658
|
-
it('should work with a basic test', async () => {
|
|
6659
|
-
const key = queryKey()
|
|
6660
|
-
let suspenseRenderCount = 0
|
|
6661
|
-
let pageRenderCount = 0
|
|
6662
|
-
|
|
6663
|
-
function MyComponent(props: { promise: Promise<string> }) {
|
|
6664
|
-
const data = React.use(props.promise)
|
|
6665
|
-
|
|
6666
|
-
return <>{data}</>
|
|
6667
|
-
}
|
|
6668
|
-
|
|
6669
|
-
function Loading() {
|
|
6670
|
-
suspenseRenderCount++
|
|
6671
|
-
return <>loading..</>
|
|
6672
|
-
}
|
|
6673
|
-
function Page() {
|
|
6674
|
-
const query = useQuery({
|
|
6675
|
-
queryKey: key,
|
|
6676
|
-
queryFn: async () => {
|
|
6677
|
-
await sleep(1)
|
|
6678
|
-
return 'test'
|
|
6679
|
-
},
|
|
6680
|
-
})
|
|
6681
|
-
|
|
6682
|
-
pageRenderCount++
|
|
6683
|
-
return (
|
|
6684
|
-
<React.Suspense fallback={<Loading />}>
|
|
6685
|
-
<MyComponent promise={query.promise} />
|
|
6686
|
-
</React.Suspense>
|
|
6687
|
-
)
|
|
6688
|
-
}
|
|
6689
|
-
|
|
6690
|
-
const rendered = renderWithClient(queryClient, <Page />)
|
|
6691
|
-
await waitFor(() => rendered.getByText('loading..'))
|
|
6692
|
-
await waitFor(() => rendered.getByText('test'))
|
|
6693
|
-
|
|
6694
|
-
// Suspense should rendered once since `.promise` is the only watched property
|
|
6695
|
-
expect(suspenseRenderCount).toBe(1)
|
|
6696
|
-
|
|
6697
|
-
// Page should be rendered once since since the promise do not change
|
|
6698
|
-
expect(pageRenderCount).toBe(1)
|
|
6699
|
-
})
|
|
6700
|
-
|
|
6701
|
-
it('colocate suspense and promise', async () => {
|
|
6702
|
-
const key = queryKey()
|
|
6703
|
-
let suspenseRenderCount = 0
|
|
6704
|
-
let pageRenderCount = 0
|
|
6705
|
-
let callCount = 0
|
|
6706
|
-
|
|
6707
|
-
function MyComponent() {
|
|
6708
|
-
const query = useQuery({
|
|
6709
|
-
queryKey: key,
|
|
6710
|
-
queryFn: async () => {
|
|
6711
|
-
callCount++
|
|
6712
|
-
await sleep(1)
|
|
6713
|
-
return 'test'
|
|
6714
|
-
},
|
|
6715
|
-
staleTime: 1000,
|
|
6716
|
-
})
|
|
6717
|
-
const data = React.use(query.promise)
|
|
6718
|
-
|
|
6719
|
-
return <>{data}</>
|
|
6720
|
-
}
|
|
6721
|
-
|
|
6722
|
-
function Loading() {
|
|
6723
|
-
suspenseRenderCount++
|
|
6724
|
-
return <>loading..</>
|
|
6725
|
-
}
|
|
6726
|
-
function Page() {
|
|
6727
|
-
pageRenderCount++
|
|
6728
|
-
return (
|
|
6729
|
-
<React.Suspense fallback={<Loading />}>
|
|
6730
|
-
<MyComponent />
|
|
6731
|
-
</React.Suspense>
|
|
6732
|
-
)
|
|
6733
|
-
}
|
|
6734
|
-
|
|
6735
|
-
const rendered = renderWithClient(queryClient, <Page />)
|
|
6736
|
-
await waitFor(() => rendered.getByText('loading..'))
|
|
6737
|
-
await waitFor(() => rendered.getByText('test'))
|
|
6738
|
-
|
|
6739
|
-
// Suspense should rendered once since `.promise` is the only watched property
|
|
6740
|
-
expect(suspenseRenderCount).toBe(1)
|
|
6741
|
-
|
|
6742
|
-
// Page should be rendered once since since the promise do not change
|
|
6743
|
-
expect(pageRenderCount).toBe(1)
|
|
6744
|
-
|
|
6745
|
-
expect(callCount).toBe(1)
|
|
6746
|
-
})
|
|
6747
|
-
|
|
6748
|
-
it('parallel queries', async () => {
|
|
6749
|
-
const key = queryKey()
|
|
6750
|
-
let suspenseRenderCount = 0
|
|
6751
|
-
let pageRenderCount = 0
|
|
6752
|
-
let callCount = 0
|
|
6753
|
-
|
|
6754
|
-
function MyComponent() {
|
|
6755
|
-
const query = useQuery({
|
|
6756
|
-
queryKey: key,
|
|
6757
|
-
queryFn: async () => {
|
|
6758
|
-
callCount++
|
|
6759
|
-
await sleep(1)
|
|
6760
|
-
return 'test'
|
|
6761
|
-
},
|
|
6762
|
-
staleTime: 1000,
|
|
6763
|
-
})
|
|
6764
|
-
const data = React.use(query.promise)
|
|
6765
|
-
|
|
6766
|
-
return data
|
|
6767
|
-
}
|
|
6768
|
-
|
|
6769
|
-
function Loading() {
|
|
6770
|
-
suspenseRenderCount++
|
|
6771
|
-
return <>loading..</>
|
|
6772
|
-
}
|
|
6773
|
-
function Page() {
|
|
6774
|
-
pageRenderCount++
|
|
6775
|
-
return (
|
|
6776
|
-
<>
|
|
6777
|
-
<React.Suspense fallback={<Loading />}>
|
|
6778
|
-
<MyComponent />
|
|
6779
|
-
<MyComponent />
|
|
6780
|
-
<MyComponent />
|
|
6781
|
-
</React.Suspense>
|
|
6782
|
-
<React.Suspense fallback={null}>
|
|
6783
|
-
<MyComponent />
|
|
6784
|
-
<MyComponent />
|
|
6785
|
-
</React.Suspense>
|
|
6786
|
-
</>
|
|
6787
|
-
)
|
|
6788
|
-
}
|
|
6789
|
-
|
|
6790
|
-
const rendered = renderWithClient(queryClient, <Page />)
|
|
6791
|
-
await waitFor(() => rendered.getByText('loading..'))
|
|
6792
|
-
await waitFor(() => {
|
|
6793
|
-
expect(rendered.queryByText('loading..')).not.toBeInTheDocument()
|
|
6794
|
-
})
|
|
6795
|
-
|
|
6796
|
-
expect(rendered.container.textContent).toBe('test'.repeat(5))
|
|
6797
|
-
|
|
6798
|
-
// Suspense should rendered once since `.promise` is the only watched property
|
|
6799
|
-
expect(suspenseRenderCount).toBe(1)
|
|
6800
|
-
|
|
6801
|
-
// Page should be rendered once since since the promise do not change
|
|
6802
|
-
expect(pageRenderCount).toBe(1)
|
|
6803
|
-
|
|
6804
|
-
expect(callCount).toBe(1)
|
|
6805
|
-
})
|
|
6806
|
-
|
|
6807
|
-
it('should work with initial data', async () => {
|
|
6808
|
-
const key = queryKey()
|
|
6809
|
-
let suspenseRenderCount = 0
|
|
6810
|
-
let pageRenderCount = 0
|
|
6811
|
-
|
|
6812
|
-
function MyComponent(props: { promise: Promise<string> }) {
|
|
6813
|
-
const data = React.use(props.promise)
|
|
6814
|
-
|
|
6815
|
-
return <>{data}</>
|
|
6816
|
-
}
|
|
6817
|
-
function Loading() {
|
|
6818
|
-
suspenseRenderCount++
|
|
6819
|
-
|
|
6820
|
-
return <>loading..</>
|
|
6821
|
-
}
|
|
6822
|
-
function Page() {
|
|
6823
|
-
const query = useQuery({
|
|
6824
|
-
queryKey: key,
|
|
6825
|
-
queryFn: async () => {
|
|
6826
|
-
await sleep(1)
|
|
6827
|
-
return 'test'
|
|
6828
|
-
},
|
|
6829
|
-
initialData: 'initial',
|
|
6830
|
-
})
|
|
6831
|
-
pageRenderCount++
|
|
6832
|
-
|
|
6833
|
-
return (
|
|
6834
|
-
<React.Suspense fallback={<Loading />}>
|
|
6835
|
-
<MyComponent promise={query.promise} />
|
|
6836
|
-
</React.Suspense>
|
|
6837
|
-
)
|
|
6838
|
-
}
|
|
6839
|
-
|
|
6840
|
-
const rendered = renderWithClient(queryClient, <Page />)
|
|
6841
|
-
await waitFor(() => rendered.getByText('initial'))
|
|
6842
|
-
await waitFor(() => rendered.getByText('test'))
|
|
6843
|
-
|
|
6844
|
-
// Suspense boundary should never be rendered since it has data immediately
|
|
6845
|
-
expect(suspenseRenderCount).toBe(0)
|
|
6846
|
-
// Page should only be rendered twice since, the promise will get swapped out when new result comes in
|
|
6847
|
-
expect(pageRenderCount).toBe(2)
|
|
6848
|
-
})
|
|
6849
|
-
|
|
6850
|
-
it('should not fetch with initial data and staleTime', async () => {
|
|
6851
|
-
const key = queryKey()
|
|
6852
|
-
let suspenseRenderCount = 0
|
|
6853
|
-
const queryFn = vi.fn().mockImplementation(async () => {
|
|
6854
|
-
await sleep(1)
|
|
6855
|
-
return 'test'
|
|
6856
|
-
})
|
|
6857
|
-
|
|
6858
|
-
function MyComponent(props: { promise: Promise<string> }) {
|
|
6859
|
-
const data = React.use(props.promise)
|
|
6860
|
-
|
|
6861
|
-
return <>{data}</>
|
|
6862
|
-
}
|
|
6863
|
-
function Loading() {
|
|
6864
|
-
suspenseRenderCount++
|
|
6865
|
-
|
|
6866
|
-
return <>loading..</>
|
|
6867
|
-
}
|
|
6868
|
-
function Page() {
|
|
6869
|
-
const query = useQuery({
|
|
6870
|
-
queryKey: key,
|
|
6871
|
-
queryFn,
|
|
6872
|
-
initialData: 'initial',
|
|
6873
|
-
staleTime: 1000,
|
|
6874
|
-
})
|
|
6875
|
-
|
|
6876
|
-
return (
|
|
6877
|
-
<React.Suspense fallback={<Loading />}>
|
|
6878
|
-
<MyComponent promise={query.promise} />
|
|
6879
|
-
</React.Suspense>
|
|
6880
|
-
)
|
|
6881
|
-
}
|
|
6882
|
-
|
|
6883
|
-
const rendered = renderWithClient(queryClient, <Page />)
|
|
6884
|
-
await waitFor(() => rendered.getByText('initial'))
|
|
6885
|
-
|
|
6886
|
-
// Suspense boundary should never be rendered since it has data immediately
|
|
6887
|
-
expect(suspenseRenderCount).toBe(0)
|
|
6888
|
-
// should not call queryFn because of staleTime + initialData combo
|
|
6889
|
-
expect(queryFn).toHaveBeenCalledTimes(0)
|
|
6890
|
-
})
|
|
6891
|
-
|
|
6892
|
-
it('should work with static placeholderData', async () => {
|
|
6893
|
-
const key = queryKey()
|
|
6894
|
-
let suspenseRenderCount = 0
|
|
6895
|
-
let pageRenderCount = 0
|
|
6896
|
-
|
|
6897
|
-
function MyComponent(props: { promise: Promise<string> }) {
|
|
6898
|
-
const data = React.use(props.promise)
|
|
6899
|
-
|
|
6900
|
-
return <>{data}</>
|
|
6901
|
-
}
|
|
6902
|
-
function Loading() {
|
|
6903
|
-
suspenseRenderCount++
|
|
6904
|
-
|
|
6905
|
-
return <>loading..</>
|
|
6906
|
-
}
|
|
6907
|
-
function Page() {
|
|
6908
|
-
const query = useQuery({
|
|
6909
|
-
queryKey: key,
|
|
6910
|
-
queryFn: async () => {
|
|
6911
|
-
await sleep(1)
|
|
6912
|
-
return 'test'
|
|
6913
|
-
},
|
|
6914
|
-
placeholderData: 'placeholder',
|
|
6915
|
-
})
|
|
6916
|
-
pageRenderCount++
|
|
6917
|
-
|
|
6918
|
-
return (
|
|
6919
|
-
<React.Suspense fallback={<Loading />}>
|
|
6920
|
-
<MyComponent promise={query.promise} />
|
|
6921
|
-
</React.Suspense>
|
|
6922
|
-
)
|
|
6923
|
-
}
|
|
6924
|
-
|
|
6925
|
-
const rendered = renderWithClient(queryClient, <Page />)
|
|
6926
|
-
await waitFor(() => rendered.getByText('placeholder'))
|
|
6927
|
-
await waitFor(() => rendered.getByText('test'))
|
|
6928
|
-
|
|
6929
|
-
// Suspense boundary should never be rendered since it has data immediately
|
|
6930
|
-
expect(suspenseRenderCount).toBe(0)
|
|
6931
|
-
// Page should only be rendered twice since, the promise will get swapped out when new result comes in
|
|
6932
|
-
expect(pageRenderCount).toBe(2)
|
|
6933
|
-
})
|
|
6934
|
-
|
|
6935
|
-
it('should work with placeholderData: keepPreviousData', async () => {
|
|
6936
|
-
const key = queryKey()
|
|
6937
|
-
let suspenseRenderCount = 0
|
|
6938
|
-
|
|
6939
|
-
function MyComponent(props: { promise: Promise<string> }) {
|
|
6940
|
-
const data = React.use(props.promise)
|
|
6941
|
-
|
|
6942
|
-
return <>{data}</>
|
|
6943
|
-
}
|
|
6944
|
-
function Loading() {
|
|
6945
|
-
suspenseRenderCount++
|
|
6946
|
-
|
|
6947
|
-
return <>loading..</>
|
|
6948
|
-
}
|
|
6949
|
-
function Page() {
|
|
6950
|
-
const [count, setCount] = React.useState(0)
|
|
6951
|
-
const query = useQuery({
|
|
6952
|
-
queryKey: [...key, count],
|
|
6953
|
-
queryFn: async () => {
|
|
6954
|
-
await sleep(1)
|
|
6955
|
-
return 'test-' + count
|
|
6956
|
-
},
|
|
6957
|
-
placeholderData: keepPreviousData,
|
|
6958
|
-
})
|
|
6959
|
-
|
|
6960
|
-
return (
|
|
6961
|
-
<div>
|
|
6962
|
-
<React.Suspense fallback={<Loading />}>
|
|
6963
|
-
<MyComponent promise={query.promise} />
|
|
6964
|
-
</React.Suspense>
|
|
6965
|
-
<button onClick={() => setCount((c) => c + 1)}>increment</button>
|
|
6966
|
-
</div>
|
|
6967
|
-
)
|
|
6968
|
-
}
|
|
6969
|
-
|
|
6970
|
-
const rendered = renderWithClient(queryClient, <Page />)
|
|
6971
|
-
await waitFor(() => rendered.getByText('loading..'))
|
|
6972
|
-
await waitFor(() => rendered.getByText('test-0'))
|
|
6973
|
-
|
|
6974
|
-
// Suspense boundary should only be rendered initially
|
|
6975
|
-
expect(suspenseRenderCount).toBe(1)
|
|
6976
|
-
|
|
6977
|
-
fireEvent.click(rendered.getByRole('button', { name: 'increment' }))
|
|
6978
|
-
|
|
6979
|
-
await waitFor(() => rendered.getByText('test-1'))
|
|
6980
|
-
|
|
6981
|
-
// no more suspense boundary rendering
|
|
6982
|
-
expect(suspenseRenderCount).toBe(1)
|
|
6983
|
-
})
|
|
6984
|
-
|
|
6985
|
-
it('should be possible to select a part of the data with select', async () => {
|
|
6986
|
-
const key = queryKey()
|
|
6987
|
-
let suspenseRenderCount = 0
|
|
6988
|
-
let pageRenderCount = 0
|
|
6989
|
-
|
|
6990
|
-
function MyComponent(props: { promise: Promise<string> }) {
|
|
6991
|
-
const data = React.use(props.promise)
|
|
6992
|
-
return <>{data}</>
|
|
6993
|
-
}
|
|
6994
|
-
|
|
6995
|
-
function Loading() {
|
|
6996
|
-
suspenseRenderCount++
|
|
6997
|
-
return <>loading..</>
|
|
6998
|
-
}
|
|
6999
|
-
|
|
7000
|
-
function Page() {
|
|
7001
|
-
const query = useQuery({
|
|
7002
|
-
queryKey: key,
|
|
7003
|
-
queryFn: async () => {
|
|
7004
|
-
await sleep(1)
|
|
7005
|
-
return { name: 'test' }
|
|
7006
|
-
},
|
|
7007
|
-
select: (data) => data.name,
|
|
7008
|
-
})
|
|
7009
|
-
|
|
7010
|
-
pageRenderCount++
|
|
7011
|
-
return (
|
|
7012
|
-
<React.Suspense fallback={<Loading />}>
|
|
7013
|
-
<MyComponent promise={query.promise} />
|
|
7014
|
-
</React.Suspense>
|
|
7015
|
-
)
|
|
7016
|
-
}
|
|
7017
|
-
|
|
7018
|
-
const rendered = renderWithClient(queryClient, <Page />)
|
|
7019
|
-
|
|
7020
|
-
await waitFor(() => {
|
|
7021
|
-
rendered.getByText('test')
|
|
7022
|
-
})
|
|
7023
|
-
expect(suspenseRenderCount).toBe(1)
|
|
7024
|
-
expect(pageRenderCount).toBe(1)
|
|
7025
|
-
})
|
|
7026
|
-
|
|
7027
|
-
it('should throw error if the promise fails', async () => {
|
|
7028
|
-
let suspenseRenderCount = 0
|
|
7029
|
-
const consoleMock = vi
|
|
7030
|
-
.spyOn(console, 'error')
|
|
7031
|
-
.mockImplementation(() => undefined)
|
|
7032
|
-
|
|
7033
|
-
const key = queryKey()
|
|
7034
|
-
function MyComponent(props: { promise: Promise<string> }) {
|
|
7035
|
-
const data = React.use(props.promise)
|
|
7036
|
-
|
|
7037
|
-
return <>{data}</>
|
|
7038
|
-
}
|
|
7039
|
-
|
|
7040
|
-
function Loading() {
|
|
7041
|
-
suspenseRenderCount++
|
|
7042
|
-
return <>loading..</>
|
|
7043
|
-
}
|
|
7044
|
-
|
|
7045
|
-
let queryCount = 0
|
|
7046
|
-
function Page() {
|
|
7047
|
-
const query = useQuery({
|
|
7048
|
-
queryKey: key,
|
|
7049
|
-
queryFn: async () => {
|
|
7050
|
-
await sleep(1)
|
|
7051
|
-
if (++queryCount > 1) {
|
|
7052
|
-
// second time this query mounts, it should not throw
|
|
7053
|
-
return 'data'
|
|
7054
|
-
}
|
|
7055
|
-
throw new Error('Error test')
|
|
7056
|
-
},
|
|
7057
|
-
retry: false,
|
|
7058
|
-
})
|
|
7059
|
-
|
|
7060
|
-
return (
|
|
7061
|
-
<React.Suspense fallback={<Loading />}>
|
|
7062
|
-
<MyComponent promise={query.promise} />
|
|
7063
|
-
</React.Suspense>
|
|
7064
|
-
)
|
|
7065
|
-
}
|
|
7066
|
-
|
|
7067
|
-
const rendered = renderWithClient(
|
|
7068
|
-
queryClient,
|
|
7069
|
-
<ErrorBoundary
|
|
7070
|
-
fallbackRender={(props) => (
|
|
7071
|
-
<>
|
|
7072
|
-
error boundary{' '}
|
|
7073
|
-
<button
|
|
7074
|
-
onClick={() => {
|
|
7075
|
-
props.resetErrorBoundary()
|
|
7076
|
-
}}
|
|
7077
|
-
>
|
|
7078
|
-
resetErrorBoundary
|
|
7079
|
-
</button>
|
|
7080
|
-
</>
|
|
7081
|
-
)}
|
|
7082
|
-
>
|
|
7083
|
-
<Page />
|
|
7084
|
-
</ErrorBoundary>,
|
|
7085
|
-
)
|
|
7086
|
-
|
|
7087
|
-
await waitFor(() => rendered.getByText('loading..'))
|
|
7088
|
-
await waitFor(() => rendered.getByText('error boundary'))
|
|
7089
|
-
|
|
7090
|
-
consoleMock.mockRestore()
|
|
7091
|
-
|
|
7092
|
-
fireEvent.click(rendered.getByText('resetErrorBoundary'))
|
|
7093
|
-
|
|
7094
|
-
await waitFor(() => rendered.getByText('loading..'))
|
|
7095
|
-
await waitFor(() => rendered.getByText('data'))
|
|
7096
|
-
|
|
7097
|
-
expect(queryCount).toBe(2)
|
|
7098
|
-
})
|
|
7099
|
-
|
|
7100
|
-
it('should recreate promise with data changes', async () => {
|
|
7101
|
-
const key = queryKey()
|
|
7102
|
-
let suspenseRenderCount = 0
|
|
7103
|
-
let pageRenderCount = 0
|
|
7104
|
-
|
|
7105
|
-
function MyComponent(props: { promise: Promise<string> }) {
|
|
7106
|
-
const data = React.use(props.promise)
|
|
7107
|
-
|
|
7108
|
-
return <>{data}</>
|
|
7109
|
-
}
|
|
7110
|
-
|
|
7111
|
-
function Loading() {
|
|
7112
|
-
suspenseRenderCount++
|
|
7113
|
-
return <>loading..</>
|
|
7114
|
-
}
|
|
7115
|
-
function Page() {
|
|
7116
|
-
const query = useQuery({
|
|
7117
|
-
queryKey: key,
|
|
7118
|
-
queryFn: async () => {
|
|
7119
|
-
await sleep(1)
|
|
7120
|
-
return 'test1'
|
|
7121
|
-
},
|
|
7122
|
-
})
|
|
7123
|
-
|
|
7124
|
-
pageRenderCount++
|
|
7125
|
-
return (
|
|
7126
|
-
<React.Suspense fallback={<Loading />}>
|
|
7127
|
-
<MyComponent promise={query.promise} />
|
|
7128
|
-
</React.Suspense>
|
|
7129
|
-
)
|
|
7130
|
-
}
|
|
7131
|
-
|
|
7132
|
-
const rendered = renderWithClient(queryClient, <Page />)
|
|
7133
|
-
await waitFor(() => rendered.getByText('loading..'))
|
|
7134
|
-
await waitFor(() => rendered.getByText('test1'))
|
|
7135
|
-
|
|
7136
|
-
// Suspense should rendered once since `.promise` is the only watched property
|
|
7137
|
-
expect(pageRenderCount).toBe(1)
|
|
7138
|
-
|
|
7139
|
-
queryClient.setQueryData(key, 'test2')
|
|
7140
|
-
|
|
7141
|
-
await waitFor(() => rendered.getByText('test2'))
|
|
7142
|
-
|
|
7143
|
-
// Suspense should rendered once since `.promise` is the only watched property
|
|
7144
|
-
expect(suspenseRenderCount).toBe(1)
|
|
7145
|
-
|
|
7146
|
-
// Page should be rendered once since since the promise changed once
|
|
7147
|
-
expect(pageRenderCount).toBe(2)
|
|
7148
|
-
})
|
|
7149
|
-
|
|
7150
|
-
it('should dedupe when re-fetched with queryClient.fetchQuery while suspending', async () => {
|
|
7151
|
-
const key = queryKey()
|
|
7152
|
-
const queryFn = vi.fn().mockImplementation(async () => {
|
|
7153
|
-
await sleep(10)
|
|
7154
|
-
return 'test'
|
|
7155
|
-
})
|
|
7156
|
-
|
|
7157
|
-
const options = {
|
|
7158
|
-
queryKey: key,
|
|
7159
|
-
queryFn,
|
|
7160
|
-
}
|
|
7161
|
-
|
|
7162
|
-
function MyComponent(props: { promise: Promise<string> }) {
|
|
7163
|
-
const data = React.use(props.promise)
|
|
7164
|
-
|
|
7165
|
-
return <>{data}</>
|
|
7166
|
-
}
|
|
7167
|
-
|
|
7168
|
-
function Loading() {
|
|
7169
|
-
return <>loading..</>
|
|
7170
|
-
}
|
|
7171
|
-
function Page() {
|
|
7172
|
-
const query = useQuery(options)
|
|
7173
|
-
|
|
7174
|
-
return (
|
|
7175
|
-
<div>
|
|
7176
|
-
<React.Suspense fallback={<Loading />}>
|
|
7177
|
-
<MyComponent promise={query.promise} />
|
|
7178
|
-
</React.Suspense>
|
|
7179
|
-
<button onClick={() => queryClient.fetchQuery(options)}>
|
|
7180
|
-
fetch
|
|
7181
|
-
</button>
|
|
7182
|
-
</div>
|
|
7183
|
-
)
|
|
7184
|
-
}
|
|
7185
|
-
|
|
7186
|
-
const rendered = renderWithClient(queryClient, <Page />)
|
|
7187
|
-
fireEvent.click(rendered.getByText('fetch'))
|
|
7188
|
-
await waitFor(() => rendered.getByText('loading..'))
|
|
7189
|
-
await waitFor(() => rendered.getByText('test'))
|
|
7190
|
-
|
|
7191
|
-
expect(queryFn).toHaveBeenCalledOnce()
|
|
7192
|
-
})
|
|
7193
|
-
|
|
7194
|
-
it('should dedupe when re-fetched with refetchQueries while suspending', async () => {
|
|
7195
|
-
const key = queryKey()
|
|
7196
|
-
let count = 0
|
|
7197
|
-
const queryFn = vi.fn().mockImplementation(async () => {
|
|
7198
|
-
await sleep(10)
|
|
7199
|
-
return 'test' + count++
|
|
7200
|
-
})
|
|
7201
|
-
|
|
7202
|
-
const options = {
|
|
7203
|
-
queryKey: key,
|
|
7204
|
-
queryFn,
|
|
7205
|
-
}
|
|
7206
|
-
|
|
7207
|
-
function MyComponent(props: { promise: Promise<string> }) {
|
|
7208
|
-
const data = React.use(props.promise)
|
|
7209
|
-
|
|
7210
|
-
return <>{data}</>
|
|
7211
|
-
}
|
|
7212
|
-
|
|
7213
|
-
function Loading() {
|
|
7214
|
-
return <>loading..</>
|
|
7215
|
-
}
|
|
7216
|
-
function Page() {
|
|
7217
|
-
const query = useQuery(options)
|
|
7218
|
-
|
|
7219
|
-
return (
|
|
7220
|
-
<div>
|
|
7221
|
-
<React.Suspense fallback={<Loading />}>
|
|
7222
|
-
<MyComponent promise={query.promise} />
|
|
7223
|
-
</React.Suspense>
|
|
7224
|
-
<button onClick={() => queryClient.refetchQueries(options)}>
|
|
7225
|
-
refetch
|
|
7226
|
-
</button>
|
|
7227
|
-
</div>
|
|
7228
|
-
)
|
|
7229
|
-
}
|
|
7230
|
-
|
|
7231
|
-
const rendered = renderWithClient(queryClient, <Page />)
|
|
7232
|
-
fireEvent.click(rendered.getByText('refetch'))
|
|
7233
|
-
await waitFor(() => rendered.getByText('loading..'))
|
|
7234
|
-
await waitFor(() => rendered.getByText('test0'))
|
|
7235
|
-
|
|
7236
|
-
expect(queryFn).toHaveBeenCalledOnce()
|
|
7237
|
-
})
|
|
7238
|
-
|
|
7239
|
-
it('should stay pending when canceled with cancelQueries while suspending until refetched', async () => {
|
|
7240
|
-
const key = queryKey()
|
|
7241
|
-
let count = 0
|
|
7242
|
-
const queryFn = vi.fn().mockImplementation(async () => {
|
|
7243
|
-
await sleep(10)
|
|
7244
|
-
return 'test' + count++
|
|
7245
|
-
})
|
|
7246
|
-
|
|
7247
|
-
const options = {
|
|
7248
|
-
queryKey: key,
|
|
7249
|
-
queryFn,
|
|
7250
|
-
}
|
|
7251
|
-
|
|
7252
|
-
function MyComponent(props: { promise: Promise<string> }) {
|
|
7253
|
-
const data = React.use(props.promise)
|
|
7254
|
-
|
|
7255
|
-
return <>{data}</>
|
|
7256
|
-
}
|
|
7257
|
-
|
|
7258
|
-
function Loading() {
|
|
7259
|
-
return <>loading..</>
|
|
7260
|
-
}
|
|
7261
|
-
function Page() {
|
|
7262
|
-
const query = useQuery(options)
|
|
7263
|
-
|
|
7264
|
-
return (
|
|
7265
|
-
<div>
|
|
7266
|
-
<React.Suspense fallback={<Loading />}>
|
|
7267
|
-
<MyComponent promise={query.promise} />
|
|
7268
|
-
</React.Suspense>
|
|
7269
|
-
<button onClick={() => queryClient.cancelQueries(options)}>
|
|
7270
|
-
cancel
|
|
7271
|
-
</button>
|
|
7272
|
-
<button
|
|
7273
|
-
onClick={() => queryClient.setQueryData<string>(key, 'hello')}
|
|
7274
|
-
>
|
|
7275
|
-
fetch
|
|
7276
|
-
</button>
|
|
7277
|
-
</div>
|
|
7278
|
-
)
|
|
7279
|
-
}
|
|
7280
|
-
|
|
7281
|
-
const rendered = renderWithClient(
|
|
7282
|
-
queryClient,
|
|
7283
|
-
<ErrorBoundary fallbackRender={() => <>error boundary</>}>
|
|
7284
|
-
<Page />
|
|
7285
|
-
</ErrorBoundary>,
|
|
7286
|
-
)
|
|
7287
|
-
fireEvent.click(rendered.getByText('cancel'))
|
|
7288
|
-
await waitFor(() => rendered.getByText('loading..'))
|
|
7289
|
-
// await waitFor(() => rendered.getByText('error boundary'))
|
|
7290
|
-
await waitFor(() =>
|
|
7291
|
-
expect(queryClient.getQueryState(key)).toMatchObject({
|
|
7292
|
-
status: 'pending',
|
|
7293
|
-
fetchStatus: 'idle',
|
|
7294
|
-
}),
|
|
7295
|
-
)
|
|
7296
|
-
|
|
7297
|
-
expect(queryFn).toHaveBeenCalledOnce()
|
|
7298
|
-
|
|
7299
|
-
fireEvent.click(rendered.getByText('fetch'))
|
|
7300
|
-
|
|
7301
|
-
await waitFor(() => rendered.getByText('hello'))
|
|
7302
|
-
})
|
|
7303
|
-
|
|
7304
|
-
it('should resolve to previous data when canceled with cancelQueries while suspending', async () => {
|
|
7305
|
-
const key = queryKey()
|
|
7306
|
-
const queryFn = vi.fn().mockImplementation(async () => {
|
|
7307
|
-
await sleep(10)
|
|
7308
|
-
return 'test'
|
|
7309
|
-
})
|
|
7310
|
-
|
|
7311
|
-
const options = {
|
|
7312
|
-
queryKey: key,
|
|
7313
|
-
queryFn,
|
|
7314
|
-
}
|
|
7315
|
-
|
|
7316
|
-
function MyComponent(props: { promise: Promise<string> }) {
|
|
7317
|
-
const data = React.use(props.promise)
|
|
7318
|
-
|
|
7319
|
-
return <>{data}</>
|
|
7320
|
-
}
|
|
7321
|
-
|
|
7322
|
-
function Loading() {
|
|
7323
|
-
return <>loading..</>
|
|
7324
|
-
}
|
|
7325
|
-
function Page() {
|
|
7326
|
-
const query = useQuery(options)
|
|
7327
|
-
|
|
7328
|
-
return (
|
|
7329
|
-
<div>
|
|
7330
|
-
<React.Suspense fallback={<Loading />}>
|
|
7331
|
-
<MyComponent promise={query.promise} />
|
|
7332
|
-
</React.Suspense>
|
|
7333
|
-
<button onClick={() => queryClient.cancelQueries(options)}>
|
|
7334
|
-
cancel
|
|
7335
|
-
</button>
|
|
7336
|
-
</div>
|
|
7337
|
-
)
|
|
7338
|
-
}
|
|
7339
|
-
|
|
7340
|
-
queryClient.setQueryData(key, 'initial')
|
|
7341
|
-
|
|
7342
|
-
const rendered = renderWithClient(queryClient, <Page />)
|
|
7343
|
-
fireEvent.click(rendered.getByText('cancel'))
|
|
7344
|
-
await waitFor(() => rendered.getByText('initial'))
|
|
7345
|
-
|
|
7346
|
-
expect(queryFn).toHaveBeenCalledTimes(1)
|
|
7347
|
-
})
|
|
7348
|
-
|
|
7349
|
-
it('should suspend when not enabled', async () => {
|
|
7350
|
-
const key = queryKey()
|
|
7351
|
-
|
|
7352
|
-
const options = (count: number) => ({
|
|
7353
|
-
queryKey: [...key, count],
|
|
7354
|
-
queryFn: async () => {
|
|
7355
|
-
await sleep(10)
|
|
7356
|
-
return 'test' + count
|
|
7357
|
-
},
|
|
7358
|
-
})
|
|
7359
|
-
|
|
7360
|
-
function MyComponent(props: { promise: Promise<string> }) {
|
|
7361
|
-
const data = React.use(props.promise)
|
|
7362
|
-
|
|
7363
|
-
return <>{data}</>
|
|
7364
|
-
}
|
|
7365
|
-
|
|
7366
|
-
function Loading() {
|
|
7367
|
-
return <>loading..</>
|
|
7368
|
-
}
|
|
7369
|
-
function Page() {
|
|
7370
|
-
const [count, setCount] = React.useState(0)
|
|
7371
|
-
const query = useQuery({ ...options(count), enabled: count > 0 })
|
|
7372
|
-
|
|
7373
|
-
return (
|
|
7374
|
-
<div>
|
|
7375
|
-
<React.Suspense fallback={<Loading />}>
|
|
7376
|
-
<MyComponent promise={query.promise} />
|
|
7377
|
-
</React.Suspense>
|
|
7378
|
-
<button onClick={() => setCount(1)}>enable</button>
|
|
7379
|
-
</div>
|
|
7380
|
-
)
|
|
7381
|
-
}
|
|
7382
|
-
|
|
7383
|
-
const rendered = renderWithClient(queryClient, <Page />)
|
|
7384
|
-
await waitFor(() => rendered.getByText('loading..'))
|
|
7385
|
-
fireEvent.click(rendered.getByText('enable'))
|
|
7386
|
-
await waitFor(() => rendered.getByText('test1'))
|
|
7387
|
-
})
|
|
7388
|
-
|
|
7389
|
-
it('should show correct data when read from cache only (staleTime)', async () => {
|
|
7390
|
-
const key = queryKey()
|
|
7391
|
-
let suspenseRenderCount = 0
|
|
7392
|
-
queryClient.setQueryData(key, 'initial')
|
|
7393
|
-
|
|
7394
|
-
function MyComponent(props: { promise: Promise<string> }) {
|
|
7395
|
-
const data = React.use(props.promise)
|
|
7396
|
-
|
|
7397
|
-
return <>{data}</>
|
|
7398
|
-
}
|
|
7399
|
-
|
|
7400
|
-
function Loading() {
|
|
7401
|
-
suspenseRenderCount++
|
|
7402
|
-
return <>loading..</>
|
|
7403
|
-
}
|
|
7404
|
-
function Page() {
|
|
7405
|
-
const query = useQuery({
|
|
7406
|
-
queryKey: key,
|
|
7407
|
-
queryFn: async () => {
|
|
7408
|
-
await sleep(1)
|
|
7409
|
-
return 'test'
|
|
7410
|
-
},
|
|
7411
|
-
staleTime: Infinity,
|
|
7412
|
-
})
|
|
7413
|
-
|
|
7414
|
-
return (
|
|
7415
|
-
<React.Suspense fallback={<Loading />}>
|
|
7416
|
-
<MyComponent promise={query.promise} />
|
|
7417
|
-
</React.Suspense>
|
|
7418
|
-
)
|
|
7419
|
-
}
|
|
7420
|
-
|
|
7421
|
-
const rendered = renderWithClient(queryClient, <Page />)
|
|
7422
|
-
await waitFor(() => rendered.getByText('initial'))
|
|
7423
|
-
|
|
7424
|
-
expect(suspenseRenderCount).toBe(0)
|
|
7425
|
-
})
|
|
7426
|
-
|
|
7427
|
-
it('should show correct data when switching between cache entries without re-fetches', async () => {
|
|
7428
|
-
const key = queryKey()
|
|
7429
|
-
|
|
7430
|
-
function MyComponent(props: { promise: Promise<string> }) {
|
|
7431
|
-
const data = React.use(props.promise)
|
|
7432
|
-
|
|
7433
|
-
return <>{data}</>
|
|
7434
|
-
}
|
|
7435
|
-
|
|
7436
|
-
function Loading() {
|
|
7437
|
-
return <>loading..</>
|
|
7438
|
-
}
|
|
7439
|
-
function Page() {
|
|
7440
|
-
const [count, setCount] = React.useState(0)
|
|
7441
|
-
const query = useQuery({
|
|
7442
|
-
queryKey: [key, count],
|
|
7443
|
-
queryFn: async () => {
|
|
7444
|
-
await sleep(10)
|
|
7445
|
-
return 'test' + count
|
|
7446
|
-
},
|
|
7447
|
-
staleTime: Infinity,
|
|
7448
|
-
})
|
|
7449
|
-
|
|
7450
|
-
return (
|
|
7451
|
-
<div>
|
|
7452
|
-
<React.Suspense fallback={<Loading />}>
|
|
7453
|
-
<MyComponent promise={query.promise} />
|
|
7454
|
-
</React.Suspense>
|
|
7455
|
-
<button onClick={() => setCount(count + 1)}>inc</button>
|
|
7456
|
-
<button onClick={() => setCount(count - 1)}>dec</button>
|
|
7457
|
-
</div>
|
|
7458
|
-
)
|
|
7459
|
-
}
|
|
7460
|
-
|
|
7461
|
-
const rendered = renderWithClient(queryClient, <Page />)
|
|
7462
|
-
await waitFor(() => rendered.getByText('loading..'))
|
|
7463
|
-
await waitFor(() => rendered.getByText('test0'))
|
|
7464
|
-
|
|
7465
|
-
fireEvent.click(rendered.getByText('inc'))
|
|
7466
|
-
await waitFor(() => rendered.getByText('loading..'))
|
|
7467
|
-
|
|
7468
|
-
await waitFor(() => rendered.getByText('test1'))
|
|
7469
|
-
|
|
7470
|
-
console.log('---------dec------------')
|
|
7471
|
-
fireEvent.click(rendered.getByText('dec'))
|
|
7472
|
-
|
|
7473
|
-
await waitFor(() => rendered.getByText('test0'))
|
|
7474
|
-
})
|
|
7475
|
-
})
|
|
7476
6637
|
})
|