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,31 @@ 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
+ }
207
+
208
+ // Go to the last page.
209
+ fixture . componentInstance . paginator . lastPage ( ) ;
210
+ fixture . detectChanges ( ) ;
211
+
212
+ // Switch to a smaller data set.
213
+ dataSource . data = [ { a : 'a_0' , b : 'b_0' , c : 'c_0' } ] ;
214
+ fixture . detectChanges ( ) ;
215
+ tick ( ) ;
216
+ fixture . detectChanges ( ) ;
217
+
218
+ expectTableToMatchContent ( tableElement , [
219
+ [ 'Column A' , 'Column B' , 'Column C' ] ,
220
+ [ 'a_0' , 'b_0' , 'c_0' ] ,
221
+ [ 'Footer A' , 'Footer B' , 'Footer C' ] ,
222
+ ] ) ;
223
+ } ) ) ;
224
+
200
225
it ( 'should be able to filter the table contents' , fakeAsync ( ( ) => {
201
226
// Change filter to a_1, should match one row
202
227
dataSource . filter = 'a_1' ;
@@ -650,7 +675,7 @@ class MatTableWithWhenRowApp {
650
675
<mat-paginator [pageSize]="5"></mat-paginator>
651
676
`
652
677
} )
653
- class ArrayDataSourceMatTableApp implements OnInit {
678
+ class ArrayDataSourceMatTableApp implements AfterViewInit {
654
679
underlyingDataSource = new FakeDataSource ( ) ;
655
680
dataSource = new MatTableDataSource < TestData > ( ) ;
656
681
columnsToRender = [ 'column_a' , 'column_b' , 'column_c' ] ;
@@ -673,9 +698,9 @@ class ArrayDataSourceMatTableApp implements OnInit {
673
698
} ) ;
674
699
}
675
700
676
- ngOnInit ( ) {
677
- this . dataSource ! . sort = this . sort ;
678
- this . dataSource ! . paginator = this . paginator ;
701
+ ngAfterViewInit ( ) {
702
+ this . dataSource . sort = this . sort ;
703
+ this . dataSource . paginator = this . paginator ;
679
704
}
680
705
}
681
706
0 commit comments