1+ import { WriteError } from '@iqss/dataverse-client-javascript'
2+ import { CollectionRepository } from '@/collection/domain/repositories/CollectionRepository'
13import { EditCollectionDropdown } from '@/sections/collection/edit-collection-dropdown/EditCollectionDropdown'
24import { CollectionMother } from '@tests/component/collection/domain/models/CollectionMother'
35import { UpwardHierarchyNodeMother } from '@tests/component/shared/hierarchy/domain/models/UpwardHierarchyNodeMother'
46
7+ const collectionRepository = { } as CollectionRepository
8+
59const PARENT_COLLECTION_ID = 'root'
610const PARENT_COLLECTION_NAME = 'Root'
711const PARENT_COLLECTION_CONTACT_EMAIL = 'root@test.com'
@@ -22,9 +26,18 @@ const rootCollection = CollectionMother.create({
2226const openDropdown = ( ) => cy . findByRole ( 'button' , { name : / E d i t / i } ) . click ( )
2327
2428describe ( 'EditCollectionDropdown' , ( ) => {
29+ beforeEach ( ( ) => {
30+ collectionRepository . delete = cy . stub ( ) . resolves ( undefined )
31+ } )
2532 describe ( 'dropdown header' , ( ) => {
2633 it ( 'shows the collection name and id, but not affiliaton if not present' , ( ) => {
27- cy . mountAuthenticated ( < EditCollectionDropdown collection = { rootCollection } /> )
34+ cy . mountAuthenticated (
35+ < EditCollectionDropdown
36+ collection = { rootCollection }
37+ collectionRepository = { collectionRepository }
38+ canUserDeleteCollection = { false }
39+ />
40+ )
2841
2942 openDropdown ( )
3043
@@ -46,7 +59,13 @@ describe('EditCollectionDropdown', () => {
4659 isFacetRoot : true ,
4760 isMetadataBlockRoot : true
4861 } )
49- cy . mountAuthenticated ( < EditCollectionDropdown collection = { rootCollectionWithAffiliation } /> )
62+ cy . mountAuthenticated (
63+ < EditCollectionDropdown
64+ collection = { rootCollectionWithAffiliation }
65+ collectionRepository = { collectionRepository }
66+ canUserDeleteCollection = { false }
67+ />
68+ )
5069
5170 openDropdown ( )
5271
@@ -57,18 +76,165 @@ describe('EditCollectionDropdown', () => {
5776 } )
5877
5978 it ( 'clicks the general info button' , ( ) => {
60- cy . mountAuthenticated ( < EditCollectionDropdown collection = { rootCollection } /> )
79+ cy . mountAuthenticated (
80+ < EditCollectionDropdown
81+ collection = { rootCollection }
82+ collectionRepository = { collectionRepository }
83+ canUserDeleteCollection = { false }
84+ />
85+ )
6186
6287 openDropdown ( )
6388
6489 cy . findByRole ( 'button' , { name : / G e n e r a l I n f o r m a t i o n / i } ) . click ( )
6590 } )
6691
6792 it ( 'clicks the featured items button' , ( ) => {
68- cy . mountAuthenticated ( < EditCollectionDropdown collection = { rootCollection } /> )
93+ cy . mountAuthenticated (
94+ < EditCollectionDropdown
95+ collection = { rootCollection }
96+ collectionRepository = { collectionRepository }
97+ canUserDeleteCollection = { false }
98+ />
99+ )
69100
70101 openDropdown ( )
71102
72103 cy . findByRole ( 'button' , { name : / F e a t u r e d I t e m s / i } ) . click ( )
73104 } )
105+
106+ describe ( 'delete button' , ( ) => {
107+ it ( 'shows the delete button if user can delete collection, collection is not root and collection has no data' , ( ) => {
108+ cy . mountAuthenticated (
109+ < EditCollectionDropdown
110+ collection = { CollectionMother . createSubCollectionWithNoChildObjects ( ) }
111+ collectionRepository = { collectionRepository }
112+ canUserDeleteCollection = { true }
113+ />
114+ )
115+
116+ openDropdown ( )
117+
118+ cy . findByRole ( 'button' , { name : / D e l e t e C o l l e c t i o n / i } ) . should ( 'exist' )
119+ // cy.findByRole('button', { name: /Confirm/i }).click()
120+ } )
121+
122+ it ( 'does not show the delete button if user cannot delete collection' , ( ) => {
123+ cy . mountAuthenticated (
124+ < EditCollectionDropdown
125+ collection = { CollectionMother . createSubCollectionWithNoChildObjects ( ) }
126+ collectionRepository = { collectionRepository }
127+ canUserDeleteCollection = { false }
128+ />
129+ )
130+
131+ openDropdown ( )
132+
133+ cy . findByRole ( 'button' , { name : / D e l e t e C o l l e c t i o n / i } ) . should ( 'not.exist' )
134+ } )
135+
136+ it ( 'does not show the delete button if collection is root' , ( ) => {
137+ cy . mountAuthenticated (
138+ < EditCollectionDropdown
139+ collection = { rootCollection }
140+ collectionRepository = { collectionRepository }
141+ canUserDeleteCollection = { true }
142+ />
143+ )
144+
145+ openDropdown ( )
146+
147+ cy . findByRole ( 'button' , { name : / D e l e t e C o l l e c t i o n / i } ) . should ( 'not.exist' )
148+ } )
149+
150+ it ( 'opens and close the delete collection confirmation modal' , ( ) => {
151+ cy . mountAuthenticated (
152+ < EditCollectionDropdown
153+ collection = { CollectionMother . createSubCollectionWithNoChildObjects ( ) }
154+ collectionRepository = { collectionRepository }
155+ canUserDeleteCollection = { true }
156+ />
157+ )
158+
159+ openDropdown ( )
160+
161+ cy . findByRole ( 'button' , { name : / D e l e t e C o l l e c t i o n / i } ) . click ( )
162+ cy . findByRole ( 'dialog' ) . should ( 'exist' )
163+
164+ cy . findByRole ( 'button' , { name : / C a n c e l / i } ) . click ( )
165+
166+ cy . findByRole ( 'dialog' ) . should ( 'not.exist' )
167+ } )
168+
169+ it ( 'closes the modal and shows toast success message when delete collection succeeds' , ( ) => {
170+ cy . mountAuthenticated (
171+ < EditCollectionDropdown
172+ collection = { CollectionMother . createSubCollectionWithNoChildObjects ( ) }
173+ collectionRepository = { collectionRepository }
174+ canUserDeleteCollection = { true }
175+ />
176+ )
177+
178+ openDropdown ( )
179+
180+ cy . findByRole ( 'button' , { name : / D e l e t e C o l l e c t i o n / i } ) . click ( )
181+ cy . findByRole ( 'dialog' ) . should ( 'exist' )
182+
183+ cy . findByRole ( 'button' , { name : / D e l e t e / i } ) . click ( )
184+
185+ // The loading spinner inside delete button
186+ cy . findByRole ( 'status' ) . should ( 'exist' )
187+
188+ cy . findByRole ( 'dialog' ) . should ( 'not.exist' )
189+ cy . findByText ( / Y o u r c o l l e c t i o n h a s b e e n d e l e t e d ./ )
190+ . should ( 'exist' )
191+ . should ( 'be.visible' )
192+ } )
193+
194+ it ( 'shows the js-dataverse WriteError message if delete collection fails with a js-dataverse WriteError' , ( ) => {
195+ collectionRepository . delete = cy
196+ . stub ( )
197+ . rejects ( new WriteError ( 'Testing delete error message.' ) )
198+
199+ cy . mountAuthenticated (
200+ < EditCollectionDropdown
201+ collection = { CollectionMother . createSubCollectionWithNoChildObjects ( ) }
202+ collectionRepository = { collectionRepository }
203+ canUserDeleteCollection = { true }
204+ />
205+ )
206+
207+ openDropdown ( )
208+
209+ cy . findByRole ( 'button' , { name : / D e l e t e C o l l e c t i o n / i } ) . click ( )
210+ cy . findByRole ( 'dialog' ) . should ( 'exist' )
211+
212+ cy . findByRole ( 'button' , { name : / D e l e t e / i } ) . click ( )
213+
214+ cy . findByText ( 'Testing delete error message.' ) . should ( 'exist' )
215+ } )
216+
217+ it ( 'shows the default error message if delete collection fails with not a js-dataverse WriteError' , ( ) => {
218+ collectionRepository . delete = cy . stub ( ) . rejects ( 'some error' )
219+
220+ cy . mountAuthenticated (
221+ < EditCollectionDropdown
222+ collection = { CollectionMother . createSubCollectionWithNoChildObjects ( ) }
223+ collectionRepository = { collectionRepository }
224+ canUserDeleteCollection = { true }
225+ />
226+ )
227+
228+ openDropdown ( )
229+
230+ cy . findByRole ( 'button' , { name : / D e l e t e C o l l e c t i o n / i } ) . click ( )
231+ cy . findByRole ( 'dialog' ) . should ( 'exist' )
232+
233+ cy . findByRole ( 'button' , { name : / D e l e t e / i } ) . click ( )
234+
235+ cy . findByText ( 'Something went wrong deleting the collection. Try again later.' ) . should (
236+ 'exist'
237+ )
238+ } )
239+ } )
74240} )
0 commit comments