11import type MockAdapter from 'axios-mock-adapter' ;
22import fetchMock from 'fetch-mock-jest' ;
3- import { cloneDeep } from 'lodash' ;
43
5- import { SearchContextProvider } from '../../search-manager' ;
6- import { mockContentSearchConfig , mockSearchResult } from '../../search-manager/data/api.mock' ;
4+ import { mockContentSearchConfig , mockGetBlockTypes } from '../../search-manager/data/api.mock' ;
75import { type CollectionHit , formatSearchHit } from '../../search-manager/data/api' ;
86import {
97 initializeMocks ,
@@ -18,13 +16,14 @@ import * as api from '../data/api';
1816import { mockContentLibrary } from '../data/api.mocks' ;
1917import CollectionDetails from './CollectionDetails' ;
2018
21- const searchEndpoint = 'http://mock.meilisearch.local/multi-search' ;
22-
2319let axiosMock : MockAdapter ;
2420let mockShowToast : ( message : string ) => void ;
2521
2622mockContentSearchConfig . applyMock ( ) ;
23+ mockGetBlockTypes . applyMock ( ) ;
24+
2725const library = mockContentLibrary . libraryData ;
26+ const collectionHit = formatSearchHit ( mockResult . results [ 2 ] . hits [ 0 ] ) as CollectionHit ;
2827
2928describe ( '<CollectionDetails />' , ( ) => {
3029 beforeEach ( ( ) => {
@@ -39,25 +38,12 @@ describe('<CollectionDetails />', () => {
3938 fetchMock . mockReset ( ) ;
4039 } ) ;
4140
42- const renderCollectionDetails = async ( ) => {
43- const collectionData : CollectionHit = formatSearchHit ( mockResult . results [ 2 ] . hits [ 0 ] ) as CollectionHit ;
44-
45- render ( (
46- < SearchContextProvider >
47- < CollectionDetails library = { library } collection = { collectionData } />
48- </ SearchContextProvider >
49- ) ) ;
50-
51- await waitFor ( ( ) => { expect ( fetchMock ) . toHaveFetchedTimes ( 1 , searchEndpoint , 'post' ) ; } ) ;
52- } ;
53-
5441 it ( 'should render Collection Details' , async ( ) => {
55- mockSearchResult ( mockResult ) ;
56- await renderCollectionDetails ( ) ;
42+ render ( < CollectionDetails library = { library } collection = { collectionHit } /> ) ;
5743
5844 // Collection Description
5945 expect ( screen . getByText ( 'Description / Card Preview Text' ) ) . toBeInTheDocument ( ) ;
60- const { description } = mockResult . results [ 2 ] . hits [ 0 ] ;
46+ const { description } = collectionHit ;
6147 expect ( screen . getByText ( description ) ) . toBeInTheDocument ( ) ;
6248
6349 // Collection History
@@ -69,14 +55,13 @@ describe('<CollectionDetails />', () => {
6955 } ) ;
7056
7157 it ( 'should allow modifying the description' , async ( ) => {
72- mockSearchResult ( mockResult ) ;
73- await renderCollectionDetails ( ) ;
58+ render ( < CollectionDetails library = { library } collection = { collectionHit } /> ) ;
7459
7560 const {
7661 description : originalDescription ,
77- block_id : blockId ,
78- context_key : contextKey ,
79- } = mockResult . results [ 2 ] . hits [ 0 ] ;
62+ blockId,
63+ contextKey,
64+ } = collectionHit ;
8065
8166 expect ( screen . getByText ( originalDescription ) ) . toBeInTheDocument ( ) ;
8267
@@ -109,14 +94,13 @@ describe('<CollectionDetails />', () => {
10994 } ) ;
11095
11196 it ( 'should show error while modifing the description' , async ( ) => {
112- mockSearchResult ( mockResult ) ;
113- await renderCollectionDetails ( ) ;
97+ render ( < CollectionDetails library = { library } collection = { collectionHit } /> ) ;
11498
11599 const {
116100 description : originalDescription ,
117- block_id : blockId ,
118- context_key : contextKey ,
119- } = mockResult . results [ 2 ] . hits [ 0 ] ;
101+ blockId,
102+ contextKey,
103+ } = collectionHit ;
120104
121105 expect ( screen . getByText ( originalDescription ) ) . toBeInTheDocument ( ) ;
122106
@@ -139,15 +123,15 @@ describe('<CollectionDetails />', () => {
139123 } ) ;
140124
141125 it ( 'should render Collection stats' , async ( ) => {
142- mockSearchResult ( mockResult ) ;
143- await renderCollectionDetails ( ) ;
126+ mockGetBlockTypes ( 'someBlocks' ) ;
127+ render ( < CollectionDetails library = { library } collection = { collectionHit } /> ) ;
144128
145129 expect ( screen . getByText ( 'Collection Stats' ) ) . toBeInTheDocument ( ) ;
146130 expect ( await screen . findByText ( 'Total' ) ) . toBeInTheDocument ( ) ;
147131
148132 [
149- { blockType : 'Total' , count : 5 } ,
150- { blockType : 'Text' , count : 4 } ,
133+ { blockType : 'Total' , count : 3 } ,
134+ { blockType : 'Text' , count : 2 } ,
151135 { blockType : 'Problem' , count : 1 } ,
152136 ] . forEach ( ( { blockType, count } ) => {
153137 const blockCount = screen . getByText ( blockType ) . closest ( 'div' ) as HTMLDivElement ;
@@ -156,32 +140,19 @@ describe('<CollectionDetails />', () => {
156140 } ) ;
157141
158142 it ( 'should render Collection stats for empty collection' , async ( ) => {
159- const mockResultCopy = cloneDeep ( mockResult ) ;
160- mockResultCopy . results [ 1 ] . facetDistribution . block_type = { } ;
161- mockSearchResult ( mockResultCopy ) ;
162- await renderCollectionDetails ( ) ;
143+ mockGetBlockTypes ( 'noBlocks' ) ;
144+ render ( < CollectionDetails library = { library } collection = { collectionHit } /> ) ;
163145
164146 expect ( screen . getByText ( 'Collection Stats' ) ) . toBeInTheDocument ( ) ;
165147 expect ( await screen . findByText ( 'This collection is currently empty.' ) ) . toBeInTheDocument ( ) ;
166148 } ) ;
167149
168150 it ( 'should render Collection stats for big collection' , async ( ) => {
169- const mockResultCopy = cloneDeep ( mockResult ) ;
170- mockResultCopy . results [ 1 ] . facetDistribution . block_type = {
171- advanced : 1 ,
172- discussion : 2 ,
173- library : 3 ,
174- drag_and_drop_v2 : 4 ,
175- openassessment : 5 ,
176- html : 6 ,
177- problem : 7 ,
178- video : 8 ,
179- } ;
180- mockSearchResult ( mockResultCopy ) ;
181- await renderCollectionDetails ( ) ;
151+ mockGetBlockTypes ( 'moreBlocks' ) ;
152+ render ( < CollectionDetails library = { library } collection = { collectionHit } /> ) ;
182153
183154 expect ( screen . getByText ( 'Collection Stats' ) ) . toBeInTheDocument ( ) ;
184- expect ( await screen . findByText ( '78 ' ) ) . toBeInTheDocument ( ) ;
155+ expect ( await screen . findByText ( '36 ' ) ) . toBeInTheDocument ( ) ;
185156
186157 [
187158 { blockType : 'Total' , count : 36 } ,
0 commit comments