Skip to content

Commit 9c20df5

Browse files
authored
feat: TanStack Query (#22)
* feat: TanStack Query * feat: TanStack Query
1 parent c419740 commit 9c20df5

File tree

10 files changed

+171
-15
lines changed

10 files changed

+171
-15
lines changed

package-lock.json

Lines changed: 28 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
"private": true,
55
"type": "module",
66
"dependencies": {
7+
"@tanstack/react-query": "^5.62.15",
8+
"@tanstack/react-router": "^1.95.0",
79
"react": "^19.0.0",
810
"react-dom": "^19.0.0"
911
},
1012
"devDependencies": {
1113
"@semantic-release/changelog": "^6.0.3",
1214
"@semantic-release/git": "^10.0.1",
1315
"@semantic-release/npm": "^12.0.1",
14-
"@tanstack/react-router": "^1.95.0",
1516
"@tanstack/router-devtools": "^1.95.0",
1617
"@tanstack/router-plugin": "^1.95.0",
1718
"@testing-library/jest-dom": "^6.6.3",
@@ -23,8 +24,8 @@
2324
"autoprefixer": "^10.4.20",
2425
"esbuild": "0.24.0",
2526
"eslint": "^9.17.0",
26-
"eslint-plugin-prettier": "^5.2.1",
2727
"eslint-config-prettier": "^9.1.0",
28+
"eslint-plugin-prettier": "^5.2.1",
2829
"eslint-plugin-react-hooks": "^5.1.0",
2930
"eslint-plugin-react-refresh": "^0.4.16",
3031
"globals": "^15.14.0",

src/components/App/App.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
text-align: center;
33

44
.app-header {
5-
min-height: 100vh;
5+
min-height: 80vh;
66
display: flex;
77
flex-direction: column;
88
align-items: center;

src/components/App/App.test.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
import { render, screen } from '@testing-library/react';
22
import App from '.';
33

4+
vi.mock('@tanstack/react-query', () => ({
5+
useQuery: vi.fn().mockReturnValue({
6+
data: {},
7+
isPending: true,
8+
error: {},
9+
}),
10+
}));
11+
412
test('renders Header', () => {
513
render(<App />);
614
const headerElement = screen.getByText(/Hello World/i);

src/components/App/index.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1+
import { QueryExample } from '../QueryExample';
2+
13
import './App.css';
24

35
export default function App() {
46
return (
5-
<div className="app">
7+
<main className="app">
68
<header className="app-header">
79
<h1>Hello World</h1>
810
</header>
9-
</div>
11+
<QueryExample />
12+
</main>
1013
);
1114
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import { render, screen } from '@testing-library/react';
2+
import { useQuery } from '@tanstack/react-query';
3+
import { QueryExample } from '.';
4+
5+
vi.mock('@tanstack/react-query');
6+
7+
describe('QueryExample', () => {
8+
test('renders data when data is provided', () => {
9+
vi.mocked(useQuery, { partial: true }).mockReturnValue({
10+
data: { name: 'Query Example' },
11+
isPending: false,
12+
error: null,
13+
});
14+
15+
render(<QueryExample />);
16+
const headerElement = screen.getByText(/Query Example/i);
17+
18+
expect(headerElement).toBeInTheDocument();
19+
});
20+
21+
test('renders error message when error is provided', () => {
22+
vi.mocked(useQuery, { partial: true }).mockReturnValue({
23+
data: {},
24+
isPending: false,
25+
error: { message: 'oops' },
26+
});
27+
28+
render(<QueryExample />);
29+
const errorElement = screen.getByText(/An error has occurred: oops/i);
30+
31+
expect(errorElement).toBeInTheDocument();
32+
});
33+
34+
test('renders loading when isPending is provided', () => {
35+
// @ts-ignore - isPending type is false and not boolean...
36+
vi.mocked(useQuery, { partial: true }).mockReturnValue({
37+
data: {},
38+
isPending: true,
39+
error: null,
40+
});
41+
42+
render(<QueryExample />);
43+
const loadingElement = screen.getByText(/Loading.../i);
44+
45+
expect(loadingElement).toBeInTheDocument();
46+
});
47+
48+
test('renders error as 1st priority', () => {
49+
// @ts-ignore - isPending type is false and not boolean...
50+
vi.mocked(useQuery, { partial: true }).mockReturnValue({
51+
data: { name: 'Query Example' },
52+
isPending: true,
53+
error: { message: 'oops' },
54+
});
55+
56+
render(<QueryExample />);
57+
const loadingElement = screen.getByText(/An error has occurred: oops/i);
58+
59+
expect(loadingElement).toBeInTheDocument();
60+
});
61+
62+
test('renders loading as 2nd priority', () => {
63+
// @ts-ignore - isPending type is false and not boolean...
64+
vi.mocked(useQuery, { partial: true }).mockReturnValue({
65+
data: { name: 'Query Example' },
66+
isPending: true,
67+
error: null,
68+
});
69+
70+
render(<QueryExample />);
71+
const loadingElement = screen.getByText(/Loading.../i);
72+
73+
expect(loadingElement).toBeInTheDocument();
74+
});
75+
});
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { useQuery } from '@tanstack/react-query';
2+
import { queryFetch } from '../../utils/queryFetch';
3+
4+
export function QueryExample() {
5+
const { isPending, error, data } = useQuery({
6+
queryKey: ['queryExample'],
7+
queryFn: queryFetch('https://api.github.com/repos/TanStack/query'),
8+
});
9+
10+
if (error) return <p>An error has occurred: {error.message}</p>;
11+
12+
if (isPending) return <p>Loading...</p>;
13+
14+
return (
15+
<div className="query-example">
16+
<h1>{data.name}</h1>
17+
<p>{data.description}</p>
18+
<strong>👀 {data.subscribers_count}</strong>{' '}
19+
<strong>{data.stargazers_count}</strong>{' '}
20+
<strong>🍴 {data.forks_count}</strong>
21+
</div>
22+
);
23+
}

src/index.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
import { StrictMode } from 'react';
22
import { createRoot } from 'react-dom/client';
3+
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
34
import App from './components/App';
45

56
const rootElement = document.getElementById('root');
7+
const queryClient = new QueryClient();
8+
69
if (rootElement) {
710
createRoot(rootElement).render(
811
<StrictMode>
9-
<App />
12+
<QueryClientProvider client={queryClient}>
13+
<App />
14+
</QueryClientProvider>
1015
</StrictMode>
1116
);
1217
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { queryFetch } from '../queryFetch';
2+
3+
const fetchMock = vi.fn(() =>
4+
Promise.resolve({ json: () => Promise.resolve({ data: 'stick' }) })
5+
);
6+
7+
vi.stubGlobal('fetch', fetchMock);
8+
9+
describe('queryFetch', () => {
10+
test('calls fetch', () => {
11+
vi.spyOn(globalThis, 'fetch');
12+
queryFetch('test')();
13+
expect(fetch).toBeCalled();
14+
});
15+
16+
test('returns data', async () => {
17+
const fetchRequest = await queryFetch('test')();
18+
expect(fetchRequest).toEqual({ data: 'stick' });
19+
});
20+
});

src/utils/queryFetch.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const queryFetch = (url: string) => () =>
2+
fetch(url).then((res) => res?.json());

0 commit comments

Comments
 (0)