Skip to content

Commit b763302

Browse files
committed
fix: Send selection collections as undefined is array is empty [fix #16]
1 parent adfcc7b commit b763302

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/hooks/useStacSearch.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,22 @@ describe('useStacSearch', () => {
6262
expect(result.current.results).toEqual({ data: '12345' });
6363
});
6464

65+
it('clears collections when array is empty', async () => {
66+
fetch.mockResponseOnce(JSON.stringify({ data: '12345' }));
67+
68+
const { result, waitForNextUpdate } = renderHook(
69+
() => useStacSearch(stacApi)
70+
);
71+
72+
act(() => result.current.setCollections([]));
73+
act(() => result.current.submit());
74+
await waitForNextUpdate();
75+
76+
const postPayload = parseRequestPayload(fetch.mock.calls[0][1]);
77+
expect(postPayload).toEqual({ limit: 25 });
78+
expect(result.current.results).toEqual({ data: '12345' });
79+
});
80+
6581
it('includes date range in search', async () => {
6682
fetch.mockResponseOnce(JSON.stringify({ data: '12345' }));
6783

src/stac-api/index.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { ApiError, GenericObject } from '../types';
2-
import type { Bbox, SearchPayload, DateRange } from '../types/stac';
2+
import type { Bbox, SearchPayload, DateRange, CollectionIdList } from '../types/stac';
33

44
type RequestPayload = SearchPayload;
55
type FetchOptions = {
@@ -36,6 +36,10 @@ class StacApi {
3636
return sortedBbox;
3737
}
3838

39+
makeCollectionsPayload(collections?: CollectionIdList) {
40+
return collections?.length ? collections : undefined;
41+
}
42+
3943
makeDatetimePayload(dateRange?: DateRange): string | undefined {
4044
if (!dateRange) {
4145
return undefined;
@@ -88,9 +92,10 @@ class StacApi {
8892
}
8993

9094
search(payload: SearchPayload, headers = {}): Promise<Response> {
91-
const { bbox, dateRange, ...restPayload } = payload;
95+
const { bbox, dateRange, collections, ...restPayload } = payload;
9296
const requestPayload = {
9397
...restPayload,
98+
collections: this.makeCollectionsPayload(collections),
9499
bbox: this.fixBboxCoordinateOrder(bbox),
95100
datetime: this.makeDatetimePayload(dateRange),
96101
limit: 25

0 commit comments

Comments
 (0)