1
- import { provideStore } from '@ngxs/store ' ;
1
+ import { MockProvider } from 'ng-mocks ' ;
2
2
3
- import { TranslatePipe } from '@ngx-translate/core' ;
4
- import { MockComponents , MockPipe } from 'ng-mocks' ;
3
+ import { PaginatorState } from 'primeng/paginator' ;
5
4
6
- import { of } from 'rxjs' ;
7
-
8
- import { provideHttpClient } from '@angular/common/http' ;
9
- import { provideHttpClientTesting } from '@angular/common/http/testing' ;
10
5
import { ComponentFixture , TestBed } from '@angular/core/testing' ;
11
- import { ActivatedRoute } from '@angular/router' ;
6
+ import { ActivatedRoute , Router } from '@angular/router' ;
12
7
13
- import {
14
- CustomPaginatorComponent ,
15
- LoadingSpinnerComponent ,
16
- SearchInputComponent ,
17
- SubHeaderComponent ,
18
- } from '@shared/components' ;
19
- import { InstitutionsState } from '@shared/stores/institutions' ;
8
+ import { InstitutionsSelectors } from '@osf/shared/stores/institutions' ;
9
+ import { MOCK_INSTITUTION } from '@shared/mocks/institution.mock' ;
20
10
21
11
import { InstitutionsListComponent } from './institutions-list.component' ;
22
12
23
- describe . skip ( 'InstitutionsListComponent' , ( ) => {
13
+ import { OSFTestingModule } from '@testing/osf.testing.module' ;
14
+ import { ActivatedRouteMockBuilder } from '@testing/providers/route-provider.mock' ;
15
+ import { RouterMockBuilder } from '@testing/providers/router-provider.mock' ;
16
+ import { provideMockStore } from '@testing/providers/store-provider.mock' ;
17
+
18
+ describe ( 'InstitutionsListComponent' , ( ) => {
24
19
let component : InstitutionsListComponent ;
25
20
let fixture : ComponentFixture < InstitutionsListComponent > ;
21
+ let routerMock : ReturnType < RouterMockBuilder [ 'build' ] > ;
22
+ let activatedRouteMock : ReturnType < ActivatedRouteMockBuilder [ 'build' ] > ;
23
+
24
+ const mockInstitutions = [ MOCK_INSTITUTION ] ;
25
+ const mockTotalCount = 2 ;
26
26
27
27
beforeEach ( async ( ) => {
28
+ routerMock = RouterMockBuilder . create ( ) . build ( ) ;
29
+ activatedRouteMock = ActivatedRouteMockBuilder . create ( )
30
+ . withQueryParams ( { page : '1' , size : '10' , search : '' } )
31
+ . build ( ) ;
32
+
28
33
await TestBed . configureTestingModule ( {
29
- imports : [
30
- InstitutionsListComponent ,
31
- ...MockComponents ( SubHeaderComponent , SearchInputComponent , CustomPaginatorComponent , LoadingSpinnerComponent ) ,
32
- MockPipe ( TranslatePipe ) ,
33
- ] ,
34
+ imports : [ InstitutionsListComponent , OSFTestingModule ] ,
34
35
providers : [
35
- {
36
- provide : ActivatedRoute ,
37
- useValue : {
38
- snapshot : { paramMap : { get : ( ) => '1' } } ,
39
- queryParams : of ( { } ) ,
40
- } ,
41
- } ,
42
- provideStore ( [ InstitutionsState ] ) ,
43
- provideHttpClient ( ) ,
44
- provideHttpClientTesting ( ) ,
36
+ provideMockStore ( {
37
+ signals : [
38
+ { selector : InstitutionsSelectors . getInstitutions , value : mockInstitutions } ,
39
+ { selector : InstitutionsSelectors . getInstitutionsTotalCount , value : mockTotalCount } ,
40
+ { selector : InstitutionsSelectors . isInstitutionsLoading , value : false } ,
41
+ ] ,
42
+ } ) ,
43
+ MockProvider ( Router , routerMock ) ,
44
+ MockProvider ( ActivatedRoute , activatedRouteMock ) ,
45
45
] ,
46
46
} ) . compileComponents ( ) ;
47
47
@@ -53,4 +53,70 @@ describe.skip('InstitutionsListComponent', () => {
53
53
it ( 'should create' , ( ) => {
54
54
expect ( component ) . toBeTruthy ( ) ;
55
55
} ) ;
56
+
57
+ it ( 'should update currentPage, first, and call updateQueryParams when page is provided' , ( ) => {
58
+ const paginatorEvent : PaginatorState = {
59
+ page : 1 ,
60
+ first : 20 ,
61
+ rows : 10 ,
62
+ pageCount : 5 ,
63
+ } ;
64
+
65
+ component . onPageChange ( paginatorEvent ) ;
66
+
67
+ expect ( component . currentPage ( ) ) . toBe ( 2 ) ;
68
+ expect ( component . first ( ) ) . toBe ( 20 ) ;
69
+ expect ( routerMock . navigate ) . toHaveBeenCalledWith ( [ ] , {
70
+ relativeTo : expect . any ( Object ) ,
71
+ queryParams : {
72
+ page : '2' ,
73
+ size : '10' ,
74
+ } ,
75
+ queryParamsHandling : 'merge' ,
76
+ } ) ;
77
+ } ) ;
78
+
79
+ it ( 'should set currentPage to 1 when page is not provided' , ( ) => {
80
+ const paginatorEvent : PaginatorState = {
81
+ page : undefined ,
82
+ first : 0 ,
83
+ rows : 20 ,
84
+ pageCount : 3 ,
85
+ } ;
86
+
87
+ component . onPageChange ( paginatorEvent ) ;
88
+
89
+ expect ( component . currentPage ( ) ) . toBe ( 1 ) ;
90
+ expect ( component . first ( ) ) . toBe ( 0 ) ;
91
+ expect ( routerMock . navigate ) . toHaveBeenCalledWith ( [ ] , {
92
+ relativeTo : expect . any ( Object ) ,
93
+ queryParams : {
94
+ page : '1' ,
95
+ size : '20' ,
96
+ } ,
97
+ queryParamsHandling : 'merge' ,
98
+ } ) ;
99
+ } ) ;
100
+
101
+ it ( 'should handle first being undefined' , ( ) => {
102
+ const paginatorEvent : PaginatorState = {
103
+ page : 2 ,
104
+ first : undefined ,
105
+ rows : 15 ,
106
+ pageCount : 4 ,
107
+ } ;
108
+
109
+ component . onPageChange ( paginatorEvent ) ;
110
+
111
+ expect ( component . currentPage ( ) ) . toBe ( 2 ) ;
112
+ expect ( component . first ( ) ) . toBe ( 0 ) ;
113
+ expect ( routerMock . navigate ) . toHaveBeenCalledWith ( [ ] , {
114
+ relativeTo : expect . any ( Object ) ,
115
+ queryParams : {
116
+ page : '2' ,
117
+ size : '15' ,
118
+ } ,
119
+ queryParamsHandling : 'merge' ,
120
+ } ) ;
121
+ } ) ;
56
122
} ) ;
0 commit comments