1- import { NgxsModule , Store } from '@ngxs/store' ;
1+ import { provideStore , Store } from '@ngxs/store' ;
22
33import { ComponentFixture , TestBed } from '@angular/core/testing' ;
44import { ActivatedRoute } from '@angular/router' ;
@@ -14,36 +14,84 @@ describe('RegistrationRecentActivityComponent', () => {
1414
1515 beforeEach ( async ( ) => {
1616 await TestBed . configureTestingModule ( {
17- imports : [ NgxsModule . forRoot ( [ ActivityLogsState ] ) , RegistrationRecentActivityComponent ] ,
18- providers : [ { provide : ActivatedRoute , useValue : { snapshot : { params : { id : 'reg123' } } , parent : null } } ] ,
17+ imports : [ RegistrationRecentActivityComponent ] ,
18+ providers : [
19+ provideStore ( [ ActivityLogsState ] ) ,
20+ { provide : ActivatedRoute , useValue : { snapshot : { params : { id : 'reg123' } } , parent : null } } ,
21+ ] ,
1922 } ) . compileComponents ( ) ;
2023
2124 store = TestBed . inject ( Store ) ;
22- spyOn ( store , 'dispatch' ) . and . callThrough ( ) ;
25+ jest . spyOn ( store , 'dispatch' ) ;
2326
2427 fixture = TestBed . createComponent ( RegistrationRecentActivityComponent ) ;
2528 fixture . detectChanges ( ) ;
2629 } ) ;
2730
28- it ( 'creates and dispatches initial registration logs fetch' , ( ) => {
29- expect ( fixture . componentInstance ) . toBeTruthy ( ) ;
30- expect ( store . dispatch ) . toHaveBeenCalledWith ( jasmine . any ( GetRegistrationActivityLogs ) ) ;
31- const action = ( store . dispatch as jasmine . Spy ) . calls . mostRecent ( ) . args [ 0 ] as GetRegistrationActivityLogs ;
31+ it ( 'dispatches initial registration logs fetch' , ( ) => {
32+ const dispatchSpy = store . dispatch as jest . Mock ;
33+ expect ( dispatchSpy ) . toHaveBeenCalledWith ( expect . any ( GetRegistrationActivityLogs ) ) ;
34+ const action = dispatchSpy . mock . calls . at ( - 1 ) ?. [ 0 ] as GetRegistrationActivityLogs ;
3235 expect ( action . registrationId ) . toBe ( 'reg123' ) ;
33- expect ( action . page ) . toBe ( '1' ) ;
36+ expect ( action . page ) . toBe ( 1 ) ;
37+ } ) ;
38+
39+ it ( 'renders empty state when no logs and not loading' , ( ) => {
40+ store . reset ( {
41+ activityLogs : {
42+ activityLogs : { data : [ ] , isLoading : false , error : null , totalCount : 0 } ,
43+ } ,
44+ } as any ) ;
45+ fixture . detectChanges ( ) ;
46+
47+ const empty = fixture . nativeElement . querySelector ( '[data-test="recent-activity-empty"]' ) ;
48+ expect ( empty ) . toBeTruthy ( ) ;
49+ } ) ;
50+
51+ it ( 'renders item & paginator when logs exist and totalCount > pageSize' , ( ) => {
52+ store . reset ( {
53+ activityLogs : {
54+ activityLogs : {
55+ data : [
56+ {
57+ id : 'log1' ,
58+ date : '2024-01-01T00:00:00Z' ,
59+ formattedActivity : '<b>formatted</b>' ,
60+ } ,
61+ ] ,
62+ isLoading : false ,
63+ error : null ,
64+ totalCount : 25 ,
65+ } ,
66+ } ,
67+ } as any ) ;
68+ fixture . detectChanges ( ) ;
69+
70+ const item = fixture . nativeElement . querySelector ( '[data-test="recent-activity-item"]' ) ;
71+ const content = fixture . nativeElement . querySelector ( '[data-test="recent-activity-item-content"]' ) ;
72+ const paginator = fixture . nativeElement . querySelector ( '[data-test="recent-activity-paginator"]' ) ;
73+
74+ expect ( item ) . toBeTruthy ( ) ;
75+ expect ( content ?. innerHTML ) . toContain ( 'formatted' ) ;
76+ expect ( paginator ) . toBeTruthy ( ) ;
3477 } ) ;
3578
3679 it ( 'dispatches on page change' , ( ) => {
37- ( store . dispatch as jasmine . Spy ) . calls . reset ( ) ;
80+ const dispatchSpy = store . dispatch as jest . Mock ;
81+ dispatchSpy . mockClear ( ) ;
82+
3883 fixture . componentInstance . onPageChange ( { page : 2 } as any ) ;
39- expect ( store . dispatch ) . toHaveBeenCalledWith ( jasmine . any ( GetRegistrationActivityLogs ) ) ;
40- const action = ( store . dispatch as jasmine . Spy ) . calls . mostRecent ( ) . args [ 0 ] as GetRegistrationActivityLogs ;
41- expect ( action . page ) . toBe ( '3' ) ;
84+ expect ( dispatchSpy ) . toHaveBeenCalledWith ( expect . any ( GetRegistrationActivityLogs ) ) ;
85+
86+ const action = dispatchSpy . mock . calls . at ( - 1 ) ?. [ 0 ] as GetRegistrationActivityLogs ;
87+ expect ( action . page ) . toBe ( 3 ) ;
4288 } ) ;
4389
4490 it ( 'clears store on destroy' , ( ) => {
45- ( store . dispatch as jasmine . Spy ) . calls . reset ( ) ;
91+ const dispatchSpy = store . dispatch as jest . Mock ;
92+ dispatchSpy . mockClear ( ) ;
93+
4694 fixture . destroy ( ) ;
47- expect ( store . dispatch ) . toHaveBeenCalledWith ( jasmine . any ( ClearActivityLogsStore ) ) ;
95+ expect ( dispatchSpy ) . toHaveBeenCalledWith ( expect . any ( ClearActivityLogsStore ) ) ;
4896 } ) ;
4997} ) ;
0 commit comments