File tree Expand file tree Collapse file tree
apps/solidjs-boilerplate/src
components/modules/PostList Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -106,8 +106,19 @@ describe('PostList component', () => {
106106 // ⚠ `vi.mock` is "hoisted" @ https://vitest.dev/api/vi.html
107107 // try vi.doMock. It works the same way but isn't hoisted.
108108 // https://vitest.dev/api/vi.html#vi-domock
109+ // vi.doMock('@/hooks/useFetchPostList', () => ({
110+ // default: vi.fn().mockRejectedValue({ status: 'error' }),
111+ // }));
112+
113+ // Mock the API call to reject
109114 vi . doMock ( '@/hooks/useFetchPostList' , ( ) => ( {
110- default : vi . fn ( ) . mockRejectedValue ( { status : 'error' } ) ,
115+ default : vi . fn ( ) . mockReturnValue ( {
116+ // data: undefined,
117+ isLoading : false ,
118+ isError : true ,
119+ error : { status : 'error' } ,
120+ status : 'error' ,
121+ } ) ,
111122 } ) ) ;
112123
113124 const queryClient = new QueryClient ( {
@@ -150,10 +161,13 @@ describe('PostList component', () => {
150161
151162 // await waitFor(() => {
152163 // expect(result.isError).toBe(true);
153- // // expect(result.error).toEqual({ status: 'error' });
154164 // });
155165
166+ await waitFor ( ( ) => {
167+ expect ( result . error ) . toEqual ( { status : 'error' } ) ;
168+ } ) ;
169+
156170 // expect(result.data).toBeUndefined();
157- // expect(screen.getByText('Error fetching data')).toBeInTheDocument();
171+ // expect(screen.getByText('Error: Failed to fetch data')).toBeInTheDocument();
158172 } ) ;
159173} ) ;
Original file line number Diff line number Diff line change 11import type { Component } from 'solid-js' ;
2- import { Switch , Match , For } from 'solid-js' ;
2+ import { ErrorBoundary , Suspense , For } from 'solid-js' ;
33
44import { A } from '@solidjs/router' ;
55import toast from 'solid-toast' ;
@@ -15,8 +15,6 @@ const PostList: Component = () => {
1515 toast . error ( 'Failed to fetch data.' ) ;
1616 }
1717
18- console . log ( query ) ;
19-
2018 // w/ createResource()
2119 // const [posts, { refetch }] = createResource<Post[]>(fetchData);
2220
@@ -30,14 +28,10 @@ const PostList: Component = () => {
3028
3129 < section >
3230 < div class = "inner" >
33- < Switch >
34- < Match when = { query . isLoading } >
35- < div > Loading...</ div >
36- </ Match >
37- < Match when = { query . isError } >
38- < div > Error: { ( query . error as Error ) . message } </ div >
39- </ Match >
40- < Match when = { query . isSuccess } >
31+ < ErrorBoundary
32+ fallback = { < div > Error: { ( query . error as Error ) . message } </ div > }
33+ >
34+ < Suspense fallback = { < div > Loading...</ div > } >
4135 < ul >
4236 < For each = { query . data } >
4337 { post => (
@@ -47,8 +41,8 @@ const PostList: Component = () => {
4741 ) }
4842 </ For >
4943 </ ul >
50- </ Match >
51- </ Switch >
44+ </ Suspense >
45+ </ ErrorBoundary >
5246 </ div >
5347 </ section >
5448
Original file line number Diff line number Diff line change 1+ import type { CreateQueryResult } from '@tanstack/solid-query' ;
12import { createQuery } from '@tanstack/solid-query' ;
23
34import type { Post } from '@/types/api/post.interface' ;
45
6+ // import type { Post } from '@/types/api/post.interface';
7+
58const apiEndpointUrl = import . meta. env . VITE_PUBLIC_API_URL ;
69
7- const fetchData = async ( ) : Promise < Post [ ] > =>
8- ( await fetch ( `${ apiEndpointUrl } /posts` ) ) . json ( ) ;
10+ // const fetchData = async (): Promise<Post[]> =>
11+ // (await fetch(`${apiEndpointUrl}/posts`)).json();
912
1013const useFetchPostList = ( ) => {
11- const query = createQuery ( ( ) => ( {
14+ // const query = createQuery(() => ({
15+ // queryKey: ['/posts'],
16+ // queryFn: fetchData,
17+ // }));
18+
19+ const query : CreateQueryResult < Post [ ] , Error > = createQuery ( ( ) => ( {
1220 queryKey : [ '/posts' ] ,
13- queryFn : fetchData ,
21+ queryFn : async ( ) => {
22+ const result = await fetch ( `${ apiEndpointUrl } /posts` ) ;
23+ if ( ! result . ok ) throw new Error ( 'Failed to fetch data' ) ;
24+ return result . json ( ) ;
25+ } ,
26+ staleTime : 1000 * 60 * 5 , // 5 minutes
27+ throwOnError : true , // Throw an error if the query fails
1428 } ) ) ;
1529
1630 return query ;
You can’t perform that action at this time.
0 commit comments