1- import { Store } from '@ngxs/store ' ;
1+ import { TranslatePipe } from '@ngx-translate/core ' ;
22
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' ;
76
87import { of } from 'rxjs' ;
98
10- import { signal } from '@angular/core' ;
119import { 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' ;
1413
1514import { TokenModel } from '../../models' ;
16- import { DeleteToken } from '../../store' ;
1715
1816import { TokensListComponent } from './tokens-list.component' ;
1917
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+
2041describe ( 'TokensListComponent' , ( ) => {
2142 let component : TokensListComponent ;
2243 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- ] ;
4244
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+ } ;
4848
49- confirmationService = {
50- confirm : jest . fn ( ) ,
51- } ;
49+ const mockToastService = {
50+ showSuccess : jest . fn ( ) ,
51+ } ;
5252
53+ beforeEach ( async ( ) => {
5354 await TestBed . configureTestingModule ( {
54- imports : [ TokensListComponent , MockPipe ( TranslatePipe ) ] ,
55+ imports : [ TokensListComponent , TranslatePipe , Button , Card , Skeleton , RouterLink ] ,
5556 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 } ,
6859 ] ,
6960 } ) . compileComponents ( ) ;
7061
@@ -73,53 +64,33 @@ describe('TokensListComponent', () => {
7364 fixture . detectChanges ( ) ;
7465 } ) ;
7566
76- it ( 'should create' , ( ) => {
67+ it ( 'should create the component ' , ( ) => {
7768 expect ( component ) . toBeTruthy ( ) ;
7869 } ) ;
7970
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 ( ) ;
8373 } ) ;
8474
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 ;
8977
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 ( ) ;
10980 } ) ;
110- component . deleteToken ( token ) ;
111- expect ( store . dispatch ) . toHaveBeenCalledWith ( new DeleteToken ( token . id ) ) ;
112- } ) ;
11381
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- } ) ;
12282 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' ) ;
12495 } ) ;
12596} ) ;
0 commit comments