|
8 | 8 | QueryCache, |
9 | 9 | QueryErrorResetBoundary, |
10 | 10 | useInfiniteQuery, |
| 11 | + useQueries, |
11 | 12 | useQuery, |
12 | 13 | useQueryErrorResetBoundary, |
13 | 14 | } from '..' |
@@ -1011,3 +1012,113 @@ describe("useQuery's in Suspense mode", () => { |
1011 | 1012 | expect(rendered.queryByText('rendered')).not.toBeNull() |
1012 | 1013 | }) |
1013 | 1014 | }) |
| 1015 | + |
| 1016 | +describe('useQueries with suspense', () => { |
| 1017 | + const queryClient = createQueryClient() |
| 1018 | + it('should suspend all queries in parallel', async () => { |
| 1019 | + const key1 = queryKey() |
| 1020 | + const key2 = queryKey() |
| 1021 | + const results: string[] = [] |
| 1022 | + |
| 1023 | + function Fallback() { |
| 1024 | + results.push('loading') |
| 1025 | + return <div>loading</div> |
| 1026 | + } |
| 1027 | + |
| 1028 | + function Page() { |
| 1029 | + const result = useQueries({ |
| 1030 | + queries: [ |
| 1031 | + { |
| 1032 | + queryKey: key1, |
| 1033 | + queryFn: async () => { |
| 1034 | + results.push('1') |
| 1035 | + await sleep(10) |
| 1036 | + return '1' |
| 1037 | + }, |
| 1038 | + suspense: true, |
| 1039 | + }, |
| 1040 | + { |
| 1041 | + queryKey: key2, |
| 1042 | + queryFn: async () => { |
| 1043 | + results.push('2') |
| 1044 | + await sleep(20) |
| 1045 | + return '2' |
| 1046 | + }, |
| 1047 | + suspense: true, |
| 1048 | + }, |
| 1049 | + ], |
| 1050 | + }) |
| 1051 | + return ( |
| 1052 | + <div> |
| 1053 | + <h1>data: {result.map((it) => it.data ?? 'null').join(',')}</h1> |
| 1054 | + </div> |
| 1055 | + ) |
| 1056 | + } |
| 1057 | + |
| 1058 | + const rendered = renderWithClient( |
| 1059 | + queryClient, |
| 1060 | + <React.Suspense fallback={<Fallback />}> |
| 1061 | + <Page /> |
| 1062 | + </React.Suspense>, |
| 1063 | + ) |
| 1064 | + await waitFor(() => rendered.getByText('loading')) |
| 1065 | + await waitFor(() => rendered.getByText('data: 1,2')) |
| 1066 | + |
| 1067 | + expect(results).toEqual(['1', '2', 'loading']) |
| 1068 | + }) |
| 1069 | + |
| 1070 | + it('should allow to mix suspense with non-suspense', async () => { |
| 1071 | + const key1 = queryKey() |
| 1072 | + const key2 = queryKey() |
| 1073 | + const results: string[] = [] |
| 1074 | + |
| 1075 | + function Fallback() { |
| 1076 | + results.push('loading') |
| 1077 | + return <div>loading</div> |
| 1078 | + } |
| 1079 | + |
| 1080 | + function Page() { |
| 1081 | + const result = useQueries({ |
| 1082 | + queries: [ |
| 1083 | + { |
| 1084 | + queryKey: key1, |
| 1085 | + queryFn: async () => { |
| 1086 | + results.push('1') |
| 1087 | + await sleep(10) |
| 1088 | + return '1' |
| 1089 | + }, |
| 1090 | + suspense: true, |
| 1091 | + }, |
| 1092 | + { |
| 1093 | + queryKey: key2, |
| 1094 | + queryFn: async () => { |
| 1095 | + results.push('2') |
| 1096 | + await sleep(20) |
| 1097 | + return '2' |
| 1098 | + }, |
| 1099 | + suspense: false, |
| 1100 | + }, |
| 1101 | + ], |
| 1102 | + }) |
| 1103 | + return ( |
| 1104 | + <div> |
| 1105 | + <h1>data: {result.map((it) => it.data ?? 'null').join(',')}</h1> |
| 1106 | + <h2>status: {result.map((it) => it.status).join(',')}</h2> |
| 1107 | + </div> |
| 1108 | + ) |
| 1109 | + } |
| 1110 | + |
| 1111 | + const rendered = renderWithClient( |
| 1112 | + queryClient, |
| 1113 | + <React.Suspense fallback={<Fallback />}> |
| 1114 | + <Page /> |
| 1115 | + </React.Suspense>, |
| 1116 | + ) |
| 1117 | + await waitFor(() => rendered.getByText('loading')) |
| 1118 | + await waitFor(() => rendered.getByText('status: success,loading')) |
| 1119 | + await waitFor(() => rendered.getByText('data: 1,null')) |
| 1120 | + await waitFor(() => rendered.getByText('data: 1,2')) |
| 1121 | + |
| 1122 | + expect(results).toEqual(['1', '2', 'loading']) |
| 1123 | + }) |
| 1124 | +}) |
0 commit comments