forked from angular/components
-
Notifications
You must be signed in to change notification settings - Fork 0
/
table-harness-example.spec.ts
86 lines (76 loc) · 3.62 KB
/
table-harness-example.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
import {ComponentFixture, TestBed} from '@angular/core/testing';
import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed';
import {MatLegacyTableHarness} from '@angular/material/legacy-table/testing';
import {HarnessLoader, parallel} from '@angular/cdk/testing';
import {MatLegacyTableModule} from '@angular/material/legacy-table';
import {TableHarnessExample} from './table-harness-example';
describe('TableHarnessExample', () => {
let fixture: ComponentFixture<TableHarnessExample>;
let loader: HarnessLoader;
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [MatLegacyTableModule],
declarations: [TableHarnessExample],
}).compileComponents();
fixture = TestBed.createComponent(TableHarnessExample);
fixture.detectChanges();
loader = TestbedHarnessEnvironment.loader(fixture);
});
it('should load harness for a table', async () => {
const tables = await loader.getAllHarnesses(MatLegacyTableHarness);
expect(tables.length).toBe(1);
});
it('should get the different kinds of rows in the table', async () => {
const table = await loader.getHarness(MatLegacyTableHarness);
const headerRows = await table.getHeaderRows();
const footerRows = await table.getFooterRows();
const rows = await table.getRows();
expect(headerRows.length).toBe(1);
expect(footerRows.length).toBe(1);
expect(rows.length).toBe(10);
});
it('should get cells inside a row', async () => {
const table = await loader.getHarness(MatLegacyTableHarness);
const headerRows = await table.getHeaderRows();
const footerRows = await table.getFooterRows();
const rows = await table.getRows();
const headerCells = (await parallel(() => headerRows.map(row => row.getCells()))).map(
row => row.length,
);
const footerCells = (await parallel(() => footerRows.map(row => row.getCells()))).map(
row => row.length,
);
const cells = (await parallel(() => rows.map(row => row.getCells()))).map(row => row.length);
expect(headerCells).toEqual([4]);
expect(cells).toEqual([4, 4, 4, 4, 4, 4, 4, 4, 4, 4]);
expect(footerCells).toEqual([4]);
});
it('should be able to get the text of a cell', async () => {
const table = await loader.getHarness(MatLegacyTableHarness);
const secondRow = (await table.getRows())[1];
const cells = await secondRow.getCells();
const cellTexts = await parallel(() => cells.map(cell => cell.getText()));
expect(cellTexts).toEqual(['2', 'Helium', '4.0026', 'He']);
});
it('should be able to get the column name of a cell', async () => {
const table = await loader.getHarness(MatLegacyTableHarness);
const fifthRow = (await table.getRows())[1];
const cells = await fifthRow.getCells();
const cellColumnNames = await parallel(() => cells.map(cell => cell.getColumnName()));
expect(cellColumnNames).toEqual(['position', 'name', 'weight', 'symbol']);
});
it('should be able to filter cells by text', async () => {
const table = await loader.getHarness(MatLegacyTableHarness);
const firstRow = (await table.getRows())[0];
const cells = await firstRow.getCells({text: '1.0079'});
const cellTexts = await parallel(() => cells.map(cell => cell.getText()));
expect(cellTexts).toEqual(['1.0079']);
});
it('should be able to filter cells by column name', async () => {
const table = await loader.getHarness(MatLegacyTableHarness);
const firstRow = (await table.getRows())[0];
const cells = await firstRow.getCells({columnName: 'symbol'});
const cellTexts = await parallel(() => cells.map(cell => cell.getText()));
expect(cellTexts).toEqual(['H']);
});
});