Skip to content

Commit 0a0530f

Browse files
committed
test: add new test for block types hook
1 parent 89f0e08 commit 0a0530f

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"comment": "This is a mock of the response from Meilisearch, based on an actual search in Studio.",
3+
"results": [
4+
{
5+
"indexUid": "studio",
6+
"hits": [],
7+
"query": "",
8+
"processingTimeMs": 1,
9+
"limit": 0,
10+
"offset": 0,
11+
"estimatedTotalHits": 0,
12+
"facetDistribution": {
13+
"block_type": {
14+
"chapter": 1,
15+
"html": 2,
16+
"problem": 16,
17+
"vertical": 2,
18+
"video": 1
19+
},
20+
"content.problem_types": {
21+
"choiceresponse": 2,
22+
"multiplechoiceresponse": 6,
23+
"numericalresponse": 3,
24+
"optionresponse": 4,
25+
"stringresponse": 1
26+
}
27+
},
28+
"facetStats": {}
29+
}
30+
]
31+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
2+
import { waitFor } from '@testing-library/react';
3+
import { renderHook } from '@testing-library/react-hooks';
4+
import fetchMock from 'fetch-mock-jest';
5+
6+
import mockResult from './__mocks__/block-types.json';
7+
import { mockContentSearchConfig } from './api.mock';
8+
import {
9+
useGetBlockTypes,
10+
} from './apiHooks';
11+
12+
mockContentSearchConfig.applyMock();
13+
14+
const queryClient = new QueryClient({
15+
defaultOptions: {
16+
queries: {
17+
retry: false,
18+
},
19+
},
20+
});
21+
22+
const wrapper = ({ children }) => (
23+
<QueryClientProvider client={queryClient}>
24+
{children}
25+
</QueryClientProvider>
26+
);
27+
28+
const fetchMockResponse = () => {
29+
fetchMock.post(
30+
mockContentSearchConfig.searchEndpointUrl,
31+
() => mockResult,
32+
{ overwriteRoutes: true },
33+
);
34+
};
35+
36+
describe('search manager api hooks', () => {
37+
afterEach(() => {
38+
fetchMock.reset();
39+
});
40+
41+
it('it should return block types facet', async () => {
42+
fetchMockResponse();
43+
const { result } = renderHook(() => useGetBlockTypes('filter'), { wrapper });
44+
await waitFor(() => {
45+
expect(result.current.isLoading).toBeFalsy();
46+
});
47+
const expectedData = {
48+
chapter: 1,
49+
html: 2,
50+
problem: 16,
51+
vertical: 2,
52+
video: 1,
53+
};
54+
expect(result.current.data).toEqual(expectedData);
55+
expect(fetchMock.calls().length).toEqual(1);
56+
});
57+
});

0 commit comments

Comments
 (0)