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