Skip to content

Commit

Permalink
fix: Remove dependency on collections object in useCollections effect…
Browse files Browse the repository at this point in the history
… hook
  • Loading branch information
oliverroick committed Nov 23, 2023
1 parent 0945c08 commit a914f22
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 24 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@developmentseed/stac-react",
"version": "0.1.0-alpha.8",
"version": "0.1.0-alpha.8.1",
"description": "React components and hooks for building STAC-API front-ends",
"repository": "[email protected]:developmentseed/stac-react.git",
"author": "Oliver Roick <[email protected]>",
Expand Down
42 changes: 21 additions & 21 deletions src/hooks/useCollections.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,27 @@ describe('useCollections', () => {
expect(result.current.state).toEqual('IDLE');
});

it('reloads collections', async () => {
fetch
.mockResponseOnce(JSON.stringify({ links: [] }), { url: 'https://fake-stac-api.net' })
.mockResponseOnce(JSON.stringify({ data: 'original' }))
.mockResponseOnce(JSON.stringify({ data: 'reloaded' }));

const { result, waitForNextUpdate } = renderHook(
() => useCollections(),
{ wrapper }
);
await waitForNextUpdate();
await waitForNextUpdate();
expect(result.current.collections).toEqual({ data: 'original' });

expect(result.current.state).toEqual('IDLE');
act(() => result.current.reload());

await waitForNextUpdate();
expect(result.current.collections).toEqual({ data: 'reloaded' });
});

it('handles error with JSON response', async () => {
fetch
.mockResponseOnce(JSON.stringify({ links: [] }), { url: 'https://fake-stac-api.net' })
Expand Down Expand Up @@ -61,25 +82,4 @@ describe('useCollections', () => {
detail: 'Wrong query'
});
});

it('reloads collections', async () => {
fetch
.mockResponseOnce(JSON.stringify({ links: [] }), { url: 'https://fake-stac-api.net' })
.mockResponseOnce(JSON.stringify({ data: 'original' }))
.mockResponseOnce(JSON.stringify({ data: 'reloaded' }));

const { result, waitForNextUpdate } = renderHook(
() => useCollections(),
{ wrapper }
);
await waitForNextUpdate();
await waitForNextUpdate();
expect(result.current.collections).toEqual({ data: 'original' });

expect(result.current.state).toEqual('IDLE');
act(() => result.current.reload());

await waitForNextUpdate();
expect(result.current.collections).toEqual({ data: 'reloaded' });
});
});
4 changes: 2 additions & 2 deletions src/hooks/useCollections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ function useCollections(): StacCollectionsHook {

useEffect(
() => {
if (stacApi && !collections) {
if (stacApi) {
getCollections();
}
},
[getCollections, stacApi, collections]
[getCollections, stacApi]
);

return {
Expand Down

0 comments on commit a914f22

Please sign in to comment.