11import { MockComponents , MockProvider } from 'ng-mocks' ;
22
3+ import { of } from 'rxjs' ;
4+
35import { ComponentFixture , TestBed } from '@angular/core/testing' ;
46import { ActivatedRoute , Router } from '@angular/router' ;
57
68import { UserSelectors } from '@core/store/user' ;
9+ import { RegistrationTab } from '@osf/features/registries/enums' ;
710import { RegistriesSelectors } from '@osf/features/registries/store' ;
811import { CustomPaginatorComponent } from '@osf/shared/components/custom-paginator/custom-paginator.component' ;
912import { RegistrationCardComponent } from '@osf/shared/components/registration-card/registration-card.component' ;
@@ -23,6 +26,8 @@ describe('MyRegistrationsComponent', () => {
2326 let fixture : ComponentFixture < MyRegistrationsComponent > ;
2427 let mockRouter : ReturnType < RouterMockBuilder [ 'build' ] > ;
2528 let mockActivatedRoute : Partial < ActivatedRoute > ;
29+ let customConfirmationService : jest . Mocked < CustomConfirmationService > ;
30+ let toastService : jest . Mocked < ToastService > ;
2631
2732 beforeEach ( async ( ) => {
2833 mockRouter = RouterMockBuilder . create ( ) . withUrl ( '/registries/me' ) . build ( ) ;
@@ -55,32 +60,101 @@ describe('MyRegistrationsComponent', () => {
5560
5661 fixture = TestBed . createComponent ( MyRegistrationsComponent ) ;
5762 component = fixture . componentInstance ;
63+ customConfirmationService = TestBed . inject ( CustomConfirmationService ) as jest . Mocked < CustomConfirmationService > ;
64+ toastService = TestBed . inject ( ToastService ) as jest . Mocked < ToastService > ;
5865 fixture . detectChanges ( ) ;
5966 } ) ;
6067
6168 it ( 'should create' , ( ) => {
6269 expect ( component ) . toBeTruthy ( ) ;
6370 } ) ;
6471
65- it ( 'should default to submitted tab and fetch submitted registrations' , ( ) => {
72+ it ( 'should default to submitted tab when no query param' , ( ) => {
73+ expect ( component . selectedTab ( ) ) . toBe ( RegistrationTab . Submitted ) ;
74+ } ) ;
75+
76+ it ( 'should switch to drafts tab when query param is drafts' , ( ) => {
77+ ( mockActivatedRoute . snapshot as any ) . queryParams = { tab : 'drafts' } ;
78+
79+ fixture = TestBed . createComponent ( MyRegistrationsComponent ) ;
80+ component = fixture . componentInstance ;
81+ fixture . detectChanges ( ) ;
82+
83+ expect ( component . selectedTab ( ) ) . toBe ( RegistrationTab . Drafts ) ;
84+ } ) ;
85+
86+ it ( 'should switch to submitted tab when query param is submitted' , ( ) => {
87+ ( mockActivatedRoute . snapshot as any ) . queryParams = { tab : 'submitted' } ;
88+
89+ fixture = TestBed . createComponent ( MyRegistrationsComponent ) ;
90+ component = fixture . componentInstance ;
91+ fixture . detectChanges ( ) ;
92+
93+ expect ( component . selectedTab ( ) ) . toBe ( RegistrationTab . Submitted ) ;
94+ } ) ;
95+
96+ it ( 'should handle tab change and update query params' , ( ) => {
6697 const actionsMock = {
6798 getDraftRegistrations : jest . fn ( ) ,
6899 getSubmittedRegistrations : jest . fn ( ) ,
69100 deleteDraft : jest . fn ( ) ,
70101 } as any ;
71102 Object . defineProperty ( component , 'actions' , { value : actionsMock } ) ;
103+ const navigateSpy = jest . spyOn ( mockRouter , 'navigate' ) ;
104+
105+ component . onTabChange ( RegistrationTab . Drafts ) ;
106+
107+ expect ( component . selectedTab ( ) ) . toBe ( RegistrationTab . Drafts ) ;
108+ expect ( component . draftFirst ) . toBe ( 0 ) ;
109+ expect ( actionsMock . getDraftRegistrations ) . toHaveBeenCalledWith ( ) ;
110+ expect ( navigateSpy ) . toHaveBeenCalledWith ( [ ] , {
111+ relativeTo : mockActivatedRoute ,
112+ queryParams : { tab : 'drafts' } ,
113+ queryParamsHandling : 'merge' ,
114+ } ) ;
115+ } ) ;
72116
73- component . selectedTab . set ( component . RegistrationTab . Drafts ) ;
74- fixture . detectChanges ( ) ;
75- component . selectedTab . set ( component . RegistrationTab . Submitted ) ;
76- fixture . detectChanges ( ) ;
77- expect ( actionsMock . getSubmittedRegistrations ) . toHaveBeenCalledWith ( 'user-1' ) ;
117+ it ( 'should handle tab change to submitted and update query params' , ( ) => {
118+ const actionsMock = {
119+ getDraftRegistrations : jest . fn ( ) ,
120+ getSubmittedRegistrations : jest . fn ( ) ,
121+ deleteDraft : jest . fn ( ) ,
122+ } as any ;
123+ Object . defineProperty ( component , 'actions' , { value : actionsMock } ) ;
124+ const navigateSpy = jest . spyOn ( mockRouter , 'navigate' ) ;
125+
126+ component . onTabChange ( RegistrationTab . Submitted ) ;
127+
128+ expect ( component . selectedTab ( ) ) . toBe ( RegistrationTab . Submitted ) ;
129+ expect ( component . submittedFirst ) . toBe ( 0 ) ;
130+ expect ( actionsMock . getSubmittedRegistrations ) . toHaveBeenCalledWith ( ) ;
131+ expect ( navigateSpy ) . toHaveBeenCalledWith ( [ ] , {
132+ relativeTo : mockActivatedRoute ,
133+ queryParams : { tab : 'submitted' } ,
134+ queryParamsHandling : 'merge' ,
135+ } ) ;
136+ } ) ;
137+
138+ it ( 'should not process tab change if tab is not a number' , ( ) => {
139+ const actionsMock = {
140+ getDraftRegistrations : jest . fn ( ) ,
141+ getSubmittedRegistrations : jest . fn ( ) ,
142+ deleteDraft : jest . fn ( ) ,
143+ } as any ;
144+ Object . defineProperty ( component , 'actions' , { value : actionsMock } ) ;
145+ const initialTab = component . selectedTab ( ) ;
146+
147+ component . onTabChange ( 'invalid' as any ) ;
148+
149+ expect ( component . selectedTab ( ) ) . toBe ( initialTab ) ;
150+ expect ( actionsMock . getDraftRegistrations ) . not . toHaveBeenCalled ( ) ;
151+ expect ( actionsMock . getSubmittedRegistrations ) . not . toHaveBeenCalled ( ) ;
78152 } ) ;
79153
80154 it ( 'should navigate to create registration page' , ( ) => {
81- const navSpy = jest . spyOn ( TestBed . inject ( Router ) , 'navigate' ) ;
155+ const navSpy = jest . spyOn ( mockRouter , 'navigate' ) ;
82156 component . goToCreateRegistration ( ) ;
83- expect ( navSpy ) . toHaveBeenCalledWith ( [ '/registries/ osf/ new' ] ) ;
157+ expect ( navSpy ) . toHaveBeenLastCalledWith ( [ '/registries' , ' osf' , ' new'] ) ;
84158 } ) ;
85159
86160 it ( 'should handle drafts pagination' , ( ) => {
@@ -95,23 +169,75 @@ describe('MyRegistrationsComponent', () => {
95169 const actionsMock = { getSubmittedRegistrations : jest . fn ( ) } as any ;
96170 Object . defineProperty ( component , 'actions' , { value : actionsMock } ) ;
97171 component . onSubmittedPageChange ( { page : 1 , first : 10 } as any ) ;
98- expect ( actionsMock . getSubmittedRegistrations ) . toHaveBeenCalledWith ( 'user-1' , 2 ) ;
172+ expect ( actionsMock . getSubmittedRegistrations ) . toHaveBeenCalledWith ( 2 ) ;
99173 expect ( component . submittedFirst ) . toBe ( 10 ) ;
100174 } ) ;
101175
102- it ( 'should switch to drafts tab based on query param and fetch drafts' , async ( ) => {
103- ( mockActivatedRoute . snapshot as any ) . queryParams = { tab : 'drafts' } ;
104- const actionsMock = { getDraftRegistrations : jest . fn ( ) , getSubmittedRegistrations : jest . fn ( ) } as any ;
105- fixture = TestBed . createComponent ( MyRegistrationsComponent ) ;
106- component = fixture . componentInstance ;
176+ it ( 'should delete draft after confirmation' , ( ) => {
177+ const actionsMock = {
178+ getDraftRegistrations : jest . fn ( ) ,
179+ getSubmittedRegistrations : jest . fn ( ) ,
180+ deleteDraft : jest . fn ( ( ) => of ( { } ) ) ,
181+ } as any ;
107182 Object . defineProperty ( component , 'actions' , { value : actionsMock } ) ;
108- fixture . detectChanges ( ) ;
109-
110- expect ( component . selectedTab ( ) ) . toBe ( 0 ) ;
111- component . selectedTab . set ( component . RegistrationTab . Submitted ) ;
112- fixture . detectChanges ( ) ;
113- component . selectedTab . set ( component . RegistrationTab . Drafts ) ;
114- fixture . detectChanges ( ) ;
183+ customConfirmationService . confirmDelete . mockImplementation ( ( { onConfirm } ) => {
184+ onConfirm ( ) ;
185+ } ) ;
186+
187+ component . onDeleteDraft ( 'draft-123' ) ;
188+
189+ expect ( customConfirmationService . confirmDelete ) . toHaveBeenCalledWith ( {
190+ headerKey : 'registries.deleteDraft' ,
191+ messageKey : 'registries.confirmDeleteDraft' ,
192+ onConfirm : expect . any ( Function ) ,
193+ } ) ;
194+ expect ( actionsMock . deleteDraft ) . toHaveBeenCalledWith ( 'draft-123' ) ;
115195 expect ( actionsMock . getDraftRegistrations ) . toHaveBeenCalled ( ) ;
196+ expect ( toastService . showSuccess ) . toHaveBeenCalledWith ( 'registries.successDeleteDraft' ) ;
197+ } ) ;
198+
199+ it ( 'should not delete draft if confirmation is cancelled' , ( ) => {
200+ const actionsMock = {
201+ getDraftRegistrations : jest . fn ( ) ,
202+ getSubmittedRegistrations : jest . fn ( ) ,
203+ deleteDraft : jest . fn ( ) ,
204+ } as any ;
205+ Object . defineProperty ( component , 'actions' , { value : actionsMock } ) ;
206+ customConfirmationService . confirmDelete . mockImplementation ( ( ) => { } ) ;
207+
208+ component . onDeleteDraft ( 'draft-123' ) ;
209+
210+ expect ( customConfirmationService . confirmDelete ) . toHaveBeenCalled ( ) ;
211+ expect ( actionsMock . deleteDraft ) . not . toHaveBeenCalled ( ) ;
212+ expect ( actionsMock . getDraftRegistrations ) . not . toHaveBeenCalled ( ) ;
213+ expect ( toastService . showSuccess ) . not . toHaveBeenCalled ( ) ;
214+ } ) ;
215+
216+ it ( 'should reset draftFirst when switching to drafts tab' , ( ) => {
217+ component . draftFirst = 20 ;
218+ const actionsMock = {
219+ getDraftRegistrations : jest . fn ( ) ,
220+ getSubmittedRegistrations : jest . fn ( ) ,
221+ deleteDraft : jest . fn ( ) ,
222+ } as any ;
223+ Object . defineProperty ( component , 'actions' , { value : actionsMock } ) ;
224+
225+ component . onTabChange ( RegistrationTab . Drafts ) ;
226+
227+ expect ( component . draftFirst ) . toBe ( 0 ) ;
228+ } ) ;
229+
230+ it ( 'should reset submittedFirst when switching to submitted tab' , ( ) => {
231+ component . submittedFirst = 20 ;
232+ const actionsMock = {
233+ getDraftRegistrations : jest . fn ( ) ,
234+ getSubmittedRegistrations : jest . fn ( ) ,
235+ deleteDraft : jest . fn ( ) ,
236+ } as any ;
237+ Object . defineProperty ( component , 'actions' , { value : actionsMock } ) ;
238+
239+ component . onTabChange ( RegistrationTab . Submitted ) ;
240+
241+ expect ( component . submittedFirst ) . toBe ( 0 ) ;
116242 } ) ;
117243} ) ;
0 commit comments