-
Notifications
You must be signed in to change notification settings - Fork 6.8k
/
Copy pathtable-data-source.spec.ts
99 lines (84 loc) · 3.34 KB
/
table-data-source.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import {MatTableDataSource} from './table-data-source';
import {waitForAsync, ComponentFixture, TestBed} from '@angular/core/testing';
import {MatSort, MatSortModule} from '@angular/material/sort';
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
import {Component, ViewChild} from '@angular/core';
describe('MatTableDataSource', () => {
beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
imports: [MatSortModule, NoopAnimationsModule, MatSortApp],
});
}));
describe('sort', () => {
let dataSource: MatTableDataSource<any>;
let fixture: ComponentFixture<MatSortApp>;
let sort: MatSort;
beforeEach(() => {
fixture = TestBed.createComponent(MatSortApp);
fixture.detectChanges();
dataSource = new MatTableDataSource();
sort = fixture.componentInstance.sort;
dataSource.sort = sort;
});
/** Test the data source's `sortData` function. */
function testSortWithValues(values: any[]) {
// The data source and MatSort expect the list to contain objects with values, where
// the sort should be performed over a particular key.
// Map the values into an array of objects where each value is keyed by "prop"
// e.g. [0, 1, 2] -> [{prop: 0}, {prop: 1}, {prop: 2}]
const data = values.map(v => ({'prop': v}));
// Set the active sort to be on the "prop" key
sort.active = 'prop';
const reversedData = data.slice().reverse();
const sortedData = dataSource.sortData(reversedData, sort);
expect(sortedData).toEqual(data);
}
it('should be able to correctly sort an array of numbers', () => {
testSortWithValues([-2, -1, 0, 1, 2]);
});
it('should be able to correctly sort an array of string', () => {
testSortWithValues(['apples', 'bananas', 'cherries', 'lemons', 'strawberries']);
});
it('should be able to correctly sort an array of strings and numbers', () => {
testSortWithValues([3, 'apples', 'bananas', 'cherries', 'lemons', 'strawberries']);
});
it('should be able to correctly sort an array of strings and numbers with left zero', () => {
testSortWithValues([
'001',
'2',
3,
4,
'apples',
'bananas',
'cherries',
'lemons',
'strawberries',
]);
});
it('should unsubscribe from the re-render stream when disconnected', () => {
const spy = spyOn(dataSource._renderChangesSubscription!, 'unsubscribe');
dataSource.disconnect();
expect(spy).toHaveBeenCalledTimes(1);
});
it('should re-subscribe to the sort stream when re-connecting after being disconnected', () => {
dataSource.disconnect();
const spy = spyOn(fixture.componentInstance.sort.sortChange, 'subscribe');
dataSource.connect();
expect(spy).toHaveBeenCalledTimes(1);
});
it('should update filteredData even if the data source is disconnected', () => {
dataSource.data = [1, 2, 3, 4, 5];
expect(dataSource.filteredData).toEqual([1, 2, 3, 4, 5]);
dataSource.disconnect();
dataSource.data = [5, 4, 3, 2, 1];
expect(dataSource.filteredData).toEqual([5, 4, 3, 2, 1]);
});
});
});
@Component({
template: `<div matSort matSortDirection="asc"></div>`,
imports: [MatSortModule],
})
class MatSortApp {
@ViewChild(MatSort, {static: true}) sort: MatSort;
}