|
| 1 | +import {HarnessLoader, parallel} from '@angular/cdk/testing'; |
| 2 | +import {TestBed, ComponentFixture, waitForAsync} from '@angular/core/testing'; |
| 3 | +import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed'; |
| 4 | +import {MatListHarness} from '@angular/material/list/testing'; |
| 5 | +import {BrowserDynamicTestingModule, platformBrowserDynamicTesting} |
| 6 | + from '@angular/platform-browser-dynamic/testing'; |
| 7 | +import {MatListModule} from '@angular/material/list'; |
| 8 | +import {ListHarnessExample} from './list-harness-example'; |
| 9 | + |
| 10 | +describe('ListHarnessExample', () => { |
| 11 | + let fixture: ComponentFixture<ListHarnessExample>; |
| 12 | + let loader: HarnessLoader; |
| 13 | + |
| 14 | + beforeAll(() => { |
| 15 | + TestBed.initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting()); |
| 16 | + }); |
| 17 | + |
| 18 | + beforeEach( |
| 19 | + waitForAsync(() => { |
| 20 | + TestBed.configureTestingModule({ |
| 21 | + imports: [MatListModule], |
| 22 | + declarations: [ListHarnessExample] |
| 23 | + }).compileComponents(); |
| 24 | + fixture = TestBed.createComponent(ListHarnessExample); |
| 25 | + fixture.detectChanges(); |
| 26 | + loader = TestbedHarnessEnvironment.loader(fixture); |
| 27 | + }) |
| 28 | + ); |
| 29 | + |
| 30 | + it('should get all items', async () => { |
| 31 | + const list = await loader.getHarness(MatListHarness); |
| 32 | + const items = await list.getItems(); |
| 33 | + expect(await parallel(() => items.map(i => i.getText()))) |
| 34 | + .toEqual(['Item 1', 'Item 2', 'Item 3']); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should get all items matching text', async () => { |
| 38 | + const list = await loader.getHarness(MatListHarness); |
| 39 | + const items = await list.getItems({text: /[13]/}); |
| 40 | + expect(await parallel(() => items.map(i => i.getText()))).toEqual(['Item 1', 'Item 3']); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should get items by subheader', async () => { |
| 44 | + const list = await loader.getHarness(MatListHarness); |
| 45 | + const sections = await list.getItemsGroupedBySubheader(); |
| 46 | + expect(sections.length).toBe(3); |
| 47 | + expect(sections[0].heading).toBeUndefined(); |
| 48 | + expect(await parallel(() => sections[0].items.map(i => i.getText()))).toEqual(['Item 1']); |
| 49 | + expect(sections[1].heading).toBe('Section 1'); |
| 50 | + expect(await parallel(() => sections[1].items.map(i => i.getText()))) |
| 51 | + .toEqual(['Item 2', 'Item 3']); |
| 52 | + expect(sections[2].heading).toBe('Section 2'); |
| 53 | + expect(sections[2].items.length).toEqual(0); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should get list item text and lines', async () => { |
| 57 | + const list = await loader.getHarness(MatListHarness); |
| 58 | + const items = await list.getItems(); |
| 59 | + expect(items.length).toBe(3); |
| 60 | + expect(await items[0].getText()).toBe('Item 1'); |
| 61 | + expect(await items[0].getLinesText()).toEqual(['Item', '1']); |
| 62 | + expect(await items[1].getText()).toBe('Item 2'); |
| 63 | + expect(await items[1].getLinesText()).toEqual([]); |
| 64 | + expect(await items[2].getText()).toBe('Item 3'); |
| 65 | + expect(await items[2].getLinesText()).toEqual([]); |
| 66 | + }); |
| 67 | +}); |
0 commit comments