1- import { provideStore } from '@ngxs/store' ;
1+ import { Store } from '@ngxs/store' ;
22
3- import { MockComponent } from 'ng-mocks' ;
3+ import { TranslatePipe } from '@ngx-translate/core' ;
4+ import { MockComponents , MockPipe , MockProvider } from 'ng-mocks' ;
45
56import { provideHttpClient } from '@angular/common/http' ;
67import { provideHttpClientTesting } from '@angular/common/http/testing' ;
78import { ComponentFixture , TestBed } from '@angular/core/testing' ;
89import { provideNoopAnimations } from '@angular/platform-browser/animations' ;
910
10- import { UserState } from '@osf/core/store/user' ;
11+ import { UserSelectors } from '@osf/core/store/user' ;
12+ import {
13+ AffiliatedInstitutionsComponent ,
14+ ChangePasswordComponent ,
15+ ConnectedEmailsComponent ,
16+ ConnectedIdentitiesComponent ,
17+ DeactivateAccountComponent ,
18+ DefaultStorageLocationComponent ,
19+ ShareIndexingComponent ,
20+ TwoFactorAuthComponent ,
21+ } from '@osf/features/settings/account-settings/components' ;
22+ import { AccountSettingsSelectors } from '@osf/features/settings/account-settings/store' ;
1123import { SubHeaderComponent } from '@osf/shared/components' ;
24+ import { MOCK_STORE , MOCK_USER , MockCustomConfirmationServiceProvider , TranslateServiceMock } from '@shared/mocks' ;
25+ import { ToastService } from '@shared/services' ;
1226
1327import { AccountSettingsComponent } from './account-settings.component' ;
1428
1529describe ( 'AccountSettingsComponent' , ( ) => {
1630 let component : AccountSettingsComponent ;
1731 let fixture : ComponentFixture < AccountSettingsComponent > ;
32+ const store = MOCK_STORE ;
1833
1934 beforeEach ( async ( ) => {
35+ store . selectSignal . mockImplementation ( ( selector ) => {
36+ switch ( selector ) {
37+ case UserSelectors . getCurrentUser :
38+ return ( ) => MOCK_USER ;
39+
40+ case AccountSettingsSelectors . getEmails :
41+ return ( ) => [ ] ;
42+
43+ case AccountSettingsSelectors . getAccountSettings :
44+ return ( ) => null ;
45+
46+ case AccountSettingsSelectors . getExternalIdentities :
47+ return ( ) => null ;
48+
49+ case AccountSettingsSelectors . getRegions :
50+ return ( ) => null ;
51+
52+ case AccountSettingsSelectors . getUserInstitutions :
53+ return ( ) => null ;
54+
55+ default :
56+ return ( ) => [ ] ;
57+ }
58+ } ) ;
2059 await TestBed . configureTestingModule ( {
21- imports : [ AccountSettingsComponent , MockComponent ( SubHeaderComponent ) ] ,
22- providers : [ provideNoopAnimations ( ) , provideHttpClient ( ) , provideHttpClientTesting ( ) , provideStore ( [ UserState ] ) ] ,
60+ imports : [
61+ AccountSettingsComponent ,
62+ ...MockComponents (
63+ SubHeaderComponent ,
64+ ConnectedEmailsComponent ,
65+ DefaultStorageLocationComponent ,
66+ ConnectedIdentitiesComponent ,
67+ ShareIndexingComponent ,
68+ ChangePasswordComponent ,
69+ TwoFactorAuthComponent ,
70+ DeactivateAccountComponent ,
71+ AffiliatedInstitutionsComponent
72+ ) ,
73+ MockPipe ( TranslatePipe ) ,
74+ ] ,
75+ providers : [
76+ MockCustomConfirmationServiceProvider ,
77+ TranslateServiceMock ,
78+ MockProvider ( ToastService ) ,
79+ provideNoopAnimations ( ) ,
80+ provideHttpClient ( ) ,
81+ provideHttpClientTesting ( ) ,
82+ MockProvider ( Store , store ) ,
83+ ] ,
2384 } ) . compileComponents ( ) ;
2485
2586 fixture = TestBed . createComponent ( AccountSettingsComponent ) ;
@@ -30,4 +91,14 @@ describe('AccountSettingsComponent', () => {
3091 it ( 'should create' , ( ) => {
3192 expect ( component ) . toBeTruthy ( ) ;
3293 } ) ;
94+
95+ it ( 'should not dispatch actions when currentUser is null' , ( ) => {
96+ store . selectSignal . mockImplementation ( ( selector ) =>
97+ selector === UserSelectors . getCurrentUser ? ( ) => null : ( ) => [ ]
98+ ) ;
99+
100+ store . dispatch . mockClear ( ) ;
101+
102+ expect ( store . dispatch ) . not . toHaveBeenCalled ( ) ;
103+ } ) ;
33104} ) ;
0 commit comments