11import { async , ComponentFixture , TestBed , inject , tick , fakeAsync } from '@angular/core/testing' ;
22import { MatPaginatorModule } from './index' ;
3- import { MatPaginator , PageEvent } from './paginator' ;
3+ import { MatPaginator } from './paginator' ;
44import { Component , ViewChild } from '@angular/core' ;
55import { MatPaginatorIntl } from './paginator-intl' ;
66import { NoopAnimationsModule } from '@angular/platform-browser/animations' ;
@@ -111,7 +111,10 @@ describe('MatPaginator', () => {
111111 dispatchMouseEvent ( getNextButton ( fixture ) , 'click' ) ;
112112
113113 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+ } ) ) ;
115118 } ) ;
116119
117120 it ( 'should be able to go to the previous page' , ( ) => {
@@ -122,7 +125,10 @@ describe('MatPaginator', () => {
122125 dispatchMouseEvent ( getPreviousButton ( fixture ) , 'click' ) ;
123126
124127 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+ } ) ) ;
126132 } ) ;
127133 } ) ;
128134
@@ -169,7 +175,10 @@ describe('MatPaginator', () => {
169175 dispatchMouseEvent ( getLastButton ( fixture ) , 'click' ) ;
170176
171177 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+ } ) ) ;
173182 } ) ;
174183
175184 it ( 'should be able to go to the first page via the first page button' , ( ) => {
@@ -180,7 +189,10 @@ describe('MatPaginator', () => {
180189 dispatchMouseEvent ( getFirstButton ( fixture ) , 'click' ) ;
181190
182191 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+ } ) ) ;
184196 } ) ;
185197
186198 it ( 'should disable navigating to the next page if at last page' , ( ) => {
@@ -189,21 +201,21 @@ describe('MatPaginator', () => {
189201 expect ( paginator . pageIndex ) . toBe ( 9 ) ;
190202 expect ( paginator . hasNextPage ( ) ) . toBe ( false ) ;
191203
192- component . latestPageEvent = null ;
204+ component . pageEvent . calls . reset ( ) ;
193205 dispatchMouseEvent ( getNextButton ( fixture ) , 'click' ) ;
194206
195- expect ( component . latestPageEvent ) . toBe ( null ) ;
207+ expect ( component . pageEvent ) . not . toHaveBeenCalled ( ) ;
196208 expect ( paginator . pageIndex ) . toBe ( 9 ) ;
197209 } ) ;
198210
199211 it ( 'should disable navigating to the previous page if at first page' , ( ) => {
200212 expect ( paginator . pageIndex ) . toBe ( 0 ) ;
201213 expect ( paginator . hasPreviousPage ( ) ) . toBe ( false ) ;
202214
203- component . latestPageEvent = null ;
215+ component . pageEvent . calls . reset ( ) ;
204216 dispatchMouseEvent ( getPreviousButton ( fixture ) , 'click' ) ;
205217
206- expect ( component . latestPageEvent ) . toBe ( null ) ;
218+ expect ( component . pageEvent ) . not . toHaveBeenCalled ( ) ;
207219 expect ( paginator . pageIndex ) . toBe ( 0 ) ;
208220 } ) ;
209221
@@ -270,35 +282,37 @@ describe('MatPaginator', () => {
270282 fixture . detectChanges ( ) ;
271283
272284 // 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 ) ;
275286
276287 // The first item on the page is now 25. Change the page size to 25 so that we should now be
277288 // on the second page where the top item is index 25.
289+ component . pageEvent . calls . reset ( ) ;
278290 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+ } ) ) ;
284296
285297 // The first item on the page is still 25. Change the page size to 8 so that we should now be
286298 // on the fourth page where the top item is index 24.
299+ component . pageEvent . calls . reset ( ) ;
287300 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+ } ) ) ;
293306
294307 // The first item on the page is 24. Change the page size to 16 so that we should now be
295308 // on the first page where the top item is index 0.
309+ component . pageEvent . calls . reset ( ) ;
296310 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+ } ) ) ;
302316 } ) ;
303317
304318 it ( 'should show a select only if there are multiple options' , ( ) => {
@@ -363,7 +377,7 @@ function getLastButton(fixture: ComponentFixture<any>) {
363377 [hidePageSize]="hidePageSize"
364378 [showFirstLastButtons]="showFirstLastButtons"
365379 [length]="length"
366- (page)="latestPageEvent = $event">
380+ (page)="pageEvent( $event) ">
367381 </mat-paginator>
368382 ` ,
369383} )
@@ -374,8 +388,7 @@ class MatPaginatorApp {
374388 hidePageSize = false ;
375389 showFirstLastButtons = false ;
376390 length = 100 ;
377-
378- latestPageEvent : PageEvent | null ;
391+ pageEvent = jasmine . createSpy ( 'page event' ) ;
379392
380393 @ViewChild ( MatPaginator ) paginator : MatPaginator ;
381394
0 commit comments