1
1
import type MockAdapter from 'axios-mock-adapter' ;
2
+ import userEvent from '@testing-library/user-event'
2
3
3
4
import { mockCollectionHit } from '../../search-manager/data/api.mock' ;
4
5
import {
@@ -51,8 +52,8 @@ describe('<CollectionInfoHeader />', () => {
51
52
52
53
const textBox = screen . getByRole ( 'textbox' , { name : / t i t l e i n p u t / i } ) ;
53
54
54
- fireEvent . change ( textBox , { target : { value : 'New Collection Title' } } ) ;
55
- fireEvent . keyDown ( textBox , { key : 'Enter' , code : 'Enter' , charCode : 13 } ) ;
55
+ userEvent . clear ( textBox ) ;
56
+ userEvent . type ( textBox , 'New Collection Title{enter}' ) ;
56
57
57
58
await waitFor ( ( ) => {
58
59
expect ( axiosMock . history . patch [ 0 ] . url ) . toEqual ( url ) ;
@@ -63,6 +64,25 @@ describe('<CollectionInfoHeader />', () => {
63
64
expect ( mockShowToast ) . toHaveBeenCalledWith ( 'Collection updated successfully.' ) ;
64
65
} ) ;
65
66
67
+ it ( 'should not update collection title if title is the same' , async ( ) => {
68
+ const library = await mockContentLibrary ( mockContentLibrary . libraryId ) ;
69
+ render ( < CollectionInfoHeader library = { library } collection = { mockCollectionHit } /> ) ;
70
+ const url = api . getLibraryCollectionApiUrl ( library . id , mockCollectionHit . blockId ) ;
71
+ axiosMock . onPatch ( url ) . reply ( 200 ) ;
72
+
73
+ fireEvent . click ( screen . getByRole ( 'button' , { name : / e d i t c o l l e c t i o n t i t l e / i } ) ) ;
74
+
75
+ const textBox = screen . getByRole ( 'textbox' , { name : / t i t l e i n p u t / i } ) ;
76
+
77
+ userEvent . clear ( textBox ) ;
78
+ userEvent . type ( textBox , mockCollectionHit . displayName ) ;
79
+ userEvent . type ( textBox , '{enter}' ) ;
80
+
81
+ await waitFor ( ( ) => expect ( axiosMock . history . patch . length ) . toEqual ( 0 ) ) ;
82
+
83
+ expect ( textBox ) . not . toBeInTheDocument ( ) ;
84
+ } ) ;
85
+
66
86
it ( 'should close edit collection title on press Escape' , async ( ) => {
67
87
const library = await mockContentLibrary ( mockContentLibrary . libraryId ) ;
68
88
render ( < CollectionInfoHeader library = { library } collection = { mockCollectionHit } /> ) ;
@@ -73,8 +93,8 @@ describe('<CollectionInfoHeader />', () => {
73
93
74
94
const textBox = screen . getByRole ( 'textbox' , { name : / t i t l e i n p u t / i } ) ;
75
95
76
- fireEvent . change ( textBox , { target : { value : 'New Collection Title' } } ) ;
77
- fireEvent . keyDown ( textBox , { key : 'Escape' , code : 'Escape' , charCode : 27 } ) ;
96
+ userEvent . clear ( textBox ) ;
97
+ userEvent . type ( textBox , 'New Collection Title{esc}' ) ;
78
98
79
99
await waitFor ( ( ) => expect ( axiosMock . history . patch . length ) . toEqual ( 0 ) ) ;
80
100
@@ -91,8 +111,8 @@ describe('<CollectionInfoHeader />', () => {
91
111
92
112
const textBox = screen . getByRole ( 'textbox' , { name : / t i t l e i n p u t / i } ) ;
93
113
94
- fireEvent . change ( textBox , { target : { value : 'New Collection Title' } } ) ;
95
- fireEvent . keyDown ( textBox , { key : 'Enter' , code : 'Enter' , charCode : 13 } ) ;
114
+ userEvent . clear ( textBox ) ;
115
+ userEvent . type ( textBox , 'New Collection Title{enter}' ) ;
96
116
97
117
await waitFor ( ( ) => {
98
118
expect ( axiosMock . history . patch [ 0 ] . url ) . toEqual ( url ) ;
0 commit comments