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,14 @@ 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 ( ) ;
23
+ mockGetBlockTypes . applyMock ( ) ;
24
+
27
25
const library = mockContentLibrary . libraryData ;
26
+ const collectionHit = formatSearchHit ( mockResult . results [ 2 ] . hits [ 0 ] ) as CollectionHit ;
28
27
29
28
describe ( '<CollectionDetails />' , ( ) => {
30
29
beforeEach ( ( ) => {
@@ -39,25 +38,12 @@ describe('<CollectionDetails />', () => {
39
38
fetchMock . mockReset ( ) ;
40
39
} ) ;
41
40
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
41
it ( 'should render Collection Details' , async ( ) => {
55
- mockSearchResult ( mockResult ) ;
56
- await renderCollectionDetails ( ) ;
42
+ render ( < CollectionDetails library = { library } collection = { collectionHit } /> ) ;
57
43
58
44
// Collection Description
59
45
expect ( screen . getByText ( 'Description / Card Preview Text' ) ) . toBeInTheDocument ( ) ;
60
- const { description } = mockResult . results [ 2 ] . hits [ 0 ] ;
46
+ const { description } = collectionHit ;
61
47
expect ( screen . getByText ( description ) ) . toBeInTheDocument ( ) ;
62
48
63
49
// Collection History
@@ -69,14 +55,13 @@ describe('<CollectionDetails />', () => {
69
55
} ) ;
70
56
71
57
it ( 'should allow modifying the description' , async ( ) => {
72
- mockSearchResult ( mockResult ) ;
73
- await renderCollectionDetails ( ) ;
58
+ render ( < CollectionDetails library = { library } collection = { collectionHit } /> ) ;
74
59
75
60
const {
76
61
description : originalDescription ,
77
- block_id : blockId ,
78
- context_key : contextKey ,
79
- } = mockResult . results [ 2 ] . hits [ 0 ] ;
62
+ blockId,
63
+ contextKey,
64
+ } = collectionHit ;
80
65
81
66
expect ( screen . getByText ( originalDescription ) ) . toBeInTheDocument ( ) ;
82
67
@@ -109,14 +94,13 @@ describe('<CollectionDetails />', () => {
109
94
} ) ;
110
95
111
96
it ( 'should show error while modifing the description' , async ( ) => {
112
- mockSearchResult ( mockResult ) ;
113
- await renderCollectionDetails ( ) ;
97
+ render ( < CollectionDetails library = { library } collection = { collectionHit } /> ) ;
114
98
115
99
const {
116
100
description : originalDescription ,
117
- block_id : blockId ,
118
- context_key : contextKey ,
119
- } = mockResult . results [ 2 ] . hits [ 0 ] ;
101
+ blockId,
102
+ contextKey,
103
+ } = collectionHit ;
120
104
121
105
expect ( screen . getByText ( originalDescription ) ) . toBeInTheDocument ( ) ;
122
106
@@ -139,15 +123,15 @@ describe('<CollectionDetails />', () => {
139
123
} ) ;
140
124
141
125
it ( 'should render Collection stats' , async ( ) => {
142
- mockSearchResult ( mockResult ) ;
143
- await renderCollectionDetails ( ) ;
126
+ mockGetBlockTypes ( 'someBlocks' ) ;
127
+ render ( < CollectionDetails library = { library } collection = { collectionHit } /> ) ;
144
128
145
129
expect ( screen . getByText ( 'Collection Stats' ) ) . toBeInTheDocument ( ) ;
146
130
expect ( await screen . findByText ( 'Total' ) ) . toBeInTheDocument ( ) ;
147
131
148
132
[
149
- { blockType : 'Total' , count : 5 } ,
150
- { blockType : 'Text' , count : 4 } ,
133
+ { blockType : 'Total' , count : 3 } ,
134
+ { blockType : 'Text' , count : 2 } ,
151
135
{ blockType : 'Problem' , count : 1 } ,
152
136
] . forEach ( ( { blockType, count } ) => {
153
137
const blockCount = screen . getByText ( blockType ) . closest ( 'div' ) as HTMLDivElement ;
@@ -156,32 +140,19 @@ describe('<CollectionDetails />', () => {
156
140
} ) ;
157
141
158
142
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 } /> ) ;
163
145
164
146
expect ( screen . getByText ( 'Collection Stats' ) ) . toBeInTheDocument ( ) ;
165
147
expect ( await screen . findByText ( 'This collection is currently empty.' ) ) . toBeInTheDocument ( ) ;
166
148
} ) ;
167
149
168
150
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 } /> ) ;
182
153
183
154
expect ( screen . getByText ( 'Collection Stats' ) ) . toBeInTheDocument ( ) ;
184
- expect ( await screen . findByText ( '78 ' ) ) . toBeInTheDocument ( ) ;
155
+ expect ( await screen . findByText ( '36 ' ) ) . toBeInTheDocument ( ) ;
185
156
186
157
[
187
158
{ blockType : 'Total' , count : 36 } ,
0 commit comments