1
1
import { DataSource } from '@angular/cdk/collections' ;
2
- import { Component , OnInit , ViewChild } from '@angular/core' ;
2
+ import { Component , OnInit , ViewChild , AfterViewInit } from '@angular/core' ;
3
3
import {
4
4
async ,
5
5
ComponentFixture ,
@@ -151,14 +151,14 @@ describe('MatTable', () => {
151
151
let dataSource : MatTableDataSource < TestData > ;
152
152
let component : ArrayDataSourceMatTableApp ;
153
153
154
- beforeEach ( ( ) => {
154
+ beforeEach ( fakeAsync ( ( ) => {
155
155
fixture = TestBed . createComponent ( ArrayDataSourceMatTableApp ) ;
156
156
fixture . detectChanges ( ) ;
157
157
158
158
tableElement = fixture . nativeElement . querySelector ( '.mat-table' ) ;
159
159
component = fixture . componentInstance ;
160
160
dataSource = fixture . componentInstance . dataSource ;
161
- } ) ;
161
+ } ) ) ;
162
162
163
163
it ( 'should create table and display data source contents' , ( ) => {
164
164
expectTableToMatchContent ( tableElement , [
@@ -197,6 +197,33 @@ describe('MatTable', () => {
197
197
] ) ;
198
198
} ) ;
199
199
200
+ it ( 'should update the page index when switching to a smaller data set from a page' ,
201
+ fakeAsync ( ( ) => {
202
+ // Add 20 rows so we can switch pages.
203
+ for ( let i = 0 ; i < 20 ; i ++ ) {
204
+ component . underlyingDataSource . addData ( ) ;
205
+ fixture . detectChanges ( ) ;
206
+ tick ( ) ;
207
+ fixture . detectChanges ( ) ;
208
+ }
209
+
210
+ // Go to the last page.
211
+ fixture . componentInstance . paginator . lastPage ( ) ;
212
+ fixture . detectChanges ( ) ;
213
+
214
+ // Switch to a smaller data set.
215
+ dataSource . data = [ { a : 'a_0' , b : 'b_0' , c : 'c_0' } ] ;
216
+ fixture . detectChanges ( ) ;
217
+ tick ( ) ;
218
+ fixture . detectChanges ( ) ;
219
+
220
+ expectTableToMatchContent ( tableElement , [
221
+ [ 'Column A' , 'Column B' , 'Column C' ] ,
222
+ [ 'a_0' , 'b_0' , 'c_0' ] ,
223
+ [ 'Footer A' , 'Footer B' , 'Footer C' ] ,
224
+ ] ) ;
225
+ } ) ) ;
226
+
200
227
it ( 'should be able to filter the table contents' , fakeAsync ( ( ) => {
201
228
// Change filter to a_1, should match one row
202
229
dataSource . filter = 'a_1' ;
@@ -650,7 +677,7 @@ class MatTableWithWhenRowApp {
650
677
<mat-paginator [pageSize]="5"></mat-paginator>
651
678
`
652
679
} )
653
- class ArrayDataSourceMatTableApp implements OnInit {
680
+ class ArrayDataSourceMatTableApp implements AfterViewInit {
654
681
underlyingDataSource = new FakeDataSource ( ) ;
655
682
dataSource = new MatTableDataSource < TestData > ( ) ;
656
683
columnsToRender = [ 'column_a' , 'column_b' , 'column_c' ] ;
@@ -673,9 +700,9 @@ class ArrayDataSourceMatTableApp implements OnInit {
673
700
} ) ;
674
701
}
675
702
676
- ngOnInit ( ) {
677
- this . dataSource ! . sort = this . sort ;
678
- this . dataSource ! . paginator = this . paginator ;
703
+ ngAfterViewInit ( ) {
704
+ this . dataSource . sort = this . sort ;
705
+ this . dataSource . paginator = this . paginator ;
679
706
}
680
707
}
681
708
0 commit comments