From 089e4ecb8ab610e27ba59f4e19c4592e2a07f6b7 Mon Sep 17 00:00:00 2001 From: Andrew Seguin Date: Mon, 9 Mar 2020 12:50:08 -0700 Subject: [PATCH 1/2] test(table): add tests for MatTableDataSource --- src/material/table/table-data-source.spec.ts | 63 ++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 src/material/table/table-data-source.spec.ts diff --git a/src/material/table/table-data-source.spec.ts b/src/material/table/table-data-source.spec.ts new file mode 100644 index 000000000000..01269a5e237d --- /dev/null +++ b/src/material/table/table-data-source.spec.ts @@ -0,0 +1,63 @@ +import {MatTableDataSource} from './table-data-source'; +import {async, 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', () => { + const dataSource = new MatTableDataSource(); + + beforeEach(async(() => { + TestBed.configureTestingModule({ + imports: [MatSortModule, NoopAnimationsModule], + declarations: [MatSortApp], + }).compileComponents(); + })); + + describe('sort', () => { + let fixture: ComponentFixture; + let sort: MatSort; + + /** Randomly shuffles the array of data. */ + function shuffle(data: any[]) { + return data.slice().sort(() => (Math.random() - 0.5)); + } + + beforeEach(() => { + fixture = TestBed.createComponent(MatSortApp); + sort = fixture.componentInstance.sort; + fixture.detectChanges(); + }); + + /** 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 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 shuffledData = shuffle(data.slice()); + const sortedData = dataSource.sortData(shuffledData, 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']); + }); + }); +}); + +@Component({ + template: `
` +}) +class MatSortApp { + @ViewChild(MatSort, {static: true}) sort: MatSort; +} From 143e58b57de59fe9e83ac152af6d5f113d207261 Mon Sep 17 00:00:00 2001 From: andrewseguin Date: Thu, 9 Apr 2020 09:58:50 -0700 Subject: [PATCH 2/2] switch shuffle to reverse --- src/material/table/table-data-source.spec.ts | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/material/table/table-data-source.spec.ts b/src/material/table/table-data-source.spec.ts index 01269a5e237d..98a1de02315b 100644 --- a/src/material/table/table-data-source.spec.ts +++ b/src/material/table/table-data-source.spec.ts @@ -18,11 +18,6 @@ describe('MatTableDataSource', () => { let fixture: ComponentFixture; let sort: MatSort; - /** Randomly shuffles the array of data. */ - function shuffle(data: any[]) { - return data.slice().sort(() => (Math.random() - 0.5)); - } - beforeEach(() => { fixture = TestBed.createComponent(MatSortApp); sort = fixture.componentInstance.sort; @@ -40,8 +35,8 @@ describe('MatTableDataSource', () => { // Set the active sort to be on the "prop" key sort.active = 'prop'; - const shuffledData = shuffle(data.slice()); - const sortedData = dataSource.sortData(shuffledData, sort); + const reversedData = data.slice().reverse(); + const sortedData = dataSource.sortData(reversedData, sort); expect(sortedData).toEqual(data); }