@@ -9,7 +9,11 @@ import { of, throwError } from 'rxjs';
99import { ComponentFixture , TestBed } from '@angular/core/testing' ;
1010
1111import { CreateCollectionSubmission } from '@osf/features/collections/store/add-to-collection/add-to-collection.actions' ;
12+ import { CedarMetadataAttributes , CedarRecordDataBinding } from '@osf/features/metadata/models' ;
13+ import { CreateCedarMetadataRecord } from '@osf/features/metadata/store' ;
1214import { UpdateProjectPublicStatus } from '@osf/features/project/overview/store' ;
15+ import { ResourceType } from '@osf/shared/enums/resource-type.enum' ;
16+ import { CollectionSubmissionPayload } from '@osf/shared/models/collections/collection-submission-payload.model' ;
1317import { ToastService } from '@osf/shared/services/toast.service' ;
1418
1519import { provideOSFCore } from '@testing/osf.testing.provider' ;
@@ -19,20 +23,39 @@ import { ToastServiceMock, ToastServiceMockType } from '@testing/providers/toast
1923
2024import { AddToCollectionConfirmationDialogComponent } from './add-to-collection-confirmation-dialog.component' ;
2125
26+ const MOCK_CEDAR_DATA : CedarRecordDataBinding = {
27+ data : { '@context' : { } } as CedarMetadataAttributes ,
28+ id : 'template-1' ,
29+ isPublished : true ,
30+ } ;
31+
2232describe ( 'AddToCollectionConfirmationDialogComponent' , ( ) => {
2333 let component : AddToCollectionConfirmationDialogComponent ;
2434 let fixture : ComponentFixture < AddToCollectionConfirmationDialogComponent > ;
2535 let store : Store ;
2636 let dialogRef : DynamicDialogRef ;
2737 let toastService : ToastServiceMockType ;
28- let dialogConfig : { data : { payload ?: unknown ; project ?: { id : string ; isPublic : boolean } } } ;
38+ let dialogConfig : {
39+ data : {
40+ payload ?: CollectionSubmissionPayload ;
41+ project ?: { id : string ; isPublic : boolean } ;
42+ cedarData ?: CedarRecordDataBinding | null ;
43+ } ;
44+ } ;
45+
46+ const MOCK_PAYLOAD : CollectionSubmissionPayload = {
47+ collectionId : 'collection-1' ,
48+ projectId : 'project-1' ,
49+ userId : 'user-1' ,
50+ } ;
2951
3052 beforeEach ( ( ) => {
3153 toastService = ToastServiceMock . simple ( ) ;
3254 dialogConfig = {
3355 data : {
34- payload : { title : 'Submission' } ,
56+ payload : MOCK_PAYLOAD ,
3557 project : { id : 'project-1' , isPublic : false } ,
58+ cedarData : null ,
3659 } ,
3760 } ;
3861
@@ -69,13 +92,14 @@ describe('AddToCollectionConfirmationDialogComponent', () => {
6992 expect ( toastService . showSuccess ) . not . toHaveBeenCalled ( ) ;
7093 } ) ;
7194
72- it ( 'should update project public status and create submission when project is private' , ( ) => {
95+ it ( 'should update project public status then create submission when project is private and no Cedar data ' , ( ) => {
7396 vi . spyOn ( store , 'dispatch' ) . mockReturnValue ( of ( void 0 ) ) ;
7497
7598 component . handleAddToCollectionConfirm ( ) ;
7699
77100 expect ( store . dispatch ) . toHaveBeenCalledWith ( new UpdateProjectPublicStatus ( [ { id : 'project-1' , public : true } ] ) ) ;
78- expect ( store . dispatch ) . toHaveBeenCalledWith ( new CreateCollectionSubmission ( { title : 'Submission' } as any ) ) ;
101+ expect ( store . dispatch ) . toHaveBeenCalledWith ( new CreateCollectionSubmission ( MOCK_PAYLOAD ) ) ;
102+ expect ( store . dispatch ) . not . toHaveBeenCalledWith ( expect . any ( CreateCedarMetadataRecord ) ) ;
79103 expect ( dialogRef . close ) . toHaveBeenCalledWith ( true ) ;
80104 expect ( toastService . showSuccess ) . toHaveBeenCalledWith ( 'collections.addToCollection.confirmationDialogToastMessage' ) ;
81105 expect ( component . isSubmitting ( ) ) . toBe ( false ) ;
@@ -87,11 +111,34 @@ describe('AddToCollectionConfirmationDialogComponent', () => {
87111
88112 component . handleAddToCollectionConfirm ( ) ;
89113
90- expect ( store . dispatch ) . toHaveBeenCalledWith ( new CreateCollectionSubmission ( { title : 'Submission' } as any ) ) ;
114+ expect ( store . dispatch ) . toHaveBeenCalledWith ( new CreateCollectionSubmission ( MOCK_PAYLOAD ) ) ;
91115 expect ( store . dispatch ) . not . toHaveBeenCalledWith ( expect . any ( UpdateProjectPublicStatus ) ) ;
92116 expect ( dialogRef . close ) . toHaveBeenCalledWith ( true ) ;
93117 } ) ;
94118
119+ it ( 'should create Cedar record before submission when cedarData is present' , ( ) => {
120+ dialogConfig . data . cedarData = MOCK_CEDAR_DATA ;
121+ vi . spyOn ( store , 'dispatch' ) . mockReturnValue ( of ( void 0 ) ) ;
122+
123+ component . handleAddToCollectionConfirm ( ) ;
124+
125+ expect ( store . dispatch ) . toHaveBeenCalledWith (
126+ new CreateCedarMetadataRecord ( MOCK_CEDAR_DATA , 'project-1' , ResourceType . Project )
127+ ) ;
128+ expect ( store . dispatch ) . toHaveBeenCalledWith ( new CreateCollectionSubmission ( MOCK_PAYLOAD ) ) ;
129+ expect ( dialogRef . close ) . toHaveBeenCalledWith ( true ) ;
130+ } ) ;
131+
132+ it ( 'should not create Cedar record when cedarData is null' , ( ) => {
133+ dialogConfig . data . cedarData = null ;
134+ vi . spyOn ( store , 'dispatch' ) . mockReturnValue ( of ( void 0 ) ) ;
135+
136+ component . handleAddToCollectionConfirm ( ) ;
137+
138+ expect ( store . dispatch ) . not . toHaveBeenCalledWith ( expect . any ( CreateCedarMetadataRecord ) ) ;
139+ expect ( store . dispatch ) . toHaveBeenCalledWith ( new CreateCollectionSubmission ( MOCK_PAYLOAD ) ) ;
140+ } ) ;
141+
95142 it ( 'should reset submitting state on error' , ( ) => {
96143 vi . spyOn ( store , 'dispatch' ) . mockImplementation ( ( action ) => {
97144 if ( action instanceof CreateCollectionSubmission ) {
@@ -106,4 +153,20 @@ describe('AddToCollectionConfirmationDialogComponent', () => {
106153 expect ( dialogRef . close ) . not . toHaveBeenCalled ( ) ;
107154 expect ( toastService . showSuccess ) . not . toHaveBeenCalled ( ) ;
108155 } ) ;
156+
157+ it ( 'should reset submitting state on Cedar record creation error' , ( ) => {
158+ dialogConfig . data . cedarData = MOCK_CEDAR_DATA ;
159+ vi . spyOn ( store , 'dispatch' ) . mockImplementation ( ( action ) => {
160+ if ( action instanceof CreateCedarMetadataRecord ) {
161+ return throwError ( ( ) => new Error ( 'cedar fail' ) ) ;
162+ }
163+ return of ( void 0 ) ;
164+ } ) ;
165+
166+ component . handleAddToCollectionConfirm ( ) ;
167+
168+ expect ( component . isSubmitting ( ) ) . toBe ( false ) ;
169+ expect ( dialogRef . close ) . not . toHaveBeenCalled ( ) ;
170+ expect ( toastService . showSuccess ) . not . toHaveBeenCalled ( ) ;
171+ } ) ;
109172} ) ;
0 commit comments