1
1
import { async , ComponentFixture , TestBed , inject , tick , fakeAsync } from '@angular/core/testing' ;
2
2
import { MatPaginatorModule } from './index' ;
3
- import { MatPaginator , PageEvent } from './paginator' ;
3
+ import { MatPaginator } from './paginator' ;
4
4
import { Component , ViewChild } from '@angular/core' ;
5
5
import { MatPaginatorIntl } from './paginator-intl' ;
6
6
import { NoopAnimationsModule } from '@angular/platform-browser/animations' ;
@@ -111,7 +111,10 @@ describe('MatPaginator', () => {
111
111
dispatchMouseEvent ( getNextButton ( fixture ) , 'click' ) ;
112
112
113
113
expect ( paginator . pageIndex ) . toBe ( 1 ) ;
114
- expect ( component . latestPageEvent ? component . latestPageEvent . pageIndex : null ) . toBe ( 1 ) ;
114
+ expect ( component . pageEvent ) . toHaveBeenCalledWith ( jasmine . objectContaining ( {
115
+ previousPageIndex : 0 ,
116
+ pageIndex : 1
117
+ } ) ) ;
115
118
} ) ;
116
119
117
120
it ( 'should be able to go to the previous page' , ( ) => {
@@ -122,7 +125,10 @@ describe('MatPaginator', () => {
122
125
dispatchMouseEvent ( getPreviousButton ( fixture ) , 'click' ) ;
123
126
124
127
expect ( paginator . pageIndex ) . toBe ( 0 ) ;
125
- expect ( component . latestPageEvent ? component . latestPageEvent . pageIndex : null ) . toBe ( 0 ) ;
128
+ expect ( component . pageEvent ) . toHaveBeenCalledWith ( jasmine . objectContaining ( {
129
+ previousPageIndex : 1 ,
130
+ pageIndex : 0
131
+ } ) ) ;
126
132
} ) ;
127
133
} ) ;
128
134
@@ -169,7 +175,10 @@ describe('MatPaginator', () => {
169
175
dispatchMouseEvent ( getLastButton ( fixture ) , 'click' ) ;
170
176
171
177
expect ( paginator . pageIndex ) . toBe ( 9 ) ;
172
- expect ( component . latestPageEvent ? component . latestPageEvent . pageIndex : null ) . toBe ( 9 ) ;
178
+ expect ( component . pageEvent ) . toHaveBeenCalledWith ( jasmine . objectContaining ( {
179
+ previousPageIndex : 0 ,
180
+ pageIndex : 9
181
+ } ) ) ;
173
182
} ) ;
174
183
175
184
it ( 'should be able to go to the first page via the first page button' , ( ) => {
@@ -180,7 +189,10 @@ describe('MatPaginator', () => {
180
189
dispatchMouseEvent ( getFirstButton ( fixture ) , 'click' ) ;
181
190
182
191
expect ( paginator . pageIndex ) . toBe ( 0 ) ;
183
- expect ( component . latestPageEvent ? component . latestPageEvent . pageIndex : null ) . toBe ( 0 ) ;
192
+ expect ( component . pageEvent ) . toHaveBeenCalledWith ( jasmine . objectContaining ( {
193
+ previousPageIndex : 3 ,
194
+ pageIndex : 0
195
+ } ) ) ;
184
196
} ) ;
185
197
186
198
it ( 'should disable navigating to the next page if at last page' , ( ) => {
@@ -189,21 +201,21 @@ describe('MatPaginator', () => {
189
201
expect ( paginator . pageIndex ) . toBe ( 9 ) ;
190
202
expect ( paginator . hasNextPage ( ) ) . toBe ( false ) ;
191
203
192
- component . latestPageEvent = null ;
204
+ component . pageEvent . calls . reset ( ) ;
193
205
dispatchMouseEvent ( getNextButton ( fixture ) , 'click' ) ;
194
206
195
- expect ( component . latestPageEvent ) . toBe ( null ) ;
207
+ expect ( component . pageEvent ) . not . toHaveBeenCalled ( ) ;
196
208
expect ( paginator . pageIndex ) . toBe ( 9 ) ;
197
209
} ) ;
198
210
199
211
it ( 'should disable navigating to the previous page if at first page' , ( ) => {
200
212
expect ( paginator . pageIndex ) . toBe ( 0 ) ;
201
213
expect ( paginator . hasPreviousPage ( ) ) . toBe ( false ) ;
202
214
203
- component . latestPageEvent = null ;
215
+ component . pageEvent . calls . reset ( ) ;
204
216
dispatchMouseEvent ( getPreviousButton ( fixture ) , 'click' ) ;
205
217
206
- expect ( component . latestPageEvent ) . toBe ( null ) ;
218
+ expect ( component . pageEvent ) . not . toHaveBeenCalled ( ) ;
207
219
expect ( paginator . pageIndex ) . toBe ( 0 ) ;
208
220
} ) ;
209
221
@@ -270,35 +282,37 @@ describe('MatPaginator', () => {
270
282
fixture . detectChanges ( ) ;
271
283
272
284
// The first item of the page should be item with index 40
273
- let firstPageItemIndex : number | null = paginator . pageIndex * paginator . pageSize ;
274
- expect ( firstPageItemIndex ) . toBe ( 40 ) ;
285
+ expect ( paginator . pageIndex * paginator . pageSize ) . toBe ( 40 ) ;
275
286
276
287
// The first item on the page is now 25. Change the page size to 25 so that we should now be
277
288
// on the second page where the top item is index 25.
289
+ component . pageEvent . calls . reset ( ) ;
278
290
paginator . _changePageSize ( 25 ) ;
279
- let paginationEvent = component . latestPageEvent ;
280
- firstPageItemIndex = paginationEvent ?
281
- paginationEvent . pageIndex * paginationEvent . pageSize : null ;
282
- expect ( firstPageItemIndex ) . toBe ( 25 ) ;
283
- expect ( paginationEvent ? paginationEvent . pageIndex : null ) . toBe ( 1 ) ;
291
+
292
+ expect ( component . pageEvent ) . toHaveBeenCalledWith ( jasmine . objectContaining ( {
293
+ pageIndex : 1 ,
294
+ pageSize : 25
295
+ } ) ) ;
284
296
285
297
// The first item on the page is still 25. Change the page size to 8 so that we should now be
286
298
// on the fourth page where the top item is index 24.
299
+ component . pageEvent . calls . reset ( ) ;
287
300
paginator . _changePageSize ( 8 ) ;
288
- paginationEvent = component . latestPageEvent ;
289
- firstPageItemIndex = paginationEvent ?
290
- paginationEvent . pageIndex * paginationEvent . pageSize : null ;
291
- expect ( firstPageItemIndex ) . toBe ( 24 ) ;
292
- expect ( paginationEvent ? paginationEvent . pageIndex : null ) . toBe ( 3 ) ;
301
+
302
+ expect ( component . pageEvent ) . toHaveBeenCalledWith ( jasmine . objectContaining ( {
303
+ pageIndex : 3 ,
304
+ pageSize : 8
305
+ } ) ) ;
293
306
294
307
// The first item on the page is 24. Change the page size to 16 so that we should now be
295
308
// on the first page where the top item is index 0.
309
+ component . pageEvent . calls . reset ( ) ;
296
310
paginator . _changePageSize ( 25 ) ;
297
- paginationEvent = component . latestPageEvent ;
298
- firstPageItemIndex = paginationEvent ?
299
- paginationEvent . pageIndex * paginationEvent . pageSize : null ;
300
- expect ( firstPageItemIndex ) . toBe ( 0 ) ;
301
- expect ( paginationEvent ? paginationEvent . pageIndex : null ) . toBe ( 0 ) ;
311
+
312
+ expect ( component . pageEvent ) . toHaveBeenCalledWith ( jasmine . objectContaining ( {
313
+ pageIndex : 0 ,
314
+ pageSize : 25
315
+ } ) ) ;
302
316
} ) ;
303
317
304
318
it ( 'should show a select only if there are multiple options' , ( ) => {
@@ -363,7 +377,7 @@ function getLastButton(fixture: ComponentFixture<any>) {
363
377
[hidePageSize]="hidePageSize"
364
378
[showFirstLastButtons]="showFirstLastButtons"
365
379
[length]="length"
366
- (page)="latestPageEvent = $event">
380
+ (page)="pageEvent( $event) ">
367
381
</mat-paginator>
368
382
` ,
369
383
} )
@@ -374,8 +388,7 @@ class MatPaginatorApp {
374
388
hidePageSize = false ;
375
389
showFirstLastButtons = false ;
376
390
length = 100 ;
377
-
378
- latestPageEvent : PageEvent | null ;
391
+ pageEvent = jasmine . createSpy ( 'page event' ) ;
379
392
380
393
@ViewChild ( MatPaginator ) paginator : MatPaginator ;
381
394
0 commit comments