1
- import { Store } from '@ngxs/store ' ;
1
+ import { TranslatePipe } from '@ngx-translate/core ' ;
2
2
3
- import { TranslatePipe , TranslateService } from '@ngx-translate/core' ;
4
- import { MockPipe , MockProvider } from 'ng-mocks' ;
5
-
6
- import { Confirmation , ConfirmationService } from 'primeng/api' ;
3
+ import { Button } from 'primeng/button' ;
4
+ import { Card } from 'primeng/card' ;
5
+ import { Skeleton } from 'primeng/skeleton' ;
7
6
8
7
import { of } from 'rxjs' ;
9
8
10
- import { signal } from '@angular/core' ;
11
9
import { ComponentFixture , TestBed } from '@angular/core/testing' ;
12
- import { By } from '@angular/platform-browser' ;
13
- import { ActivatedRoute } from '@angular/router' ;
10
+ import { RouterLink } from '@angular/router' ;
11
+
12
+ import { CustomConfirmationService , ToastService } from '@osf/shared/services' ;
14
13
15
14
import { TokenModel } from '../../models' ;
16
- import { DeleteToken } from '../../store' ;
17
15
18
16
import { TokensListComponent } from './tokens-list.component' ;
19
17
18
+ jest . mock ( '@core/store/user' , ( ) => ( { } ) ) ;
19
+ jest . mock ( '@osf/shared/stores/collections' , ( ) => ( { } ) ) ;
20
+ jest . mock ( '@osf/shared/stores/addons' , ( ) => ( { } ) ) ;
21
+ jest . mock ( '@osf/features/settings/tokens/store' , ( ) => ( { } ) ) ;
22
+
23
+ const mockGetTokens = jest . fn ( ) ;
24
+ const mockDeleteToken = jest . fn ( ( ) => of ( void 0 ) ) ;
25
+
26
+ jest . mock ( '@ngxs/store' , ( ) => {
27
+ return {
28
+ createDispatchMap : jest . fn ( ( ) => ( {
29
+ getTokens : mockGetTokens ,
30
+ deleteToken : mockDeleteToken ,
31
+ } ) ) ,
32
+ select : ( selectorFn : any ) => {
33
+ const name = selectorFn ?. name ;
34
+ if ( name === 'isTokensLoading' ) return of ( false ) ;
35
+ if ( name === 'getTokens' ) return of ( [ ] ) ;
36
+ return of ( undefined ) ;
37
+ } ,
38
+ } ;
39
+ } ) ;
40
+
20
41
describe ( 'TokensListComponent' , ( ) => {
21
42
let component : TokensListComponent ;
22
43
let fixture : ComponentFixture < TokensListComponent > ;
23
- let store : Partial < Store > ;
24
- let confirmationService : Partial < ConfirmationService > ;
25
-
26
- const mockTokens : TokenModel [ ] = [
27
- {
28
- id : '1' ,
29
- name : 'Test Token 1' ,
30
- tokenId : 'token1' ,
31
- scopes : [ 'read' , 'write' ] ,
32
- ownerId : 'user1' ,
33
- } ,
34
- {
35
- id : '2' ,
36
- name : 'Test Token 2' ,
37
- tokenId : 'token2' ,
38
- scopes : [ 'read' ] ,
39
- ownerId : 'user1' ,
40
- } ,
41
- ] ;
42
44
43
- beforeEach ( async ( ) => {
44
- store = {
45
- dispatch : jest . fn ( ) . mockReturnValue ( of ( undefined ) ) ,
46
- selectSignal : jest . fn ( ) . mockReturnValue ( signal ( mockTokens ) ) ,
47
- } ;
45
+ const mockConfirmationService = {
46
+ confirmDelete : jest . fn ( ) ,
47
+ } ;
48
48
49
- confirmationService = {
50
- confirm : jest . fn ( ) ,
51
- } ;
49
+ const mockToastService = {
50
+ showSuccess : jest . fn ( ) ,
51
+ } ;
52
52
53
+ beforeEach ( async ( ) => {
53
54
await TestBed . configureTestingModule ( {
54
- imports : [ TokensListComponent , MockPipe ( TranslatePipe ) ] ,
55
+ imports : [ TokensListComponent , TranslatePipe , Button , Card , Skeleton , RouterLink ] ,
55
56
providers : [
56
- MockProvider ( TranslateService ) ,
57
- MockProvider ( Store , store ) ,
58
- MockProvider ( ConfirmationService , confirmationService ) ,
59
- {
60
- provide : ActivatedRoute ,
61
- useValue : {
62
- snapshot : {
63
- params : { } ,
64
- queryParams : { } ,
65
- } ,
66
- } ,
67
- } ,
57
+ { provide : CustomConfirmationService , useValue : mockConfirmationService } ,
58
+ { provide : ToastService , useValue : mockToastService } ,
68
59
] ,
69
60
} ) . compileComponents ( ) ;
70
61
@@ -73,53 +64,33 @@ describe('TokensListComponent', () => {
73
64
fixture . detectChanges ( ) ;
74
65
} ) ;
75
66
76
- it ( 'should create' , ( ) => {
67
+ it ( 'should create the component ' , ( ) => {
77
68
expect ( component ) . toBeTruthy ( ) ;
78
69
} ) ;
79
70
80
- it ( 'should not load tokens on init if they already exist' , ( ) => {
81
- component . ngOnInit ( ) ;
82
- expect ( store . dispatch ) . not . toHaveBeenCalled ( ) ;
71
+ it ( 'should dispatch getTokens on init' , ( ) => {
72
+ expect ( mockGetTokens ) . toHaveBeenCalled ( ) ;
83
73
} ) ;
84
74
85
- it ( 'should display tokens in the list' , ( ) => {
86
- const tokenElements = fixture . debugElement . queryAll ( By . css ( 'p-card' ) ) ;
87
- expect ( tokenElements . length ) . toBe ( mockTokens . length ) ;
88
- } ) ;
75
+ it ( 'should call confirmDelete and deleteToken, then showSuccess' , ( ) => {
76
+ const token : TokenModel = { id : 'abc123' , name : 'My Token' } as TokenModel ;
89
77
90
- it ( 'should show token names in the list' , ( ) => {
91
- const tokenNames = fixture . debugElement . queryAll ( By . css ( 'h2' ) ) ;
92
- expect ( tokenNames [ 0 ] . nativeElement . textContent ) . toBe ( mockTokens [ 0 ] . name ) ;
93
- expect ( tokenNames [ 1 ] . nativeElement . textContent ) . toBe ( mockTokens [ 1 ] . name ) ;
94
- } ) ;
95
-
96
- it ( 'should show confirmation dialog when deleting token' , ( ) => {
97
- const token = mockTokens [ 0 ] ;
98
- component . deleteToken ( token ) ;
99
- expect ( confirmationService . confirm ) . toHaveBeenCalled ( ) ;
100
- } ) ;
101
-
102
- it ( 'should dispatch delete action when confirmation is accepted' , ( ) => {
103
- const token = mockTokens [ 0 ] ;
104
- ( confirmationService . confirm as jest . Mock ) . mockImplementation ( ( config : Confirmation ) => {
105
- if ( config . accept ) {
106
- config . accept ( ) ;
107
- }
108
- return confirmationService ;
78
+ mockConfirmationService . confirmDelete . mockImplementation ( ( config : any ) => {
79
+ config . onConfirm ( ) ;
109
80
} ) ;
110
- component . deleteToken ( token ) ;
111
- expect ( store . dispatch ) . toHaveBeenCalledWith ( new DeleteToken ( token . id ) ) ;
112
- } ) ;
113
81
114
- it ( 'should not dispatch delete action when confirmation is rejected' , ( ) => {
115
- const token = mockTokens [ 0 ] ;
116
- ( confirmationService . confirm as jest . Mock ) . mockImplementation ( ( config : Confirmation ) => {
117
- if ( config . reject ) {
118
- config . reject ( ) ;
119
- }
120
- return confirmationService ;
121
- } ) ;
122
82
component . deleteToken ( token ) ;
123
- expect ( store . dispatch ) . not . toHaveBeenCalledWith ( new DeleteToken ( token . id ) ) ;
83
+
84
+ expect ( mockConfirmationService . confirmDelete ) . toHaveBeenCalledWith (
85
+ expect . objectContaining ( {
86
+ headerKey : 'settings.tokens.confirmation.delete.title' ,
87
+ messageKey : 'settings.tokens.confirmation.delete.message' ,
88
+ headerParams : { name : token . name } ,
89
+ onConfirm : expect . any ( Function ) ,
90
+ } )
91
+ ) ;
92
+
93
+ expect ( mockDeleteToken ) . toHaveBeenCalledWith ( token . id ) ;
94
+ expect ( mockToastService . showSuccess ) . toHaveBeenCalledWith ( 'settings.tokens.toastMessage.successDelete' ) ;
124
95
} ) ;
125
96
} ) ;
0 commit comments