|
| 1 | +import type MockAdapter from 'axios-mock-adapter'; |
| 2 | +import fetchMock from 'fetch-mock-jest'; |
| 3 | +import { cloneDeep } from 'lodash'; |
| 4 | + |
| 5 | +import { SearchContextProvider } from '../../search-manager'; |
| 6 | +import { mockContentSearchConfig, mockSearchResult } from '../../search-manager/data/api.mock'; |
| 7 | +import { type CollectionHit, formatSearchHit } from '../../search-manager/data/api'; |
| 8 | +import { |
| 9 | + initializeMocks, |
| 10 | + render, |
| 11 | + screen, |
| 12 | + waitFor, |
| 13 | + within, |
| 14 | +} from '../../testUtils'; |
| 15 | +import mockResult from '../__mocks__/collection-search.json'; |
| 16 | +import { mockContentLibrary } from '../data/api.mocks'; |
| 17 | +import * as api from '../data/api'; |
| 18 | +import CollectionDetails from './CollectionDetails'; |
| 19 | + |
| 20 | +const searchEndpoint = 'http://mock.meilisearch.local/multi-search'; |
| 21 | +const mockCollection = { |
| 22 | + collectionId: mockResult.results[2].hits[0].block_id, |
| 23 | + collectionNoComponents: 'collection-no-components', |
| 24 | + collectionMultipleComponents: 'collection-multiple-components', |
| 25 | + title: mockResult.results[2].hits[0].display_name, |
| 26 | + description: mockResult.results[2].hits[0].description, |
| 27 | +}; |
| 28 | + |
| 29 | +let axiosMock: MockAdapter; |
| 30 | +let mockShowToast: (message: string) => void; |
| 31 | + |
| 32 | +mockContentSearchConfig.applyMock(); |
| 33 | + |
| 34 | +describe('<CollectionDetails />', () => { |
| 35 | + beforeEach(() => { |
| 36 | + const mocks = initializeMocks(); |
| 37 | + axiosMock = mocks.axiosMock; |
| 38 | + mockShowToast = mocks.mockShowToast; |
| 39 | + return; |
| 40 | + |
| 41 | + // The Meilisearch client-side API uses fetch, not Axios. |
| 42 | + fetchMock.post(searchEndpoint, (_url, req) => { |
| 43 | + const requestData = JSON.parse(req.body?.toString() ?? ''); |
| 44 | + const query = requestData?.queries[0]?.q ?? ''; |
| 45 | + const mockResultCopy = cloneDeep(mockResult); |
| 46 | + // We have to replace the query (search keywords) in the mock results with the actual query, |
| 47 | + // because otherwise Instantsearch will update the UI and change the query, |
| 48 | + // leading to unexpected results in the test cases. |
| 49 | + mockResultCopy.results[0].query = query; |
| 50 | + mockResultCopy.results[1].query = query; |
| 51 | + mockResultCopy.results[2].query = query; |
| 52 | + // And fake the required '_formatted' fields; it contains the highlighting <mark>...</mark> around matched words |
| 53 | + // eslint-disable-next-line no-underscore-dangle, no-param-reassign |
| 54 | + mockResultCopy.results[0]?.hits.forEach((hit) => { hit._formatted = { ...hit }; }); |
| 55 | + const collectionQueryId = requestData?.queries[2]?.filter[2]?.split('block_id = "')[1].split('"')[0]; |
| 56 | + mockResultCopy.results[0].description = requestData?.queries[2]?.filter[2]; |
| 57 | + switch (collectionQueryId) { |
| 58 | + case mockCollection.collectionNoComponents: |
| 59 | + mockResultCopy.results[1].facetDistribution.block_type = {}; |
| 60 | + break; |
| 61 | + case mockCollection.collectionMultipleComponents: |
| 62 | + mockResultCopy.results[1].facetDistribution.block_type = { |
| 63 | + annotatable: 1, |
| 64 | + chapter: 2, |
| 65 | + discussion: 3, |
| 66 | + drag_and_drop_v2: 4, |
| 67 | + html: 5, |
| 68 | + library_content: 6, |
| 69 | + openassessment: 7, |
| 70 | + problem: 8, |
| 71 | + sequential: 9, |
| 72 | + vertical: 10, |
| 73 | + video: 11, |
| 74 | + choiceresponse: 12, |
| 75 | + }; |
| 76 | + break; |
| 77 | + default: |
| 78 | + break; |
| 79 | + } |
| 80 | + return mockResultCopy; |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + afterEach(() => { |
| 85 | + jest.clearAllMocks(); |
| 86 | + axiosMock.restore(); |
| 87 | + fetchMock.mockReset(); |
| 88 | + }); |
| 89 | + |
| 90 | + const renderCollectionDetails = async (collectionId?: string) => { |
| 91 | + const colId = collectionId || mockCollection.collectionId; |
| 92 | + const collectionData: CollectionHit = formatSearchHit(mockResult.results[2].hits[0]) as CollectionHit; |
| 93 | + const library = mockContentLibrary.libraryData; |
| 94 | + |
| 95 | + render(( |
| 96 | + <SearchContextProvider> |
| 97 | + <CollectionDetails library={library} collection={collectionData} /> |
| 98 | + </SearchContextProvider> |
| 99 | + )); |
| 100 | + |
| 101 | + await waitFor(() => { expect(fetchMock).toHaveFetchedTimes(1, searchEndpoint, 'post'); }); |
| 102 | + }; |
| 103 | + |
| 104 | + it('should render Collection Detail', async () => { |
| 105 | + mockSearchResult(mockResult); |
| 106 | + await renderCollectionDetails(); |
| 107 | + |
| 108 | + // Collection Description |
| 109 | + expect(screen.getByText('Description / Card Preview Text')).toBeInTheDocument(); |
| 110 | + expect(screen.getByText(mockCollection.description)).toBeInTheDocument(); |
| 111 | + |
| 112 | + // Collection History |
| 113 | + expect(screen.getByText('Collection History')).toBeInTheDocument(); |
| 114 | + // Modified date |
| 115 | + expect(screen.getByText('September 20, 2024')).toBeInTheDocument(); |
| 116 | + // Created date |
| 117 | + expect(screen.getByText('September 19, 2024')).toBeInTheDocument(); |
| 118 | + }); |
| 119 | + |
| 120 | + it('should render Collection stats', async () => { |
| 121 | + mockSearchResult(mockResult); |
| 122 | + await renderCollectionDetails(); |
| 123 | + |
| 124 | + expect(screen.getByText('Collection Stats')).toBeInTheDocument(); |
| 125 | + expect(await screen.findByText('Total')).toBeInTheDocument(); |
| 126 | + |
| 127 | + [ |
| 128 | + { blockType: 'Total', count: 5 }, |
| 129 | + { blockType: 'Text', count: 4 }, |
| 130 | + { blockType: 'Problem', count: 1 }, |
| 131 | + ].forEach(({ blockType, count }) => { |
| 132 | + const blockCount = screen.getByText(blockType).closest('div') as HTMLDivElement; |
| 133 | + expect(within(blockCount).getByText(count.toString())).toBeInTheDocument(); |
| 134 | + }); |
| 135 | + }); |
| 136 | + |
| 137 | + it('should render Collection stats for empty collection', async () => { |
| 138 | + const mockResultCopy = cloneDeep(mockResult); |
| 139 | + mockResultCopy.results[1].facetDistribution.block_type = {}; |
| 140 | + mockSearchResult(mockResultCopy); |
| 141 | + await renderCollectionDetails(mockCollection.collectionNoComponents); |
| 142 | + |
| 143 | + expect(screen.getByText('Collection Stats')).toBeInTheDocument(); |
| 144 | + expect(await screen.findByText('This collection is currently empty.')).toBeInTheDocument(); |
| 145 | + }); |
| 146 | + |
| 147 | + it('should render Collection stats for big collection', async () => { |
| 148 | + const mockResultCopy = cloneDeep(mockResult); |
| 149 | + mockResultCopy.results[1].facetDistribution.block_type = { |
| 150 | + annotatable: 1, |
| 151 | + chapter: 2, |
| 152 | + discussion: 3, |
| 153 | + drag_and_drop_v2: 4, |
| 154 | + html: 5, |
| 155 | + library_content: 6, |
| 156 | + openassessment: 7, |
| 157 | + problem: 8, |
| 158 | + sequential: 9, |
| 159 | + vertical: 10, |
| 160 | + video: 11, |
| 161 | + choiceresponse: 12, |
| 162 | + }; |
| 163 | + mockSearchResult(mockResultCopy); |
| 164 | + await renderCollectionDetails(mockCollection.collectionMultipleComponents); |
| 165 | + |
| 166 | + expect(screen.getByText('Collection Stats')).toBeInTheDocument(); |
| 167 | + expect(await screen.findByText('78')).toBeInTheDocument(); |
| 168 | + |
| 169 | + [ |
| 170 | + { blockType: 'Total', count: 78 }, |
| 171 | + { blockType: 'Multiple Choice', count: 12 }, |
| 172 | + { blockType: 'Video', count: 11 }, |
| 173 | + { blockType: 'Unit', count: 10 }, |
| 174 | + { blockType: 'Other', count: 45 }, |
| 175 | + ].forEach(({ blockType, count }) => { |
| 176 | + const blockCount = screen.getByText(blockType).closest('div') as HTMLDivElement; |
| 177 | + expect(within(blockCount).getByText(count.toString())).toBeInTheDocument(); |
| 178 | + }); |
| 179 | + }); |
| 180 | +}); |
0 commit comments