1
- import { provideStore } from '@ngxs/store' ;
1
+ import { Store } from '@ngxs/store' ;
2
2
3
- import { MockComponent } from 'ng-mocks' ;
3
+ import { TranslatePipe } from '@ngx-translate/core' ;
4
+ import { MockComponents , MockPipe , MockProvider } from 'ng-mocks' ;
4
5
5
6
import { provideHttpClient } from '@angular/common/http' ;
6
7
import { provideHttpClientTesting } from '@angular/common/http/testing' ;
7
8
import { ComponentFixture , TestBed } from '@angular/core/testing' ;
8
9
import { provideNoopAnimations } from '@angular/platform-browser/animations' ;
9
10
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' ;
11
23
import { SubHeaderComponent } from '@osf/shared/components' ;
24
+ import { MOCK_STORE , MOCK_USER , MockCustomConfirmationServiceProvider , TranslateServiceMock } from '@shared/mocks' ;
25
+ import { ToastService } from '@shared/services' ;
12
26
13
27
import { AccountSettingsComponent } from './account-settings.component' ;
14
28
15
29
describe ( 'AccountSettingsComponent' , ( ) => {
16
30
let component : AccountSettingsComponent ;
17
31
let fixture : ComponentFixture < AccountSettingsComponent > ;
32
+ const store = MOCK_STORE ;
18
33
19
34
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
+ } ) ;
20
59
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
+ ] ,
23
84
} ) . compileComponents ( ) ;
24
85
25
86
fixture = TestBed . createComponent ( AccountSettingsComponent ) ;
@@ -30,4 +91,14 @@ describe('AccountSettingsComponent', () => {
30
91
it ( 'should create' , ( ) => {
31
92
expect ( component ) . toBeTruthy ( ) ;
32
93
} ) ;
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
+ } ) ;
33
104
} ) ;
0 commit comments