File tree Expand file tree Collapse file tree 4 files changed +49
-2
lines changed
Expand file tree Collapse file tree 4 files changed +49
-2
lines changed Original file line number Diff line number Diff line change @@ -26,8 +26,8 @@ export type UseQueryResult<T> = {
2626export type MakeDataRequired <
2727 T extends readonly UseQueryResult < unknown > [ ]
2828> = {
29- // @ts -ignore: Cannot use data to index type T[K]
30- [ K in keyof T ] -? : T [ K ] & { data : NonNullable < T [ K ] [ "data" ] > } ;
29+ // @ts -ignore: TS2536: Type '" data"' cannot be used to index type ' T[K]'.
30+ [ K in keyof T ] : T [ K ] & { data : NonNullable < T [ K ] [ "data" ] > } ;
3131} ;
3232
3333export type LoaderTransformFunction <
Original file line number Diff line number Diff line change @@ -20,6 +20,9 @@ export const handlers = [
2020 ) ;
2121 } ) ,
2222 rest . get ( "/pokemon/:name" , ( req , res , c ) => {
23+ if ( req . params . name === "error" ) {
24+ return res ( c . delay ( RESPONSE_DELAY ) , c . status ( 500 ) ) ;
25+ }
2326 return res (
2427 c . delay ( RESPONSE_DELAY ) ,
2528 c . status ( 200 ) ,
Original file line number Diff line number Diff line change @@ -58,6 +58,29 @@ export const ExtendedLoaderComponent = withLoader(
5858 extendedLoader
5959) ;
6060
61+ const pokemonByNameLoader = createLoader ( {
62+ queries : ( name : string ) =>
63+ [ useGetPokemonByNameQuery ( name ) ] as const ,
64+ queriesArg : ( props : { name : string } ) => props . name ,
65+ } ) ;
66+
67+ export const LoadPokemon = withLoader ( ( props , loaderData ) => {
68+ return (
69+ < div >
70+ Loaded: "{ loaderData [ 0 ] . data . name } ", props: "{ props . name } "
71+ </ div >
72+ ) ;
73+ } , pokemonByNameLoader ) ;
74+
75+ export const FailTester = withLoader (
76+ ( ) => < div > Success</ div > ,
77+ createLoader ( {
78+ queries : ( ) => [ useGetPokemonByNameQuery ( "error" ) ] as const ,
79+ onError : ( ) => < div > Error</ div > ,
80+ onLoading : ( ) => < div > Loading</ div > ,
81+ } )
82+ ) ;
83+
6184export const TestAggregateComponent = ( ) => {
6285 const q1 = useGetPokemonByNameQuery ( "charizard" ) ;
6386 const q2 = useGetPokemonsQuery ( undefined ) ;
Original file line number Diff line number Diff line change 11/* eslint-disable react-hooks/rules-of-hooks */
22import {
33 ExtendedLoaderComponent ,
4+ FailTester ,
5+ LoadPokemon ,
46 SimpleLoadedComponent ,
57 TestAggregateComponent ,
68} from "./testComponents" ;
@@ -26,6 +28,25 @@ describe("withLoader", () => {
2628 expect ( screen . getByText ( "pikachu" ) ) . toBeVisible ( ) ;
2729 } ) ;
2830
31+ test ( "Loader passes props through queriesArg to queries" , async ( ) => {
32+ render ( < LoadPokemon name = "charizard" /> ) ;
33+ await waitFor ( ( ) =>
34+ expect (
35+ screen . getByText (
36+ 'Loaded: "charizard", props: "charizard"'
37+ )
38+ ) . toBeVisible ( )
39+ ) ;
40+ } ) ;
41+
42+ test ( "onError renders when applicable" , async ( ) => {
43+ render ( < FailTester /> ) ;
44+ expect ( screen . getByText ( "Loading" ) ) . toBeVisible ( ) ;
45+ await waitFor ( ( ) =>
46+ expect ( screen . getByText ( "Error" ) ) . toBeVisible ( )
47+ ) ;
48+ } ) ;
49+
2950 describe ( ".extend()" , ( ) => {
3051 test ( "Can extend onLoading" , async ( ) => {
3152 render ( < ExtendedLoaderComponent /> ) ;
You can’t perform that action at this time.
0 commit comments