Skip to content

Commit 5a65cdf

Browse files
committed
test: add testing for response 500 error
1 parent 7b98671 commit 5a65cdf

2 files changed

Lines changed: 48 additions & 33 deletions

File tree

apps/solidjs-boilerplate/src/components/modules/PostList/PostList.test.tsx

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,21 @@ import { MetaProvider } from '@solidjs/meta';
44
import { Route, Router } from '@solidjs/router';
55
import { render, renderHook, screen, waitFor } from '@solidjs/testing-library';
66
import { 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

911
import PostList from '@/components/modules/PostList/PostList';
1012
import 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

1223
describe('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
});

apps/solidjs-boilerplate/src/components/modules/PostList/PostList.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Component } from 'solid-js';
2-
import { ErrorBoundary, Suspense, For } from 'solid-js';
2+
import { ErrorBoundary, Suspense, For, createEffect } from 'solid-js';
33

44
import { A } from '@solidjs/router';
55
import toast from 'solid-toast';
@@ -11,9 +11,11 @@ const PostList: Component = () => {
1111
// w/ createQuery() on '@tanstack/solid-query'
1212
const query = useFetchPostList();
1313

14-
if (query.isError) {
15-
toast.error('Failed to fetch data.');
16-
}
14+
createEffect(() => {
15+
if (query.isError) {
16+
toast.error('Failed to fetch data.');
17+
}
18+
});
1719

1820
// w/ createResource()
1921
// const [posts, { refetch }] = createResource<Post[]>(fetchData);

0 commit comments

Comments
 (0)