Skip to content

Commit 226cd2d

Browse files
committed
test: add unit test
1 parent 386bf25 commit 226cd2d

File tree

2 files changed

+172
-5
lines changed

2 files changed

+172
-5
lines changed

src/sections/collection/edit-collection-dropdown/delete-collection-button/useDeleteCollection.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ export const useDeleteCollection = ({
3939
} catch (err: WriteError | unknown) {
4040
if (err instanceof WriteError) {
4141
const error = new JSDataverseWriteErrorHandler(err)
42-
const formattedError = error.getReasonWithoutStatusCode() ?? error.getErrorMessage()
42+
const formattedError =
43+
error.getReasonWithoutStatusCode() ?? /* istanbul ignore next */ error.getErrorMessage()
4344
setErrorDeletingCollection(formattedError)
4445
} else {
4546
setErrorDeletingCollection(t('defaultCollectionDeleteError'))

tests/component/sections/collection/edit-collection-dropdown/EditCollectionDropdown.spec.tsx

Lines changed: 170 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
import { WriteError } from '@iqss/dataverse-client-javascript'
2+
import { CollectionRepository } from '@/collection/domain/repositories/CollectionRepository'
13
import { EditCollectionDropdown } from '@/sections/collection/edit-collection-dropdown/EditCollectionDropdown'
24
import { CollectionMother } from '@tests/component/collection/domain/models/CollectionMother'
35
import { UpwardHierarchyNodeMother } from '@tests/component/shared/hierarchy/domain/models/UpwardHierarchyNodeMother'
46

7+
const collectionRepository = {} as CollectionRepository
8+
59
const PARENT_COLLECTION_ID = 'root'
610
const PARENT_COLLECTION_NAME = 'Root'
711
const PARENT_COLLECTION_CONTACT_EMAIL = 'root@test.com'
@@ -22,9 +26,18 @@ const rootCollection = CollectionMother.create({
2226
const openDropdown = () => cy.findByRole('button', { name: /Edit/i }).click()
2327

2428
describe('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: /General Information/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: /Featured Items/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: /Delete Collection/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: /Delete Collection/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: /Delete Collection/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: /Delete Collection/i }).click()
162+
cy.findByRole('dialog').should('exist')
163+
164+
cy.findByRole('button', { name: /Cancel/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: /Delete Collection/i }).click()
181+
cy.findByRole('dialog').should('exist')
182+
183+
cy.findByRole('button', { name: /Delete/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(/Your collection has been deleted./)
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: /Delete Collection/i }).click()
210+
cy.findByRole('dialog').should('exist')
211+
212+
cy.findByRole('button', { name: /Delete/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: /Delete Collection/i }).click()
231+
cy.findByRole('dialog').should('exist')
232+
233+
cy.findByRole('button', { name: /Delete/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

Comments
 (0)