@@ -4,10 +4,21 @@ import { MetaProvider } from '@solidjs/meta';
44import { Route , Router } from '@solidjs/router' ;
55import { render , renderHook , screen , waitFor } from '@solidjs/testing-library' ;
66import { QueryClient , QueryClientProvider } from '@tanstack/solid-query' ;
7- import { describe , test , vi } from 'vitest' ;
7+ import { HttpResponse , http } from 'msw' ;
8+ import toast from 'solid-toast' ;
9+ import { describe , expect , test , vi } from 'vitest' ;
810
911import PostList from '@/components/modules/PostList/PostList' ;
1012import useFetchPostList from '@/hooks/useFetchPostList' ;
13+ import { server } from '@/mocks/server' ;
14+
15+ const apiEndpointUrl = import . meta. env . VITE_PUBLIC_API_URL ;
16+
17+ vi . mock ( 'solid-toast' , ( ) => ( {
18+ default : {
19+ error : vi . fn ( ) ,
20+ } ,
21+ } ) ) ;
1122
1223describe ( 'PostList component' , ( ) => {
1324 // const queryClient = new QueryClient({
@@ -102,24 +113,18 @@ describe('PostList component', () => {
102113 } ) ;
103114
104115 test ( 'should handle API call failure' , async ( ) => {
105- // Mock the API call to reject
106- // ⚠ `vi.mock` is "hoisted" @ https://vitest.dev/api/vi.html
107- // try vi.doMock. It works the same way but isn't hoisted.
108- // 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
114- vi . doMock ( '@/hooks/useFetchPostList' , ( ) => ( {
115- default : vi . fn ( ) . mockReturnValue ( {
116- // data: undefined,
117- isLoading : false ,
118- isError : true ,
119- error : { status : 'error' } ,
120- status : 'error' ,
121- } ) ,
122- } ) ) ;
116+ const errorMessage = 'Failed to fetch data.' ;
117+ const toastErrorSpy = vi . spyOn ( toast , 'error' ) ;
118+
119+ server . use (
120+ http . get (
121+ `${ apiEndpointUrl } /posts` ,
122+ ( ) =>
123+ new HttpResponse ( null , {
124+ status : 500 ,
125+ } ) ,
126+ ) ,
127+ ) ;
123128
124129 const queryClient = new QueryClient ( {
125130 defaultOptions : { queries : { retry : false } } , // @ https://github.com/TanStack/query/discussions/2300#discussioncomment-811768
@@ -146,28 +151,36 @@ describe('PostList component', () => {
146151 // eslint-disable-next-line @typescript-eslint/no-shadow
147152 root = { props => < > { props . children } </ > }
148153 >
149- < Route path = "/" component = { ( ) => < > { props . children } </ > } />
154+ < Route
155+ path = "/"
156+ component = { ( ) => (
157+ < >
158+ { props . children }
159+ { /* <Toaster position="bottom-center" /> */ }
160+ </ >
161+ ) }
162+ />
150163 </ Router >
151164 </ QueryClientProvider >
152165 ) ;
153166
167+ // screen.getByRole('');
154168 const { result } = renderHook ( ( ) => useFetchPostList ( ) , {
155169 wrapper,
156170 } ) ;
157171
158- await waitFor ( ( ) => result . isError ) ;
159-
160- expect ( result . data ) . toEqual ( undefined ) ;
172+ await waitFor ( ( ) => expect ( result . isError ) . toBe ( true ) ) ;
161173
162- // await waitFor(() => {
163- // expect(result.isError).toBe(true );
164- // });
174+ await waitFor ( ( ) => {
175+ expect ( result . error ) . toEqual ( Error ( 'Failed to fetch data' ) ) ;
176+ } ) ;
165177
166178 await waitFor ( ( ) => {
167- expect ( result . error ) . toEqual ( { status : 'error' } ) ;
179+ expect ( toastErrorSpy ) . toHaveBeenCalledWith ( errorMessage ) ;
168180 } ) ;
169181
170- // expect(result.data).toBeUndefined();
171- // expect(screen.getByText('Error: Failed to fetch data')).toBeInTheDocument();
182+ // expect(
183+ // await screen.findByText('Failed to fetch data.'),
184+ // ).toBeInTheDocument();
172185 } ) ;
173186} ) ;
0 commit comments