1
- import { async , ComponentFixture , TestBed , inject , tick , fakeAsync } from '@angular/core/testing' ;
2
- import { Component , ViewChild } from '@angular/core' ;
1
+ import { ComponentFixture , TestBed , tick , fakeAsync } from '@angular/core/testing' ;
2
+ import { Component , ViewChild , Type , Provider } from '@angular/core' ;
3
3
import { NoopAnimationsModule } from '@angular/platform-browser/animations' ;
4
4
import { dispatchMouseEvent } from '@angular/cdk/testing/private' ;
5
5
import { ThemePalette } from '@angular/material/core' ;
6
6
import { MatSelect } from '@angular/material/select' ;
7
7
import { By } from '@angular/platform-browser' ;
8
8
import { MatPaginatorModule , MatPaginator , MatPaginatorIntl } from './index' ;
9
+ import { MAT_PAGINATOR_DEFAULT_OPTIONS , MatPaginatorDefaultOptions } from './paginator' ;
9
10
10
11
11
12
describe ( 'MatPaginator' , ( ) => {
12
- let fixture : ComponentFixture < MatPaginatorApp > ;
13
- let component : MatPaginatorApp ;
14
- let paginator : MatPaginator ;
15
-
16
- beforeEach ( async ( ( ) => {
13
+ function createComponent < T > ( type : Type < T > , providers : Provider [ ] = [ ] ) : ComponentFixture < T > {
17
14
TestBed . configureTestingModule ( {
18
- imports : [
19
- MatPaginatorModule ,
20
- NoopAnimationsModule ,
21
- ] ,
22
- declarations : [
23
- MatPaginatorApp ,
24
- MatPaginatorWithoutPageSizeApp ,
25
- MatPaginatorWithoutOptionsApp ,
26
- MatPaginatorWithoutInputsApp ,
27
- MatPaginatorWithStringValues
28
- ] ,
29
- providers : [ MatPaginatorIntl ]
15
+ imports : [ MatPaginatorModule , NoopAnimationsModule ] ,
16
+ declarations : [ type ] ,
17
+ providers : [ MatPaginatorIntl , ...providers ]
30
18
} ) . compileComponents ( ) ;
31
- } ) ) ;
32
19
33
- beforeEach ( ( ) => {
34
- fixture = TestBed . createComponent ( MatPaginatorApp ) ;
20
+ const fixture = TestBed . createComponent ( type ) ;
35
21
fixture . detectChanges ( ) ;
36
-
37
- component = fixture . componentInstance ;
38
- paginator = component . paginator ;
39
- } ) ;
22
+ return fixture ;
23
+ }
40
24
41
25
describe ( 'with the default internationalization provider' , ( ) => {
42
26
it ( 'should show the right range text' , ( ) => {
27
+ const fixture = createComponent ( MatPaginatorApp ) ;
28
+ const component = fixture . componentInstance ;
43
29
const rangeElement = fixture . nativeElement . querySelector ( '.mat-paginator-range-label' ) ;
44
30
45
31
// View second page of list of 100, each page contains 10 items.
@@ -86,27 +72,32 @@ describe('MatPaginator', () => {
86
72
} ) ;
87
73
88
74
it ( 'should show right aria-labels for select and buttons' , ( ) => {
75
+ const fixture = createComponent ( MatPaginatorApp ) ;
89
76
const select = fixture . nativeElement . querySelector ( '.mat-select' ) ;
90
77
expect ( select . getAttribute ( 'aria-label' ) ) . toBe ( 'Items per page:' ) ;
91
78
92
79
expect ( getPreviousButton ( fixture ) . getAttribute ( 'aria-label' ) ) . toBe ( 'Previous page' ) ;
93
80
expect ( getNextButton ( fixture ) . getAttribute ( 'aria-label' ) ) . toBe ( 'Next page' ) ;
94
81
} ) ;
95
82
96
- it ( 'should re-render when the i18n labels change' ,
97
- inject ( [ MatPaginatorIntl ] , ( intl : MatPaginatorIntl ) => {
98
- const label = fixture . nativeElement . querySelector ( '.mat-paginator-page-size-label' ) ;
83
+ it ( 'should re-render when the i18n labels change' , ( ) => {
84
+ const fixture = createComponent ( MatPaginatorApp ) ;
85
+ const label = fixture . nativeElement . querySelector ( '.mat-paginator-page-size-label' ) ;
86
+ const intl = TestBed . get < MatPaginatorIntl > ( MatPaginatorIntl ) ;
99
87
100
- intl . itemsPerPageLabel = '1337 items per page' ;
101
- intl . changes . next ( ) ;
102
- fixture . detectChanges ( ) ;
88
+ intl . itemsPerPageLabel = '1337 items per page' ;
89
+ intl . changes . next ( ) ;
90
+ fixture . detectChanges ( ) ;
103
91
104
- expect ( label . textContent ! . trim ( ) ) . toBe ( '1337 items per page' ) ;
105
- } ) ) ;
92
+ expect ( label . textContent ! . trim ( ) ) . toBe ( '1337 items per page' ) ;
93
+ } ) ;
106
94
} ) ;
107
95
108
96
describe ( 'when navigating with the next and previous buttons' , ( ) => {
109
97
it ( 'should be able to go to the next page' , ( ) => {
98
+ const fixture = createComponent ( MatPaginatorApp ) ;
99
+ const component = fixture . componentInstance ;
100
+ const paginator = component . paginator ;
110
101
expect ( paginator . pageIndex ) . toBe ( 0 ) ;
111
102
112
103
dispatchMouseEvent ( getNextButton ( fixture ) , 'click' ) ;
@@ -119,6 +110,9 @@ describe('MatPaginator', () => {
119
110
} ) ;
120
111
121
112
it ( 'should be able to go to the previous page' , ( ) => {
113
+ const fixture = createComponent ( MatPaginatorApp ) ;
114
+ const component = fixture . componentInstance ;
115
+ const paginator = component . paginator ;
122
116
paginator . pageIndex = 1 ;
123
117
fixture . detectChanges ( ) ;
124
118
expect ( paginator . pageIndex ) . toBe ( 1 ) ;
@@ -134,6 +128,7 @@ describe('MatPaginator', () => {
134
128
} ) ;
135
129
136
130
it ( 'should be able to show the first/last buttons' , ( ) => {
131
+ const fixture = createComponent ( MatPaginatorApp ) ;
137
132
expect ( getFirstButton ( fixture ) )
138
133
. toBeNull ( 'Expected first button to not exist.' ) ;
139
134
@@ -151,6 +146,9 @@ describe('MatPaginator', () => {
151
146
} ) ;
152
147
153
148
it ( 'should mark itself as initialized' , fakeAsync ( ( ) => {
149
+ const fixture = createComponent ( MatPaginatorApp ) ;
150
+ const component = fixture . componentInstance ;
151
+ const paginator = component . paginator ;
154
152
let isMarkedInitialized = false ;
155
153
paginator . initialized . subscribe ( ( ) => isMarkedInitialized = true ) ;
156
154
@@ -159,16 +157,24 @@ describe('MatPaginator', () => {
159
157
} ) ) ;
160
158
161
159
it ( 'should not allow a negative pageSize' , ( ) => {
160
+ const fixture = createComponent ( MatPaginatorApp ) ;
161
+ const component = fixture . componentInstance ;
162
+ const paginator = component . paginator ;
162
163
paginator . pageSize = - 1337 ;
163
164
expect ( paginator . pageSize ) . toBeGreaterThanOrEqual ( 0 ) ;
164
165
} ) ;
165
166
166
167
it ( 'should not allow a negative pageIndex' , ( ) => {
168
+ const fixture = createComponent ( MatPaginatorApp ) ;
169
+ const component = fixture . componentInstance ;
170
+ const paginator = component . paginator ;
167
171
paginator . pageIndex = - 42 ;
168
172
expect ( paginator . pageIndex ) . toBeGreaterThanOrEqual ( 0 ) ;
169
173
} ) ;
170
174
171
175
it ( 'should be able to set the color of the form field' , ( ) => {
176
+ const fixture = createComponent ( MatPaginatorApp ) ;
177
+ const component = fixture . componentInstance ;
172
178
const formField : HTMLElement = fixture . nativeElement . querySelector ( '.mat-form-field' ) ;
173
179
174
180
expect ( formField . classList ) . toContain ( 'mat-primary' ) ;
@@ -181,8 +187,14 @@ describe('MatPaginator', () => {
181
187
} ) ;
182
188
183
189
describe ( 'when showing the first and last button' , ( ) => {
190
+ let fixture : ComponentFixture < MatPaginatorApp > ;
191
+ let component : MatPaginatorApp ;
192
+ let paginator : MatPaginator ;
184
193
185
194
beforeEach ( ( ) => {
195
+ fixture = createComponent ( MatPaginatorApp ) ;
196
+ component = fixture . componentInstance ;
197
+ paginator = component . paginator ;
186
198
component . showFirstLastButtons = true ;
187
199
fixture . detectChanges ( ) ;
188
200
} ) ;
@@ -245,6 +257,9 @@ describe('MatPaginator', () => {
245
257
} ) ;
246
258
247
259
it ( 'should mark for check when inputs are changed directly' , ( ) => {
260
+ const fixture = createComponent ( MatPaginatorApp ) ;
261
+ const component = fixture . componentInstance ;
262
+ const paginator = component . paginator ;
248
263
const rangeElement = fixture . nativeElement . querySelector ( '.mat-paginator-range-label' ) ;
249
264
250
265
expect ( rangeElement . innerText . trim ( ) ) . toBe ( '1 – 10 of 100' ) ;
@@ -270,21 +285,23 @@ describe('MatPaginator', () => {
270
285
} ) ;
271
286
272
287
it ( 'should default the page size options to the page size if no options provided' , ( ) => {
273
- const withoutOptionsAppFixture = TestBed . createComponent ( MatPaginatorWithoutOptionsApp ) ;
274
- withoutOptionsAppFixture . detectChanges ( ) ;
288
+ const fixture = createComponent ( MatPaginatorWithoutOptionsApp ) ;
289
+ fixture . detectChanges ( ) ;
275
290
276
- expect ( withoutOptionsAppFixture . componentInstance . paginator . _displayedPageSizeOptions )
277
- . toEqual ( [ 10 ] ) ;
291
+ expect ( fixture . componentInstance . paginator . _displayedPageSizeOptions ) . toEqual ( [ 10 ] ) ;
278
292
} ) ;
279
293
280
294
it ( 'should default the page size to the first page size option if not provided' , ( ) => {
281
- const withoutPageSizeAppFixture = TestBed . createComponent ( MatPaginatorWithoutPageSizeApp ) ;
282
- withoutPageSizeAppFixture . detectChanges ( ) ;
295
+ const fixture = createComponent ( MatPaginatorWithoutPageSizeApp ) ;
296
+ fixture . detectChanges ( ) ;
283
297
284
- expect ( withoutPageSizeAppFixture . componentInstance . paginator . pageSize ) . toEqual ( 10 ) ;
298
+ expect ( fixture . componentInstance . paginator . pageSize ) . toEqual ( 10 ) ;
285
299
} ) ;
286
300
287
301
it ( 'should show a sorted list of page size options including the current page size' , ( ) => {
302
+ const fixture = createComponent ( MatPaginatorApp ) ;
303
+ const component = fixture . componentInstance ;
304
+ const paginator = component . paginator ;
288
305
expect ( paginator . _displayedPageSizeOptions ) . toEqual ( [ 5 , 10 , 25 , 100 ] ) ;
289
306
290
307
component . pageSize = 30 ;
@@ -298,6 +315,10 @@ describe('MatPaginator', () => {
298
315
} ) ;
299
316
300
317
it ( 'should be able to change the page size while keeping the first item present' , ( ) => {
318
+ const fixture = createComponent ( MatPaginatorApp ) ;
319
+ const component = fixture . componentInstance ;
320
+ const paginator = component . paginator ;
321
+
301
322
// Start on the third page of a list of 100 with a page size of 10.
302
323
component . pageIndex = 4 ;
303
324
component . pageSize = 10 ;
@@ -339,6 +360,10 @@ describe('MatPaginator', () => {
339
360
} ) ;
340
361
341
362
it ( 'should keep track of the right number of pages' , ( ) => {
363
+ const fixture = createComponent ( MatPaginatorApp ) ;
364
+ const component = fixture . componentInstance ;
365
+ const paginator = component . paginator ;
366
+
342
367
component . pageSize = 10 ;
343
368
component . length = 100 ;
344
369
fixture . detectChanges ( ) ;
@@ -356,6 +381,10 @@ describe('MatPaginator', () => {
356
381
} ) ;
357
382
358
383
it ( 'should show a select only if there are multiple options' , ( ) => {
384
+ const fixture = createComponent ( MatPaginatorApp ) ;
385
+ const component = fixture . componentInstance ;
386
+ const paginator = component . paginator ;
387
+
359
388
expect ( paginator . _displayedPageSizeOptions ) . toEqual ( [ 5 , 10 , 25 , 100 ] ) ;
360
389
expect ( fixture . nativeElement . querySelector ( '.mat-select' ) ) . not . toBeNull ( ) ;
361
390
@@ -367,17 +396,18 @@ describe('MatPaginator', () => {
367
396
} ) ;
368
397
369
398
it ( 'should handle the number inputs being passed in as strings' , ( ) => {
370
- const withStringFixture = TestBed . createComponent ( MatPaginatorWithStringValues ) ;
371
- withStringFixture . detectChanges ( ) ;
399
+ const fixture = createComponent ( MatPaginatorWithStringValues ) ;
400
+ fixture . detectChanges ( ) ;
372
401
373
- const withStringPaginator = withStringFixture . componentInstance . paginator ;
402
+ const withStringPaginator = fixture . componentInstance . paginator ;
374
403
expect ( withStringPaginator . pageIndex ) . toEqual ( 0 ) ;
375
404
expect ( withStringPaginator . length ) . toEqual ( 100 ) ;
376
405
expect ( withStringPaginator . pageSize ) . toEqual ( 10 ) ;
377
406
expect ( withStringPaginator . pageSizeOptions ) . toEqual ( [ 5 , 10 , 25 , 100 ] ) ;
378
407
} ) ;
379
408
380
409
it ( 'should be able to hide the page size select' , ( ) => {
410
+ const fixture = createComponent ( MatPaginatorApp ) ;
381
411
const element = fixture . nativeElement ;
382
412
383
413
expect ( element . querySelector ( '.mat-paginator-page-size' ) )
@@ -391,6 +421,7 @@ describe('MatPaginator', () => {
391
421
} ) ;
392
422
393
423
it ( 'should be able to disable all the controls in the paginator via the binding' , ( ) => {
424
+ const fixture = createComponent ( MatPaginatorApp ) ;
394
425
const select : MatSelect =
395
426
fixture . debugElement . query ( By . directive ( MatSelect ) ) ! . componentInstance ;
396
427
@@ -414,6 +445,25 @@ describe('MatPaginator', () => {
414
445
expect ( getLastButton ( fixture ) . hasAttribute ( 'disabled' ) ) . toBe ( true ) ;
415
446
} ) ;
416
447
448
+
449
+ it ( 'should be able to configure the default options via a provider' , ( ) => {
450
+ const fixture = createComponent ( MatPaginatorWithoutInputsApp , [ {
451
+ provide : MAT_PAGINATOR_DEFAULT_OPTIONS ,
452
+ useValue : {
453
+ pageSize : 7 ,
454
+ pageSizeOptions : [ 7 , 14 , 21 ] ,
455
+ hidePageSize : true ,
456
+ showFirstLastButtons : true
457
+ } as MatPaginatorDefaultOptions
458
+ } ] ) ;
459
+ const paginator = fixture . componentInstance . paginator ;
460
+
461
+ expect ( paginator . pageSize ) . toBe ( 7 ) ;
462
+ expect ( paginator . pageSizeOptions ) . toEqual ( [ 7 , 14 , 21 ] ) ;
463
+ expect ( paginator . hidePageSize ) . toBe ( true ) ;
464
+ expect ( paginator . showFirstLastButtons ) . toBe ( true ) ;
465
+ } ) ;
466
+
417
467
} ) ;
418
468
419
469
function getPreviousButton ( fixture : ComponentFixture < any > ) {
0 commit comments