Skip to content

Commit 8b8fb30

Browse files
committed
test: fix coverage
1 parent 4679ef3 commit 8b8fb30

2 files changed

Lines changed: 37 additions & 2 deletions

File tree

src/search-manager/data/api.mock.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export async function mockContentSearchConfig(): ReturnType<typeof api.getConten
1616
};
1717
}
1818
mockContentSearchConfig.multisearchEndpointUrl = 'http://mock.meilisearch.local/multi-search';
19+
mockContentSearchConfig.searchEndpointUrl = 'http://mock.meilisearch.local/indexes/studio/search';
1920
mockContentSearchConfig.applyMock = () => (
2021
jest.spyOn(api, 'getContentSearchConfig').mockImplementation(mockContentSearchConfig)
2122
);
@@ -102,3 +103,24 @@ mockFetchIndexDocuments.applyMock = () => {
102103
{ overwriteRoutes: true },
103104
);
104105
};
106+
107+
/**
108+
* Mock the useGetContentHits
109+
*/
110+
export async function mockGetContentHits(
111+
mockResponse: 'noHits' | 'someHits',
112+
) {
113+
fetchMock.post(mockContentSearchConfig.searchEndpointUrl, () => {
114+
const mockResponseMap = {
115+
noHits: {
116+
hits: [],
117+
estimatedTotalHits: 0,
118+
},
119+
someHits: {
120+
hits: [{ usage_key: 'some-key' }, { usage_key: 'other-key' }],
121+
estimatedTotalHits: 2,
122+
},
123+
};
124+
return mockResponseMap[mockResponse];
125+
});
126+
}

src/search-manager/data/apiHooks.test.tsx

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import { renderHook, waitFor } from '@testing-library/react';
33
import fetchMock from 'fetch-mock-jest';
44

55
import mockResult from './__mocks__/block-types.json';
6-
import { mockContentSearchConfig } from './api.mock';
6+
import { mockContentSearchConfig, mockGetContentHits } from './api.mock';
77
import {
8-
useGetBlockTypes,
8+
useGetBlockTypes, useGetContentHits,
99
} from './apiHooks';
1010

1111
mockContentSearchConfig.applyMock();
@@ -53,4 +53,17 @@ describe('search manager api hooks', () => {
5353
expect(result.current.data).toEqual(expectedData);
5454
expect(fetchMock.calls().length).toEqual(1);
5555
});
56+
57+
it('useGetContentHits should return hits', async () => {
58+
mockGetContentHits('someHits');
59+
const { result } = renderHook(() => useGetContentHits('filter'), { wrapper });
60+
await waitFor(() => {
61+
expect(result.current.isPending).toBeFalsy();
62+
});
63+
const expectedData = {
64+
hits: [{ usage_key: 'some-key' }, { usage_key: 'other-key' }],
65+
estimatedTotalHits: 2,
66+
};
67+
expect(result.current.data).toEqual(expectedData);
68+
});
5669
});

0 commit comments

Comments
 (0)