@@ -7,27 +7,30 @@ import { mockContentSearchConfig, mockSearchResult } from '../../search-manager/
7
7
import { type CollectionHit , formatSearchHit } from '../../search-manager/data/api' ;
8
8
import {
9
9
initializeMocks ,
10
+ fireEvent ,
10
11
render ,
11
12
screen ,
12
13
waitFor ,
13
14
within ,
14
15
} from '../../testUtils' ;
15
16
import mockResult from '../__mocks__/collection-search.json' ;
17
+ import * as api from '../data/api' ;
16
18
import { mockContentLibrary } from '../data/api.mocks' ;
17
19
import CollectionDetails from './CollectionDetails' ;
18
20
19
21
const searchEndpoint = 'http://mock.meilisearch.local/multi-search' ;
20
22
21
23
let axiosMock : MockAdapter ;
22
- // let mockShowToast: (message: string) => void;
24
+ let mockShowToast : ( message : string ) => void ;
23
25
24
26
mockContentSearchConfig . applyMock ( ) ;
27
+ const library = mockContentLibrary . libraryData ;
25
28
26
29
describe ( '<CollectionDetails />' , ( ) => {
27
30
beforeEach ( ( ) => {
28
31
const mocks = initializeMocks ( ) ;
29
32
axiosMock = mocks . axiosMock ;
30
- // mockShowToast = mocks.mockShowToast;
33
+ mockShowToast = mocks . mockShowToast ;
31
34
} ) ;
32
35
33
36
afterEach ( ( ) => {
@@ -38,7 +41,6 @@ describe('<CollectionDetails />', () => {
38
41
39
42
const renderCollectionDetails = async ( ) => {
40
43
const collectionData : CollectionHit = formatSearchHit ( mockResult . results [ 2 ] . hits [ 0 ] ) as CollectionHit ;
41
- const library = mockContentLibrary . libraryData ;
42
44
43
45
render ( (
44
46
< SearchContextProvider >
@@ -49,7 +51,7 @@ describe('<CollectionDetails />', () => {
49
51
await waitFor ( ( ) => { expect ( fetchMock ) . toHaveFetchedTimes ( 1 , searchEndpoint , 'post' ) ; } ) ;
50
52
} ;
51
53
52
- it ( 'should render Collection Detail ' , async ( ) => {
54
+ it ( 'should render Collection Details ' , async ( ) => {
53
55
mockSearchResult ( mockResult ) ;
54
56
await renderCollectionDetails ( ) ;
55
57
@@ -66,6 +68,76 @@ describe('<CollectionDetails />', () => {
66
68
expect ( screen . getByText ( 'September 19, 2024' ) ) . toBeInTheDocument ( ) ;
67
69
} ) ;
68
70
71
+ it ( 'should allow modifying the description' , async ( ) => {
72
+ mockSearchResult ( mockResult ) ;
73
+ await renderCollectionDetails ( ) ;
74
+
75
+ const {
76
+ description : originalDescription ,
77
+ block_id : blockId ,
78
+ context_key : contextKey ,
79
+ } = mockResult . results [ 2 ] . hits [ 0 ] ;
80
+
81
+ expect ( screen . getByText ( originalDescription ) ) . toBeInTheDocument ( ) ;
82
+
83
+ const url = api . getLibraryCollectionApiUrl ( contextKey , blockId ) ;
84
+ axiosMock . onPatch ( url ) . reply ( 200 ) ;
85
+
86
+ const textArea = screen . getByRole ( 'textbox' ) ;
87
+
88
+ // Change the description to the same value
89
+ fireEvent . focus ( textArea ) ;
90
+ fireEvent . change ( textArea , { target : { value : originalDescription } } ) ;
91
+ fireEvent . blur ( textArea ) ;
92
+
93
+ await waitFor ( ( ) => {
94
+ expect ( axiosMock . history . patch ) . toHaveLength ( 0 ) ;
95
+ expect ( mockShowToast ) . not . toHaveBeenCalled ( ) ;
96
+ } ) ;
97
+
98
+ // Change the description to a new value
99
+ fireEvent . focus ( textArea ) ;
100
+ fireEvent . change ( textArea , { target : { value : 'New description' } } ) ;
101
+ fireEvent . blur ( textArea ) ;
102
+
103
+ await waitFor ( ( ) => {
104
+ expect ( axiosMock . history . patch ) . toHaveLength ( 1 ) ;
105
+ expect ( axiosMock . history . patch [ 0 ] . url ) . toEqual ( url ) ;
106
+ expect ( axiosMock . history . patch [ 0 ] . data ) . toEqual ( JSON . stringify ( { description : 'New description' } ) ) ;
107
+ expect ( mockShowToast ) . toHaveBeenCalledWith ( 'Collection updated successfully.' ) ;
108
+ } ) ;
109
+ } ) ;
110
+
111
+ it ( 'should show error while modifing the description' , async ( ) => {
112
+ mockSearchResult ( mockResult ) ;
113
+ await renderCollectionDetails ( ) ;
114
+
115
+ const {
116
+ description : originalDescription ,
117
+ block_id : blockId ,
118
+ context_key : contextKey ,
119
+ } = mockResult . results [ 2 ] . hits [ 0 ] ;
120
+
121
+ expect ( screen . getByText ( originalDescription ) ) . toBeInTheDocument ( ) ;
122
+
123
+ const url = api . getLibraryCollectionApiUrl ( contextKey , blockId ) ;
124
+ axiosMock . onPatch ( url ) . reply ( 500 ) ;
125
+
126
+ const textArea = screen . getByRole ( 'textbox' ) ;
127
+
128
+ // Change the description to a new value
129
+ fireEvent . focus ( textArea ) ;
130
+ fireEvent . change ( textArea , { target : { value : 'New description' } } ) ;
131
+ fireEvent . blur ( textArea ) ;
132
+
133
+ await waitFor ( ( ) => {
134
+ expect ( axiosMock . history . patch ) . toHaveLength ( 1 ) ;
135
+ expect ( axiosMock . history . patch [ 0 ] . url ) . toEqual ( url ) ;
136
+ expect ( axiosMock . history . patch [ 0 ] . data ) . toEqual ( JSON . stringify ( { description : 'New description' } ) ) ;
137
+ expect ( mockShowToast ) . toHaveBeenCalledWith ( 'Failed to update collection.' ) ;
138
+ } ) ;
139
+ } ) ;
140
+
69
141
it ( 'should render Collection stats' , async ( ) => {
70
142
mockSearchResult ( mockResult ) ;
71
143
await renderCollectionDetails ( ) ;
0 commit comments