1
1
import type MockAdapter from 'axios-mock-adapter' ;
2
2
import fetchMock from 'fetch-mock-jest' ;
3
- import { cloneDeep } from 'lodash' ;
4
3
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' ;
7
5
import { type CollectionHit , formatSearchHit } from '../../search-manager/data/api' ;
8
6
import {
9
7
initializeMocks ,
@@ -18,13 +16,12 @@ import * as api from '../data/api';
18
16
import { mockContentLibrary } from '../data/api.mocks' ;
19
17
import CollectionDetails from './CollectionDetails' ;
20
18
21
- const searchEndpoint = 'http://mock.meilisearch.local/multi-search' ;
22
-
23
19
let axiosMock : MockAdapter ;
24
20
let mockShowToast : ( message : string ) => void ;
25
21
26
22
mockContentSearchConfig . applyMock ( ) ;
27
23
const library = mockContentLibrary . libraryData ;
24
+ const collectionHit = formatSearchHit ( mockResult . results [ 2 ] . hits [ 0 ] ) as CollectionHit ;
28
25
29
26
describe ( '<CollectionDetails />' , ( ) => {
30
27
beforeEach ( ( ) => {
@@ -39,25 +36,12 @@ describe('<CollectionDetails />', () => {
39
36
fetchMock . mockReset ( ) ;
40
37
} ) ;
41
38
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
-
54
39
it ( 'should render Collection Details' , async ( ) => {
55
- mockSearchResult ( mockResult ) ;
56
- await renderCollectionDetails ( ) ;
40
+ render ( < CollectionDetails library = { library } collection = { collectionHit } /> ) ;
57
41
58
42
// Collection Description
59
43
expect ( screen . getByText ( 'Description / Card Preview Text' ) ) . toBeInTheDocument ( ) ;
60
- const { description } = mockResult . results [ 2 ] . hits [ 0 ] ;
44
+ const { description } = collectionHit ;
61
45
expect ( screen . getByText ( description ) ) . toBeInTheDocument ( ) ;
62
46
63
47
// Collection History
@@ -69,14 +53,13 @@ describe('<CollectionDetails />', () => {
69
53
} ) ;
70
54
71
55
it ( 'should allow modifying the description' , async ( ) => {
72
- mockSearchResult ( mockResult ) ;
73
- await renderCollectionDetails ( ) ;
56
+ render ( < CollectionDetails library = { library } collection = { collectionHit } /> ) ;
74
57
75
58
const {
76
59
description : originalDescription ,
77
- block_id : blockId ,
78
- context_key : contextKey ,
79
- } = mockResult . results [ 2 ] . hits [ 0 ] ;
60
+ blockId,
61
+ contextKey,
62
+ } = collectionHit ;
80
63
81
64
expect ( screen . getByText ( originalDescription ) ) . toBeInTheDocument ( ) ;
82
65
@@ -109,14 +92,13 @@ describe('<CollectionDetails />', () => {
109
92
} ) ;
110
93
111
94
it ( 'should show error while modifing the description' , async ( ) => {
112
- mockSearchResult ( mockResult ) ;
113
- await renderCollectionDetails ( ) ;
95
+ render ( < CollectionDetails library = { library } collection = { collectionHit } /> ) ;
114
96
115
97
const {
116
98
description : originalDescription ,
117
- block_id : blockId ,
118
- context_key : contextKey ,
119
- } = mockResult . results [ 2 ] . hits [ 0 ] ;
99
+ blockId,
100
+ contextKey,
101
+ } = collectionHit ;
120
102
121
103
expect ( screen . getByText ( originalDescription ) ) . toBeInTheDocument ( ) ;
122
104
@@ -139,15 +121,15 @@ describe('<CollectionDetails />', () => {
139
121
} ) ;
140
122
141
123
it ( 'should render Collection stats' , async ( ) => {
142
- mockSearchResult ( mockResult ) ;
143
- await renderCollectionDetails ( ) ;
124
+ mockGetBlockTypes ( 'someBlocks' ) ;
125
+ render ( < CollectionDetails library = { library } collection = { collectionHit } /> ) ;
144
126
145
127
expect ( screen . getByText ( 'Collection Stats' ) ) . toBeInTheDocument ( ) ;
146
128
expect ( await screen . findByText ( 'Total' ) ) . toBeInTheDocument ( ) ;
147
129
148
130
[
149
- { blockType : 'Total' , count : 5 } ,
150
- { blockType : 'Text' , count : 4 } ,
131
+ { blockType : 'Total' , count : 3 } ,
132
+ { blockType : 'Text' , count : 2 } ,
151
133
{ blockType : 'Problem' , count : 1 } ,
152
134
] . forEach ( ( { blockType, count } ) => {
153
135
const blockCount = screen . getByText ( blockType ) . closest ( 'div' ) as HTMLDivElement ;
@@ -156,32 +138,19 @@ describe('<CollectionDetails />', () => {
156
138
} ) ;
157
139
158
140
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 ( ) ;
141
+ mockGetBlockTypes ( 'noBlocks' ) ;
142
+ render ( < CollectionDetails library = { library } collection = { collectionHit } /> ) ;
163
143
164
144
expect ( screen . getByText ( 'Collection Stats' ) ) . toBeInTheDocument ( ) ;
165
145
expect ( await screen . findByText ( 'This collection is currently empty.' ) ) . toBeInTheDocument ( ) ;
166
146
} ) ;
167
147
168
148
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 ( ) ;
149
+ mockGetBlockTypes ( 'moreBlocks' ) ;
150
+ render ( < CollectionDetails library = { library } collection = { collectionHit } /> ) ;
182
151
183
152
expect ( screen . getByText ( 'Collection Stats' ) ) . toBeInTheDocument ( ) ;
184
- expect ( await screen . findByText ( '78 ' ) ) . toBeInTheDocument ( ) ;
153
+ expect ( await screen . findByText ( '36 ' ) ) . toBeInTheDocument ( ) ;
185
154
186
155
[
187
156
{ blockType : 'Total' , count : 36 } ,
0 commit comments