1- import { dispatchMouseEvent } from '@angular/cdk/testing/private' ;
21import { ChangeDetectorRef , Component , Provider , Type , ViewChild , inject } from '@angular/core' ;
32import { ComponentFixture , TestBed , fakeAsync , tick } from '@angular/core/testing' ;
43import { ThemePalette } from '@angular/material/core' ;
@@ -135,7 +134,7 @@ describe('MatPaginator', () => {
135134 const paginator = component . paginator ;
136135 expect ( paginator . pageIndex ) . toBe ( 0 ) ;
137136
138- dispatchMouseEvent ( getNextButton ( fixture ) , ' click' ) ;
137+ getNextButton ( fixture ) . click ( ) ;
139138
140139 expect ( paginator . pageIndex ) . toBe ( 1 ) ;
141140 expect ( component . pageEvent ) . toHaveBeenCalledWith (
@@ -154,7 +153,7 @@ describe('MatPaginator', () => {
154153 fixture . detectChanges ( ) ;
155154 expect ( paginator . pageIndex ) . toBe ( 1 ) ;
156155
157- dispatchMouseEvent ( getPreviousButton ( fixture ) , ' click' ) ;
156+ getPreviousButton ( fixture ) . click ( ) ;
158157
159158 expect ( paginator . pageIndex ) . toBe ( 0 ) ;
160159 expect ( component . pageEvent ) . toHaveBeenCalledWith (
@@ -164,12 +163,37 @@ describe('MatPaginator', () => {
164163 } ) ,
165164 ) ;
166165 } ) ;
166+
167+ it ( 'should not navigate to the next page when the paginator is disabled' , ( ) => {
168+ const fixture = createComponent ( MatPaginatorApp ) ;
169+ expect ( fixture . componentInstance . paginator . pageIndex ) . toBe ( 0 ) ;
170+
171+ fixture . componentInstance . disabled = true ;
172+ fixture . changeDetectorRef . markForCheck ( ) ;
173+ fixture . detectChanges ( ) ;
174+ getNextButton ( fixture ) . click ( ) ;
175+ expect ( fixture . componentInstance . paginator . pageIndex ) . toBe ( 0 ) ;
176+ } ) ;
177+
178+ it ( 'should not navigate to the previous page when the paginator is disabled' , ( ) => {
179+ const fixture = createComponent ( MatPaginatorApp ) ;
180+ fixture . componentInstance . pageIndex = 1 ;
181+ fixture . changeDetectorRef . markForCheck ( ) ;
182+ fixture . detectChanges ( ) ;
183+
184+ expect ( fixture . componentInstance . paginator . pageIndex ) . toBe ( 1 ) ;
185+
186+ fixture . componentInstance . disabled = true ;
187+ fixture . changeDetectorRef . markForCheck ( ) ;
188+ fixture . detectChanges ( ) ;
189+ getPreviousButton ( fixture ) . click ( ) ;
190+ expect ( fixture . componentInstance . paginator . pageIndex ) . toBe ( 1 ) ;
191+ } ) ;
167192 } ) ;
168193
169194 it ( 'should be able to show the first/last buttons' , ( ) => {
170195 const fixture = createComponent ( MatPaginatorApp ) ;
171196 expect ( getFirstButton ( fixture ) ) . withContext ( 'Expected first button to not exist.' ) . toBeNull ( ) ;
172-
173197 expect ( getLastButton ( fixture ) ) . withContext ( 'Expected last button to not exist.' ) . toBeNull ( ) ;
174198
175199 fixture . componentInstance . showFirstLastButtons = true ;
@@ -271,7 +295,7 @@ describe('MatPaginator', () => {
271295 it ( 'should be able to go to the last page via the last page button' , ( ) => {
272296 expect ( paginator . pageIndex ) . toBe ( 0 ) ;
273297
274- dispatchMouseEvent ( getLastButton ( fixture ) , ' click' ) ;
298+ getLastButton ( fixture ) . click ( ) ;
275299
276300 expect ( paginator . pageIndex ) . toBe ( 9 ) ;
277301 expect ( component . pageEvent ) . toHaveBeenCalledWith (
@@ -287,7 +311,7 @@ describe('MatPaginator', () => {
287311 fixture . detectChanges ( ) ;
288312 expect ( paginator . pageIndex ) . toBe ( 3 ) ;
289313
290- dispatchMouseEvent ( getFirstButton ( fixture ) , ' click' ) ;
314+ getFirstButton ( fixture ) . click ( ) ;
291315
292316 expect ( paginator . pageIndex ) . toBe ( 0 ) ;
293317 expect ( component . pageEvent ) . toHaveBeenCalledWith (
@@ -305,7 +329,7 @@ describe('MatPaginator', () => {
305329 expect ( paginator . hasNextPage ( ) ) . toBe ( false ) ;
306330
307331 component . pageEvent . calls . reset ( ) ;
308- dispatchMouseEvent ( getNextButton ( fixture ) , ' click' ) ;
332+ getNextButton ( fixture ) . click ( ) ;
309333
310334 expect ( component . pageEvent ) . not . toHaveBeenCalled ( ) ;
311335 expect ( paginator . pageIndex ) . toBe ( 9 ) ;
@@ -316,11 +340,35 @@ describe('MatPaginator', () => {
316340 expect ( paginator . hasPreviousPage ( ) ) . toBe ( false ) ;
317341
318342 component . pageEvent . calls . reset ( ) ;
319- dispatchMouseEvent ( getPreviousButton ( fixture ) , ' click' ) ;
343+ getPreviousButton ( fixture ) . click ( ) ;
320344
321345 expect ( component . pageEvent ) . not . toHaveBeenCalled ( ) ;
322346 expect ( paginator . pageIndex ) . toBe ( 0 ) ;
323347 } ) ;
348+
349+ it ( 'should not navigate to the last page when the paginator is disabled' , ( ) => {
350+ expect ( fixture . componentInstance . paginator . pageIndex ) . toBe ( 0 ) ;
351+
352+ fixture . componentInstance . disabled = true ;
353+ fixture . changeDetectorRef . markForCheck ( ) ;
354+ fixture . detectChanges ( ) ;
355+ getLastButton ( fixture ) . click ( ) ;
356+ expect ( fixture . componentInstance . paginator . pageIndex ) . toBe ( 0 ) ;
357+ } ) ;
358+
359+ it ( 'should not navigate to the first page when the paginator is disabled' , ( ) => {
360+ fixture . componentInstance . pageIndex = 1 ;
361+ fixture . changeDetectorRef . markForCheck ( ) ;
362+ fixture . detectChanges ( ) ;
363+
364+ expect ( fixture . componentInstance . paginator . pageIndex ) . toBe ( 1 ) ;
365+
366+ fixture . componentInstance . disabled = true ;
367+ fixture . changeDetectorRef . markForCheck ( ) ;
368+ fixture . detectChanges ( ) ;
369+ getFirstButton ( fixture ) . click ( ) ;
370+ expect ( fixture . componentInstance . paginator . pageIndex ) . toBe ( 1 ) ;
371+ } ) ;
324372 } ) ;
325373
326374 it ( 'should mark for check when inputs are changed directly' , ( ) => {
@@ -569,19 +617,19 @@ describe('MatPaginator', () => {
569617 } ) ;
570618} ) ;
571619
572- function getPreviousButton ( fixture : ComponentFixture < any > ) {
620+ function getPreviousButton ( fixture : ComponentFixture < any > ) : HTMLButtonElement {
573621 return fixture . nativeElement . querySelector ( '.mat-mdc-paginator-navigation-previous' ) ;
574622}
575623
576- function getNextButton ( fixture : ComponentFixture < any > ) {
624+ function getNextButton ( fixture : ComponentFixture < any > ) : HTMLButtonElement {
577625 return fixture . nativeElement . querySelector ( '.mat-mdc-paginator-navigation-next' ) ;
578626}
579627
580- function getFirstButton ( fixture : ComponentFixture < any > ) {
628+ function getFirstButton ( fixture : ComponentFixture < any > ) : HTMLButtonElement {
581629 return fixture . nativeElement . querySelector ( '.mat-mdc-paginator-navigation-first' ) ;
582630}
583631
584- function getLastButton ( fixture : ComponentFixture < any > ) {
632+ function getLastButton ( fixture : ComponentFixture < any > ) : HTMLButtonElement {
585633 return fixture . nativeElement . querySelector ( '.mat-mdc-paginator-navigation-last' ) ;
586634}
587635
0 commit comments