Skip to content

Commit bb2337a

Browse files
committed
Update test to match changes
1 parent 3b6f2b2 commit bb2337a

File tree

3 files changed

+120
-78
lines changed

3 files changed

+120
-78
lines changed
Lines changed: 65 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,65 +3,81 @@ import { renderHook, act } from '@testing-library/react-hooks';
33
import useCollection from './useCollection';
44
import wrapper from './wrapper';
55

6-
describe('useCollection' ,() => {
6+
describe('useCollection', () => {
77
beforeEach(() => {
88
fetch.resetMocks();
99
});
1010

1111
it('queries collection', async () => {
1212
fetch
13-
.mockResponseOnce(JSON.stringify({ links: [] }), { url: 'https://fake-stac-api.net' })
14-
.mockResponseOnce(JSON.stringify({
15-
collections: [
16-
{id: 'abc', title: 'Collection A'},
17-
{id: 'def', title: 'Collection B'}
18-
]
19-
}));
13+
.mockResponseOnce(JSON.stringify({ links: [] }), {
14+
url: 'https://fake-stac-api.net'
15+
})
16+
.mockResponseOnce(
17+
JSON.stringify({
18+
id: 'abc',
19+
title: 'Collection A'
20+
})
21+
);
2022

2123
const { result, waitForNextUpdate } = renderHook(
2224
() => useCollection('abc'),
2325
{ wrapper }
2426
);
2527
await waitForNextUpdate();
26-
await waitForNextUpdate();
27-
expect(result.current.collection).toEqual({id: 'abc', title: 'Collection A'});
28+
expect(result.current.collection).toEqual({
29+
id: 'abc',
30+
title: 'Collection A'
31+
});
2832
expect(result.current.state).toEqual('IDLE');
2933
});
3034

3135
it('returns error if collection does not exist', async () => {
3236
fetch
33-
.mockResponseOnce(JSON.stringify({ links: [] }), { url: 'https://fake-stac-api.net' })
34-
.mockResponseOnce(JSON.stringify({
35-
collections: [
36-
{id: 'abc', title: 'Collection A'},
37-
{id: 'def', title: 'Collection B'}
38-
]
39-
}));
37+
.mockResponseOnce(JSON.stringify({ links: [] }), {
38+
url: 'https://fake-stac-api.net'
39+
})
40+
.mockResponseOnce(
41+
JSON.stringify({
42+
code: 'NotFoundError',
43+
description: 'Collection asdasd does not exist.'
44+
}),
45+
{
46+
status: 404,
47+
statusText: 'Not found'
48+
}
49+
);
4050

4151
const { result, waitForNextUpdate } = renderHook(
4252
() => useCollection('ghi'),
4353
{ wrapper }
4454
);
4555
await waitForNextUpdate();
46-
await waitForNextUpdate();
4756
expect(result.current.error).toEqual({
4857
status: 404,
4958
statusText: 'Not found',
50-
detail: 'Collection does not exist'
59+
detail: {
60+
code: 'NotFoundError',
61+
description: 'Collection asdasd does not exist.'
62+
}
5163
});
5264
});
5365

5466
it('handles error with JSON response', async () => {
5567
fetch
56-
.mockResponseOnce(JSON.stringify({ links: [] }), { url: 'https://fake-stac-api.net' })
57-
.mockResponseOnce(JSON.stringify({ error: 'Wrong query' }), { status: 400, statusText: 'Bad Request' });
68+
.mockResponseOnce(JSON.stringify({ links: [] }), {
69+
url: 'https://fake-stac-api.net'
70+
})
71+
.mockResponseOnce(JSON.stringify({ error: 'Wrong query' }), {
72+
status: 400,
73+
statusText: 'Bad Request'
74+
});
5875

5976
const { result, waitForNextUpdate } = renderHook(
6077
() => useCollection('abc'),
6178
{ wrapper }
6279
);
6380
await waitForNextUpdate();
64-
await waitForNextUpdate();
6581

6682
expect(result.current.error).toEqual({
6783
status: 400,
@@ -72,15 +88,19 @@ describe('useCollection' ,() => {
7288

7389
it('handles error with non-JSON response', async () => {
7490
fetch
75-
.mockResponseOnce(JSON.stringify({ links: [] }), { url: 'https://fake-stac-api.net' })
76-
.mockResponseOnce('Wrong query', { status: 400, statusText: 'Bad Request' });
91+
.mockResponseOnce(JSON.stringify({ links: [] }), {
92+
url: 'https://fake-stac-api.net'
93+
})
94+
.mockResponseOnce('Wrong query', {
95+
status: 400,
96+
statusText: 'Bad Request'
97+
});
7798

7899
const { result, waitForNextUpdate } = renderHook(
79100
() => useCollection('abc'),
80101
{ wrapper }
81102
);
82103
await waitForNextUpdate();
83-
await waitForNextUpdate();
84104

85105
expect(result.current.error).toEqual({
86106
status: 400,
@@ -91,31 +111,35 @@ describe('useCollection' ,() => {
91111

92112
it('reloads collection', async () => {
93113
fetch
94-
.mockResponseOnce(JSON.stringify({ links: [] }), { url: 'https://fake-stac-api.net' })
95-
.mockResponseOnce(JSON.stringify({
96-
collections: [
97-
{id: 'abc', title: 'Collection A'},
98-
{id: 'def', title: 'Collection B'}
99-
]
100-
}))
101-
.mockResponseOnce(JSON.stringify({
102-
collections: [
103-
{id: 'abc', title: 'Collection A - Updated'},
104-
{id: 'def', title: 'Collection B'}
105-
]
106-
}));
114+
.mockResponseOnce(JSON.stringify({ links: [] }), {
115+
url: 'https://fake-stac-api.net'
116+
})
117+
.mockResponseOnce(
118+
JSON.stringify({
119+
id: 'abc',
120+
title: 'Collection A'
121+
})
122+
)
123+
.mockResponseOnce(
124+
JSON.stringify({ id: 'abc', title: 'Collection A - Updated' })
125+
);
107126

108127
const { result, waitForNextUpdate } = renderHook(
109128
() => useCollection('abc'),
110129
{ wrapper }
111130
);
112131
await waitForNextUpdate();
113-
await waitForNextUpdate();
114-
expect(result.current.collection).toEqual({id: 'abc', title: 'Collection A'});
132+
expect(result.current.collection).toEqual({
133+
id: 'abc',
134+
title: 'Collection A'
135+
});
115136

116137
act(() => result.current.reload());
117138

118139
await waitForNextUpdate();
119-
expect(result.current.collection).toEqual({id: 'abc', title: 'Collection A - Updated'});
140+
expect(result.current.collection).toEqual({
141+
id: 'abc',
142+
title: 'Collection A - Updated'
143+
});
120144
});
121145
});

src/hooks/useCollections.test.ts

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,50 +10,58 @@ describe('useCollections', () => {
1010

1111
it('queries collections', async () => {
1212
fetch
13-
.mockResponseOnce(JSON.stringify({ links: [] }), { url: 'https://fake-stac-api.net' })
13+
.mockResponseOnce(JSON.stringify({ links: [] }), {
14+
url: 'https://fake-stac-api.net'
15+
})
1416
.mockResponseOnce(JSON.stringify({ data: '12345' }));
1517

16-
const { result, waitForNextUpdate } = renderHook(
17-
() => useCollections(),
18-
{ wrapper }
19-
);
18+
const { result, waitForNextUpdate } = renderHook(() => useCollections(), {
19+
wrapper
20+
});
2021
await waitForNextUpdate();
2122
await waitForNextUpdate();
22-
expect(fetch.mock.calls[1][0]).toEqual('https://fake-stac-api.net/collections');
23+
expect(fetch.mock.calls[1][0]).toEqual(
24+
'https://fake-stac-api.net/collections?limit=10'
25+
);
2326
expect(result.current.collections).toEqual({ data: '12345' });
2427
expect(result.current.state).toEqual('IDLE');
2528
});
2629

2730
it('reloads collections', async () => {
2831
fetch
29-
.mockResponseOnce(JSON.stringify({ links: [] }), { url: 'https://fake-stac-api.net' })
32+
.mockResponseOnce(JSON.stringify({ links: [] }), {
33+
url: 'https://fake-stac-api.net'
34+
})
3035
.mockResponseOnce(JSON.stringify({ data: 'original' }))
3136
.mockResponseOnce(JSON.stringify({ data: 'reloaded' }));
3237

33-
const { result, waitForNextUpdate } = renderHook(
34-
() => useCollections(),
35-
{ wrapper }
36-
);
38+
const { result, waitForNextUpdate } = renderHook(() => useCollections(), {
39+
wrapper
40+
});
3741
await waitForNextUpdate();
3842
await waitForNextUpdate();
3943
expect(result.current.collections).toEqual({ data: 'original' });
40-
44+
4145
expect(result.current.state).toEqual('IDLE');
4246
act(() => result.current.reload());
43-
47+
4448
await waitForNextUpdate();
4549
expect(result.current.collections).toEqual({ data: 'reloaded' });
4650
});
4751

4852
it('handles error with JSON response', async () => {
4953
fetch
50-
.mockResponseOnce(JSON.stringify({ links: [] }), { url: 'https://fake-stac-api.net' })
51-
.mockResponseOnce(JSON.stringify({ error: 'Wrong query' }), { status: 400, statusText: 'Bad Request' });
54+
.mockResponseOnce(JSON.stringify({ links: [] }), {
55+
url: 'https://fake-stac-api.net'
56+
})
57+
.mockResponseOnce(JSON.stringify({ error: 'Wrong query' }), {
58+
status: 400,
59+
statusText: 'Bad Request'
60+
});
5261

53-
const { result, waitForNextUpdate } = renderHook(
54-
() => useCollections(),
55-
{ wrapper }
56-
);
62+
const { result, waitForNextUpdate } = renderHook(() => useCollections(), {
63+
wrapper
64+
});
5765

5866
await waitForNextUpdate();
5967
await waitForNextUpdate();
@@ -67,13 +75,17 @@ describe('useCollections', () => {
6775

6876
it('handles error with non-JSON response', async () => {
6977
fetch
70-
.mockResponseOnce(JSON.stringify({ links: [] }), { url: 'https://fake-stac-api.net' })
71-
.mockResponseOnce('Wrong query', { status: 400, statusText: 'Bad Request' });
78+
.mockResponseOnce(JSON.stringify({ links: [] }), {
79+
url: 'https://fake-stac-api.net'
80+
})
81+
.mockResponseOnce('Wrong query', {
82+
status: 400,
83+
statusText: 'Bad Request'
84+
});
7285

73-
const { result, waitForNextUpdate } = renderHook(
74-
() => useCollections(),
75-
{ wrapper }
76-
);
86+
const { result, waitForNextUpdate } = renderHook(() => useCollections(), {
87+
wrapper
88+
});
7789
await waitForNextUpdate();
7890
await waitForNextUpdate();
7991
expect(result.current.error).toEqual({

src/hooks/useStacApi.test.ts

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,35 @@ describe('useStacApi', () => {
77
beforeEach(() => fetch.resetMocks());
88
it('initilises StacAPI', async () => {
99
fetch
10-
.mockResponseOnce(JSON.stringify({ links: [] }), { url: 'https://fake-stac-api.net' })
10+
.mockResponseOnce(JSON.stringify({ links: [] }), {
11+
url: 'https://fake-stac-api.net'
12+
})
1113
.mockResponseOnce(JSON.stringify({ data: '12345' }));
1214

13-
const { waitForNextUpdate } = renderHook(
14-
() => useCollections(),
15-
{ wrapper }
16-
);
15+
const { waitForNextUpdate } = renderHook(() => useCollections(), {
16+
wrapper
17+
});
1718
await waitForNextUpdate();
1819
await waitForNextUpdate();
19-
expect(fetch.mock.calls[1][0]).toEqual('https://fake-stac-api.net/collections');
20+
expect(fetch.mock.calls[1][0]).toEqual(
21+
'https://fake-stac-api.net/collections?limit=10'
22+
);
2023
});
2124

2225
it('initilises StacAPI with redirect URL', async () => {
2326
fetch
24-
.mockResponseOnce(JSON.stringify({ links: [] }), { url: 'https://fake-stac-api.net/redirect/' })
27+
.mockResponseOnce(JSON.stringify({ links: [] }), {
28+
url: 'https://fake-stac-api.net/redirect/'
29+
})
2530
.mockResponseOnce(JSON.stringify({ data: '12345' }));
2631

27-
const { waitForNextUpdate } = renderHook(
28-
() => useCollections(),
29-
{ wrapper }
30-
);
32+
const { waitForNextUpdate } = renderHook(() => useCollections(), {
33+
wrapper
34+
});
3135
await waitForNextUpdate();
3236
await waitForNextUpdate();
33-
expect(fetch.mock.calls[1][0]).toEqual('https://fake-stac-api.net/redirect/collections');
37+
expect(fetch.mock.calls[1][0]).toEqual(
38+
'https://fake-stac-api.net/redirect/collections?limit=10'
39+
);
3440
});
3541
});

0 commit comments

Comments
 (0)