Skip to content

Commit

Permalink
fix: Send selection collections as undefined is array is empty [fix #16]
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverroick committed Jan 25, 2023
1 parent adfcc7b commit b763302
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
16 changes: 16 additions & 0 deletions src/hooks/useStacSearch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,22 @@ describe('useStacSearch', () => {
expect(result.current.results).toEqual({ data: '12345' });
});

it('clears collections when array is empty', async () => {
fetch.mockResponseOnce(JSON.stringify({ data: '12345' }));

const { result, waitForNextUpdate } = renderHook(
() => useStacSearch(stacApi)
);

act(() => result.current.setCollections([]));
act(() => result.current.submit());
await waitForNextUpdate();

const postPayload = parseRequestPayload(fetch.mock.calls[0][1]);
expect(postPayload).toEqual({ limit: 25 });
expect(result.current.results).toEqual({ data: '12345' });
});

it('includes date range in search', async () => {
fetch.mockResponseOnce(JSON.stringify({ data: '12345' }));

Expand Down
9 changes: 7 additions & 2 deletions src/stac-api/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ApiError, GenericObject } from '../types';
import type { Bbox, SearchPayload, DateRange } from '../types/stac';
import type { Bbox, SearchPayload, DateRange, CollectionIdList } from '../types/stac';

type RequestPayload = SearchPayload;
type FetchOptions = {
Expand Down Expand Up @@ -36,6 +36,10 @@ class StacApi {
return sortedBbox;
}

makeCollectionsPayload(collections?: CollectionIdList) {
return collections?.length ? collections : undefined;
}

makeDatetimePayload(dateRange?: DateRange): string | undefined {
if (!dateRange) {
return undefined;
Expand Down Expand Up @@ -88,9 +92,10 @@ class StacApi {
}

search(payload: SearchPayload, headers = {}): Promise<Response> {
const { bbox, dateRange, ...restPayload } = payload;
const { bbox, dateRange, collections, ...restPayload } = payload;
const requestPayload = {
...restPayload,
collections: this.makeCollectionsPayload(collections),
bbox: this.fixBboxCoordinateOrder(bbox),
datetime: this.makeDatetimePayload(dateRange),
limit: 25
Expand Down

0 comments on commit b763302

Please sign in to comment.