Skip to content

test(table): add tests for MatTableDataSource #18767

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 9, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions src/material/table/table-data-source.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
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<MatSortApp>;
let sort: MatSort;

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 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']);
});
});
});

@Component({
template: `<div matSort matSortDirection="asc"></div>`
})
class MatSortApp {
@ViewChild(MatSort, {static: true}) sort: MatSort;
}