|
| 1 | +import {TestBed, ComponentFixture, waitForAsync} from '@angular/core/testing'; |
| 2 | +import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed'; |
| 3 | +import {MatTableHarness} from '@angular/material/table/testing'; |
| 4 | +import {HarnessLoader, parallel} from '@angular/cdk/testing'; |
| 5 | +import {BrowserDynamicTestingModule, platformBrowserDynamicTesting} |
| 6 | + from '@angular/platform-browser-dynamic/testing'; |
| 7 | +import {MatTableModule} from '@angular/material/table'; |
| 8 | +import {TableHarnessExample} from './table-harness-example'; |
| 9 | + |
| 10 | +describe('TableHarnessExample', () => { |
| 11 | + let fixture: ComponentFixture<TableHarnessExample>; |
| 12 | + let loader: HarnessLoader; |
| 13 | + |
| 14 | + beforeAll(() => { |
| 15 | + TestBed.initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting()); |
| 16 | + }); |
| 17 | + |
| 18 | + beforeEach( |
| 19 | + waitForAsync(() => { |
| 20 | + TestBed.configureTestingModule({ |
| 21 | + imports: [MatTableModule], |
| 22 | + declarations: [TableHarnessExample] |
| 23 | + }).compileComponents(); |
| 24 | + fixture = TestBed.createComponent(TableHarnessExample); |
| 25 | + fixture.detectChanges(); |
| 26 | + loader = TestbedHarnessEnvironment.loader(fixture); |
| 27 | + }) |
| 28 | + ); |
| 29 | + |
| 30 | + it('should load harness for a table', async () => { |
| 31 | + const tables = await loader.getAllHarnesses(MatTableHarness); |
| 32 | + expect(tables.length).toBe(1); |
| 33 | + }); |
| 34 | + |
| 35 | + it('should get the different kinds of rows in the table', async () => { |
| 36 | + const table = await loader.getHarness(MatTableHarness); |
| 37 | + const headerRows = await table.getHeaderRows(); |
| 38 | + const footerRows = await table.getFooterRows(); |
| 39 | + const rows = await table.getRows(); |
| 40 | + expect(headerRows.length).toBe(1); |
| 41 | + expect(footerRows.length).toBe(1); |
| 42 | + expect(rows.length).toBe(10); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should get cells inside a row', async () => { |
| 46 | + const table = await loader.getHarness(MatTableHarness); |
| 47 | + const headerRows = await table.getHeaderRows(); |
| 48 | + const footerRows = await table.getFooterRows(); |
| 49 | + const rows = await table.getRows(); |
| 50 | + const headerCells = (await parallel(() => headerRows.map(row => row.getCells()))) |
| 51 | + .map(row => row.length); |
| 52 | + const footerCells = (await parallel(() => footerRows.map(row => row.getCells()))) |
| 53 | + .map(row => row.length); |
| 54 | + const cells = (await parallel(() => rows.map(row => row.getCells()))) |
| 55 | + .map(row => row.length); |
| 56 | + |
| 57 | + expect(headerCells).toEqual([4]); |
| 58 | + expect(cells).toEqual([4, 4, 4, 4, 4, 4, 4, 4, 4, 4]); |
| 59 | + expect(footerCells).toEqual([4]); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should be able to get the text of a cell', async () => { |
| 63 | + const table = await loader.getHarness(MatTableHarness); |
| 64 | + const secondRow = (await table.getRows())[1]; |
| 65 | + const cells = await secondRow.getCells(); |
| 66 | + const cellTexts = await parallel(() => cells.map(cell => cell.getText())); |
| 67 | + expect(cellTexts).toEqual(['2', 'Helium', '4.0026', 'He']); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should be able to get the column name of a cell', async () => { |
| 71 | + const table = await loader.getHarness(MatTableHarness); |
| 72 | + const fifthRow = (await table.getRows())[1]; |
| 73 | + const cells = await fifthRow.getCells(); |
| 74 | + const cellColumnNames = await parallel(() => cells.map(cell => cell.getColumnName())); |
| 75 | + expect(cellColumnNames).toEqual(['position', 'name', 'weight', 'symbol']); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should be able to filter cells by text', async () => { |
| 79 | + const table = await loader.getHarness(MatTableHarness); |
| 80 | + const firstRow = (await table.getRows())[0]; |
| 81 | + const cells = await firstRow.getCells({text: '1.0079'}); |
| 82 | + const cellTexts = await parallel(() => cells.map(cell => cell.getText())); |
| 83 | + expect(cellTexts).toEqual(['1.0079']); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should be able to filter cells by column name', async () => { |
| 87 | + const table = await loader.getHarness(MatTableHarness); |
| 88 | + const firstRow = (await table.getRows())[0]; |
| 89 | + const cells = await firstRow.getCells({columnName: 'symbol'}); |
| 90 | + const cellTexts = await parallel(() => cells.map(cell => cell.getText())); |
| 91 | + expect(cellTexts).toEqual(['H']); |
| 92 | + }); |
| 93 | +}); |
0 commit comments