Skip to content

Commit 363d53e

Browse files
committed
test: add StacApiProvider test coverage
Add tests for StacApiProvider covering QueryClient management, DevTools integration, and context provisioning. - Creates QueryClient when no parent exists - Uses parent QueryClient without nesting providers - DevTools respects enableDevTools prop - Provides stacApi and context methods
1 parent 3ddb6f2 commit 363d53e

File tree

1 file changed

+205
-0
lines changed

1 file changed

+205
-0
lines changed
Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
import React from 'react';
2+
import { render, screen, waitFor } from '@testing-library/react';
3+
import { QueryClient, QueryClientProvider, useQueryClient } from '@tanstack/react-query';
4+
import { StacApiProvider } from './index';
5+
import { useStacApiContext } from './useStacApiContext';
6+
7+
// Mock fetch for testing - returns a successful response
8+
beforeEach(() => {
9+
(global.fetch as jest.Mock) = jest.fn((url: string) =>
10+
Promise.resolve({
11+
ok: true,
12+
url, // Return the requested URL
13+
json: () =>
14+
Promise.resolve({
15+
links: [],
16+
}),
17+
})
18+
);
19+
});
20+
21+
// Component to test that hooks work inside StacApiProvider
22+
function TestComponent() {
23+
const context = useStacApiContext();
24+
const queryClient = useQueryClient();
25+
26+
return (
27+
<div>
28+
<div data-testid="stac-api">{context.stacApi ? 'stacApi exists' : 'no stacApi'}</div>
29+
<div data-testid="query-client">{queryClient ? 'queryClient exists' : 'no queryClient'}</div>
30+
</div>
31+
);
32+
}
33+
34+
describe('StacApiProvider', () => {
35+
beforeEach(() => {
36+
// Clean up window.__TANSTACK_QUERY_CLIENT__ before each test
37+
delete (window as Window & { __TANSTACK_QUERY_CLIENT__?: unknown }).__TANSTACK_QUERY_CLIENT__;
38+
});
39+
40+
describe('QueryClient management', () => {
41+
it('creates a QueryClient when no parent exists', async () => {
42+
render(
43+
<StacApiProvider apiUrl="https://test-stac-api.com">
44+
<TestComponent />
45+
</StacApiProvider>
46+
);
47+
48+
await waitFor(() => {
49+
expect(screen.getByTestId('stac-api')).toHaveTextContent('stacApi exists');
50+
});
51+
expect(screen.getByTestId('query-client')).toHaveTextContent('queryClient exists');
52+
});
53+
54+
it('uses existing QueryClient from parent context', async () => {
55+
const parentClient = new QueryClient({
56+
defaultOptions: {
57+
queries: { retry: false },
58+
},
59+
});
60+
61+
render(
62+
<QueryClientProvider client={parentClient}>
63+
<StacApiProvider apiUrl="https://test-stac-api.com">
64+
<TestComponent />
65+
</StacApiProvider>
66+
</QueryClientProvider>
67+
);
68+
69+
await waitFor(() => {
70+
expect(screen.getByTestId('stac-api')).toHaveTextContent('stacApi exists');
71+
});
72+
expect(screen.getByTestId('query-client')).toHaveTextContent('queryClient exists');
73+
});
74+
75+
it('does not create nested QueryClientProvider when parent exists', () => {
76+
const parentClient = new QueryClient();
77+
78+
// Component that checks if QueryClient instance is the parent
79+
function ClientChecker() {
80+
const client = useQueryClient();
81+
return <div data-testid="is-parent">{client === parentClient ? 'true' : 'false'}</div>;
82+
}
83+
84+
render(
85+
<QueryClientProvider client={parentClient}>
86+
<StacApiProvider apiUrl="https://test-stac-api.com">
87+
<ClientChecker />
88+
</StacApiProvider>
89+
</QueryClientProvider>
90+
);
91+
92+
expect(screen.getByTestId('is-parent')).toHaveTextContent('true');
93+
});
94+
});
95+
96+
describe('DevTools integration', () => {
97+
it('does not set up DevTools when enableDevTools is false', () => {
98+
render(
99+
<StacApiProvider apiUrl="https://test-stac-api.com" enableDevTools={false}>
100+
<TestComponent />
101+
</StacApiProvider>
102+
);
103+
104+
expect(
105+
(window as Window & { __TANSTACK_QUERY_CLIENT__?: unknown }).__TANSTACK_QUERY_CLIENT__
106+
).toBeUndefined();
107+
});
108+
109+
it('does not set up DevTools when enableDevTools is not provided', () => {
110+
render(
111+
<StacApiProvider apiUrl="https://test-stac-api.com">
112+
<TestComponent />
113+
</StacApiProvider>
114+
);
115+
116+
expect(
117+
(window as Window & { __TANSTACK_QUERY_CLIENT__?: unknown }).__TANSTACK_QUERY_CLIENT__
118+
).toBeUndefined();
119+
});
120+
121+
it('sets up DevTools when enableDevTools is true', () => {
122+
render(
123+
<StacApiProvider apiUrl="https://test-stac-api.com" enableDevTools={true}>
124+
<TestComponent />
125+
</StacApiProvider>
126+
);
127+
128+
expect(
129+
(window as Window & { __TANSTACK_QUERY_CLIENT__?: unknown }).__TANSTACK_QUERY_CLIENT__
130+
).toBeDefined();
131+
});
132+
133+
it('sets up DevTools with parent QueryClient when enabled', () => {
134+
const parentClient = new QueryClient();
135+
136+
render(
137+
<QueryClientProvider client={parentClient}>
138+
<StacApiProvider apiUrl="https://test-stac-api.com" enableDevTools={true}>
139+
<TestComponent />
140+
</StacApiProvider>
141+
</QueryClientProvider>
142+
);
143+
144+
expect(
145+
(window as Window & { __TANSTACK_QUERY_CLIENT__?: unknown }).__TANSTACK_QUERY_CLIENT__
146+
).toBe(parentClient);
147+
});
148+
});
149+
150+
describe('Context value', () => {
151+
it('provides stacApi with correct apiUrl', async () => {
152+
function ApiUrlChecker() {
153+
const { stacApi } = useStacApiContext();
154+
return <div data-testid="api-url">{stacApi?.baseUrl || 'loading'}</div>;
155+
}
156+
157+
render(
158+
<StacApiProvider apiUrl="https://my-custom-api.com">
159+
<ApiUrlChecker />
160+
</StacApiProvider>
161+
);
162+
163+
await waitFor(() => {
164+
expect(screen.getByTestId('api-url')).toHaveTextContent('https://my-custom-api.com');
165+
});
166+
});
167+
168+
it('provides context methods', () => {
169+
function ContextChecker() {
170+
const context = useStacApiContext();
171+
const hasMethods =
172+
typeof context.getItem === 'function' &&
173+
typeof context.addItem === 'function' &&
174+
typeof context.deleteItem === 'function';
175+
return <div data-testid="has-methods">{hasMethods ? 'true' : 'false'}</div>;
176+
}
177+
178+
render(
179+
<StacApiProvider apiUrl="https://test-stac-api.com">
180+
<ContextChecker />
181+
</StacApiProvider>
182+
);
183+
184+
expect(screen.getByTestId('has-methods')).toHaveTextContent('true');
185+
});
186+
187+
it('passes options to useStacApi hook', () => {
188+
const customOptions = { headers: { 'X-Custom': 'value' } };
189+
190+
function OptionsChecker() {
191+
// Options are passed to useStacApi which uses them in the query
192+
return <div data-testid="has-options">true</div>;
193+
}
194+
195+
render(
196+
<StacApiProvider apiUrl="https://test-stac-api.com" options={customOptions}>
197+
<OptionsChecker />
198+
</StacApiProvider>
199+
);
200+
201+
// If provider doesn't error and renders, options were passed successfully
202+
expect(screen.getByTestId('has-options')).toHaveTextContent('true');
203+
});
204+
});
205+
});

0 commit comments

Comments
 (0)