1
- import { dispatchMouseEvent } from '@angular/cdk/testing/private' ;
2
1
import { ChangeDetectorRef , Component , Provider , Type , ViewChild , inject } from '@angular/core' ;
3
2
import { ComponentFixture , TestBed , fakeAsync , tick } from '@angular/core/testing' ;
4
3
import { ThemePalette } from '@angular/material/core' ;
@@ -135,7 +134,7 @@ describe('MatPaginator', () => {
135
134
const paginator = component . paginator ;
136
135
expect ( paginator . pageIndex ) . toBe ( 0 ) ;
137
136
138
- dispatchMouseEvent ( getNextButton ( fixture ) , ' click' ) ;
137
+ getNextButton ( fixture ) . click ( ) ;
139
138
140
139
expect ( paginator . pageIndex ) . toBe ( 1 ) ;
141
140
expect ( component . pageEvent ) . toHaveBeenCalledWith (
@@ -154,7 +153,7 @@ describe('MatPaginator', () => {
154
153
fixture . detectChanges ( ) ;
155
154
expect ( paginator . pageIndex ) . toBe ( 1 ) ;
156
155
157
- dispatchMouseEvent ( getPreviousButton ( fixture ) , ' click' ) ;
156
+ getPreviousButton ( fixture ) . click ( ) ;
158
157
159
158
expect ( paginator . pageIndex ) . toBe ( 0 ) ;
160
159
expect ( component . pageEvent ) . toHaveBeenCalledWith (
@@ -164,12 +163,37 @@ describe('MatPaginator', () => {
164
163
} ) ,
165
164
) ;
166
165
} ) ;
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
+ } ) ;
167
192
} ) ;
168
193
169
194
it ( 'should be able to show the first/last buttons' , ( ) => {
170
195
const fixture = createComponent ( MatPaginatorApp ) ;
171
196
expect ( getFirstButton ( fixture ) ) . withContext ( 'Expected first button to not exist.' ) . toBeNull ( ) ;
172
-
173
197
expect ( getLastButton ( fixture ) ) . withContext ( 'Expected last button to not exist.' ) . toBeNull ( ) ;
174
198
175
199
fixture . componentInstance . showFirstLastButtons = true ;
@@ -271,7 +295,7 @@ describe('MatPaginator', () => {
271
295
it ( 'should be able to go to the last page via the last page button' , ( ) => {
272
296
expect ( paginator . pageIndex ) . toBe ( 0 ) ;
273
297
274
- dispatchMouseEvent ( getLastButton ( fixture ) , ' click' ) ;
298
+ getLastButton ( fixture ) . click ( ) ;
275
299
276
300
expect ( paginator . pageIndex ) . toBe ( 9 ) ;
277
301
expect ( component . pageEvent ) . toHaveBeenCalledWith (
@@ -287,7 +311,7 @@ describe('MatPaginator', () => {
287
311
fixture . detectChanges ( ) ;
288
312
expect ( paginator . pageIndex ) . toBe ( 3 ) ;
289
313
290
- dispatchMouseEvent ( getFirstButton ( fixture ) , ' click' ) ;
314
+ getFirstButton ( fixture ) . click ( ) ;
291
315
292
316
expect ( paginator . pageIndex ) . toBe ( 0 ) ;
293
317
expect ( component . pageEvent ) . toHaveBeenCalledWith (
@@ -305,7 +329,7 @@ describe('MatPaginator', () => {
305
329
expect ( paginator . hasNextPage ( ) ) . toBe ( false ) ;
306
330
307
331
component . pageEvent . calls . reset ( ) ;
308
- dispatchMouseEvent ( getNextButton ( fixture ) , ' click' ) ;
332
+ getNextButton ( fixture ) . click ( ) ;
309
333
310
334
expect ( component . pageEvent ) . not . toHaveBeenCalled ( ) ;
311
335
expect ( paginator . pageIndex ) . toBe ( 9 ) ;
@@ -316,11 +340,35 @@ describe('MatPaginator', () => {
316
340
expect ( paginator . hasPreviousPage ( ) ) . toBe ( false ) ;
317
341
318
342
component . pageEvent . calls . reset ( ) ;
319
- dispatchMouseEvent ( getPreviousButton ( fixture ) , ' click' ) ;
343
+ getPreviousButton ( fixture ) . click ( ) ;
320
344
321
345
expect ( component . pageEvent ) . not . toHaveBeenCalled ( ) ;
322
346
expect ( paginator . pageIndex ) . toBe ( 0 ) ;
323
347
} ) ;
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
+ } ) ;
324
372
} ) ;
325
373
326
374
it ( 'should mark for check when inputs are changed directly' , ( ) => {
@@ -569,19 +617,19 @@ describe('MatPaginator', () => {
569
617
} ) ;
570
618
} ) ;
571
619
572
- function getPreviousButton ( fixture : ComponentFixture < any > ) {
620
+ function getPreviousButton ( fixture : ComponentFixture < any > ) : HTMLButtonElement {
573
621
return fixture . nativeElement . querySelector ( '.mat-mdc-paginator-navigation-previous' ) ;
574
622
}
575
623
576
- function getNextButton ( fixture : ComponentFixture < any > ) {
624
+ function getNextButton ( fixture : ComponentFixture < any > ) : HTMLButtonElement {
577
625
return fixture . nativeElement . querySelector ( '.mat-mdc-paginator-navigation-next' ) ;
578
626
}
579
627
580
- function getFirstButton ( fixture : ComponentFixture < any > ) {
628
+ function getFirstButton ( fixture : ComponentFixture < any > ) : HTMLButtonElement {
581
629
return fixture . nativeElement . querySelector ( '.mat-mdc-paginator-navigation-first' ) ;
582
630
}
583
631
584
- function getLastButton ( fixture : ComponentFixture < any > ) {
632
+ function getLastButton ( fixture : ComponentFixture < any > ) : HTMLButtonElement {
585
633
return fixture . nativeElement . querySelector ( '.mat-mdc-paginator-navigation-last' ) ;
586
634
}
587
635
0 commit comments