Skip to content

Commit b9168c2

Browse files
committed
feature(useQueries): add test cases for invalid query function return types
1 parent 201cae1 commit b9168c2

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

src/reactjs/tests/useQueries.test.tsx

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,76 @@ describe('useQueries', () => {
607607
// @ts-expect-error (Page component is not rendered)
608608
// eslint-disable-next-line
609609
function Page() {
610+
// Rejects queryFn that returns/resolved to undefined or void
611+
// @ts-expect-error (queryFn must not return undefined)
612+
useQueries({ queries: [{ queryKey: key1, queryFn: () => undefined }] })
613+
// @ts-expect-error (queryFn must not return void)
614+
// eslint-disable-next-line @typescript-eslint/no-empty-function
615+
useQueries({ queries: [{ queryKey: key1, queryFn: () => {} }] })
616+
617+
useQueries({
618+
// @ts-expect-error (queryFn must not return explicitly undefined)
619+
queries: [{ queryKey: key1, queryFn: (): undefined => undefined }],
620+
})
621+
622+
useQueries({
623+
// @ts-expect-error (queryFn must not return explicitly void)
624+
queries: [{ queryKey: key1, queryFn: (): void => undefined }],
625+
})
626+
627+
useQueries({
628+
// @ts-expect-error (queryFn must not return explicitly Promise<void>)
629+
queries: [{ queryKey: key1, queryFn: (): Promise<void> => undefined }],
630+
})
631+
632+
useQueries({
633+
queries: [
634+
// @ts-expect-error (queryFn must not return explicitly Promise<undefined>)
635+
{ queryKey: key1, queryFn: (): Promise<undefined> => undefined },
636+
],
637+
})
638+
useQueries({
639+
queries: [
640+
// @ts-expect-error (queryFn must not return Promise<undefined>)
641+
{ queryKey: key2, queryFn: () => Promise.resolve(undefined) },
642+
],
643+
})
644+
useQueries({
645+
// @ts-expect-error (queryFn must not return Promise<undefined>)
646+
queries: Array(50).map((_, i) => ({
647+
queryKey: ['key', i] as const,
648+
queryFn: () => Promise.resolve(undefined),
649+
})),
650+
})
651+
652+
// Rejects queryFn that always throws
653+
useQueries({
654+
queries: [
655+
// @ts-expect-error (queryFn must not return undefined)
656+
{
657+
queryKey: key3,
658+
queryFn: async () => {
659+
throw new Error('')
660+
},
661+
},
662+
],
663+
})
664+
665+
// Accepts queryFn that *sometimes* throws
666+
useQueries({
667+
queries: [
668+
{
669+
queryKey: key3,
670+
queryFn: async () => {
671+
if (Math.random() > 0.1) {
672+
throw new Error('')
673+
}
674+
return 'result'
675+
},
676+
},
677+
],
678+
})
679+
610680
// Array.map preserves TQueryFnData
611681
const result1 = useQueries({
612682
queries: Array(50).map((_, i) => ({

0 commit comments

Comments
 (0)