forked from angular/components
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtable-data-source.spec.ts
More file actions
130 lines (108 loc) · 4.4 KB
/
table-data-source.spec.ts
File metadata and controls
130 lines (108 loc) · 4.4 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import {MatTableDataSource} from './table-data-source';
import {ComponentFixture, fakeAsync, TestBed, tick} from '@angular/core/testing';
import {MatSort, MatSortModule} from '@angular/material/sort';
import {Component, ViewChild} from '@angular/core';
declare global {
interface Window {
ngDevMode?: object | null;
}
}
describe('MatTableDataSource', () => {
describe('sort', () => {
let dataSource: MatTableDataSource<{'prop': string | number}>;
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: (string | number)[]) {
// 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 = [{'prop': 1}, {'prop': 2}, {'prop': 3}];
expect(dataSource.filteredData).toEqual([{'prop': 1}, {'prop': 2}, {'prop': 3}]);
dataSource.disconnect();
dataSource.data = [{'prop': 3}, {'prop': 2}, {'prop': 1}];
expect(dataSource.filteredData).toEqual([{'prop': 3}, {'prop': 2}, {'prop': 1}]);
});
it('should filter data', () => {
dataSource.data = [{'prop': 1}, {'prop': 'foo'}, {'prop': 'banana'}];
dataSource.filter = 'b';
expect(dataSource.filteredData).toEqual([{'prop': 'banana'}]);
});
it('does not warn in non-dev mode when filtering non-object data', fakeAsync(() => {
const warnSpy = spyOn(console, 'warn');
window.ngDevMode = null;
dataSource.data = [1, 2, 3, 4, 5] as unknown as {'prop': number}[];
dataSource.filter = '1';
tick();
expect(warnSpy).not.toHaveBeenCalled();
expect(dataSource.filteredData).toEqual([]);
}));
it('displays the warning in dev mode when filtering non-object data', fakeAsync(() => {
const warnSpy = spyOn(console, 'warn');
window.ngDevMode = {};
dataSource.data = [1, 2, 3, 4, 5] as unknown as {'prop': number}[];
dataSource.filter = '1';
tick();
expect(warnSpy).toHaveBeenCalledWith(
jasmine.stringContaining('requires data to be a non-null object'),
);
expect(dataSource.filteredData).toEqual([]);
}));
});
});
@Component({
template: `<div matSort matSortDirection="asc"></div>`,
imports: [MatSortModule],
})
class MatSortApp {
@ViewChild(MatSort, {static: true}) sort!: MatSort;
}