Skip to content

Commit ed9de8f

Browse files
committed
Fix tests
1 parent db165ff commit ed9de8f

File tree

5 files changed

+63
-57
lines changed

5 files changed

+63
-57
lines changed

src/context/StacApiProvider.test.tsx

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,11 @@ import { useStacApiContext } from './useStacApiContext';
66

77
// Mock fetch for testing - returns a successful response
88
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-
);
9+
(global.fetch as jest.Mock) = jest.fn((url: string) => {
10+
const response = new Response(JSON.stringify({ links: [] }));
11+
Object.defineProperty(response, 'url', { value: url });
12+
return Promise.resolve(response);
13+
});
1914
});
2015

2116
// Component to test that hooks work inside StacApiProvider

src/hooks/useCollection.test.ts

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import fetch from 'jest-fetch-mock';
22
import { renderHook, act, waitFor } from '@testing-library/react';
33
import useCollection from './useCollection';
44
import wrapper from './wrapper';
5+
import { ApiError } from '../utils/ApiError';
56

67
describe('useCollection', () => {
78
beforeEach(() => {
@@ -31,11 +32,14 @@ describe('useCollection', () => {
3132

3233
const { result } = renderHook(() => useCollection('nonexistent'), { wrapper });
3334
await waitFor(() =>
34-
expect(result.current.error).toEqual({
35-
status: 404,
36-
statusText: 'Not Found',
37-
detail: { error: 'Collection not found' },
38-
})
35+
expect(result.current.error).toEqual(
36+
new ApiError(
37+
'Not Found',
38+
404,
39+
{ error: 'Collection not found' },
40+
'https://fake-stac-api.net/collections/nonexistent'
41+
)
42+
)
3943
);
4044
});
4145

@@ -49,11 +53,14 @@ describe('useCollection', () => {
4953

5054
const { result } = renderHook(() => useCollection('abc'), { wrapper });
5155
await waitFor(() =>
52-
expect(result.current.error).toEqual({
53-
status: 400,
54-
statusText: 'Bad Request',
55-
detail: { error: 'Wrong query' },
56-
})
56+
expect(result.current.error).toEqual(
57+
new ApiError(
58+
'Bad Request',
59+
400,
60+
{ error: 'Wrong query' },
61+
'https://fake-stac-api.net/search'
62+
)
63+
)
5764
);
5865
});
5966

@@ -64,11 +71,9 @@ describe('useCollection', () => {
6471

6572
const { result } = renderHook(() => useCollection('abc'), { wrapper });
6673
await waitFor(() =>
67-
expect(result.current.error).toEqual({
68-
status: 400,
69-
statusText: 'Bad Request',
70-
detail: 'Wrong query',
71-
})
74+
expect(result.current.error).toEqual(
75+
new ApiError('Bad Request', 400, 'Wrong query', 'https://fake-stac-api.net/search')
76+
)
7277
);
7378
});
7479

src/hooks/useCollections.test.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import fetch from 'jest-fetch-mock';
22
import { renderHook, act, waitFor } from '@testing-library/react';
33
import useCollections from './useCollections';
44
import wrapper from './wrapper';
5+
import { ApiError } from '../utils/ApiError';
56

67
describe('useCollections', () => {
78
beforeEach(() => {
@@ -50,11 +51,14 @@ describe('useCollections', () => {
5051
const { result } = renderHook(() => useCollections(), { wrapper });
5152

5253
await waitFor(() =>
53-
expect(result.current.error).toEqual({
54-
status: 400,
55-
statusText: 'Bad Request',
56-
detail: { error: 'Wrong query' },
57-
})
54+
expect(result.current.error).toEqual(
55+
new ApiError(
56+
'Bad Request',
57+
400,
58+
{ error: 'Wrong query' },
59+
'https://fake-stac-api.net/search'
60+
)
61+
)
5862
);
5963
});
6064

@@ -65,11 +69,9 @@ describe('useCollections', () => {
6569

6670
const { result } = renderHook(() => useCollections(), { wrapper });
6771
await waitFor(() =>
68-
expect(result.current.error).toEqual({
69-
status: 400,
70-
statusText: 'Bad Request',
71-
detail: 'Wrong query',
72-
})
72+
expect(result.current.error).toEqual(
73+
new ApiError('Bad Request', 400, 'Wrong query', 'https://fake-stac-api.net/search')
74+
)
7375
);
7476
});
7577
});

src/hooks/useItem.test.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import fetch from 'jest-fetch-mock';
22
import { renderHook, act, waitFor } from '@testing-library/react';
33
import useItem from './useItem';
44
import wrapper from './wrapper';
5+
import { ApiError } from '../utils/ApiError';
56

67
describe('useItem', () => {
78
beforeEach(() => {
@@ -32,11 +33,14 @@ describe('useItem', () => {
3233
wrapper,
3334
});
3435
await waitFor(() =>
35-
expect(result.current.error).toEqual({
36-
status: 400,
37-
statusText: 'Bad Request',
38-
detail: { error: 'Wrong query' },
39-
})
36+
expect(result.current.error).toEqual(
37+
new ApiError(
38+
'Bad Request',
39+
400,
40+
{ error: 'Wrong query' },
41+
'https://fake-stac-api.net/search'
42+
)
43+
)
4044
);
4145
});
4246

@@ -49,11 +53,9 @@ describe('useItem', () => {
4953
wrapper,
5054
});
5155
await waitFor(() =>
52-
expect(result.current.error).toEqual({
53-
status: 400,
54-
statusText: 'Bad Request',
55-
detail: 'Wrong query',
56-
})
56+
expect(result.current.error).toEqual(
57+
new ApiError('Bad Request', 400, 'Wrong query', 'https://fake-stac-api.net/search')
58+
)
5759
);
5860
});
5961

src/hooks/useStacSearch.test.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import fetch from 'jest-fetch-mock';
22
import { renderHook, act, waitFor } from '@testing-library/react';
33
import useStacSearch from './useStacSearch';
44
import wrapper from './wrapper';
5+
import { ApiError } from '../utils/ApiError';
56

67
function parseRequestPayload(mockApiCall?: RequestInit) {
78
if (!mockApiCall) {
@@ -268,13 +269,16 @@ describe('useStacSearch — API supports POST', () => {
268269
// Wait for the search request to complete (second fetch call)
269270
await waitFor(() => expect(fetch).toHaveBeenCalledTimes(2));
270271
// Wait for error to be set in state
271-
await waitFor(() =>
272-
expect(result.current.error).toEqual({
273-
status: 400,
274-
statusText: 'Bad Request',
275-
detail: { error: 'Wrong query' },
276-
})
277-
);
272+
await waitFor(() => {
273+
expect(result.current.error).toEqual(
274+
new ApiError(
275+
'Bad Request',
276+
400,
277+
{ error: 'Wrong query' },
278+
'https://fake-stac-api.net/search'
279+
)
280+
);
281+
});
278282
});
279283

280284
it('handles error with non-JSON response', async () => {
@@ -299,11 +303,9 @@ describe('useStacSearch — API supports POST', () => {
299303
await waitFor(() => expect(fetch).toHaveBeenCalledTimes(2));
300304
// Wait for error to be set in state
301305
await waitFor(() =>
302-
expect(result.current.error).toEqual({
303-
status: 400,
304-
statusText: 'Bad Request',
305-
detail: 'Wrong query',
306-
})
306+
expect(result.current.error).toEqual(
307+
new ApiError('Bad Request', 400, 'Wrong query', 'https://fake-stac-api.net/search')
308+
)
307309
);
308310
});
309311

0 commit comments

Comments
 (0)