|
| 1 | +import {TestBed, ComponentFixture, waitForAsync} from '@angular/core/testing'; |
| 2 | +import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed'; |
| 3 | +import {MatChipHarness, MatChipListHarness} from '@angular/material/chips/testing'; |
| 4 | +import {HarnessLoader} from '@angular/cdk/testing'; |
| 5 | +import {BrowserDynamicTestingModule, platformBrowserDynamicTesting} |
| 6 | + from '@angular/platform-browser-dynamic/testing'; |
| 7 | +import {ChipsHarnessExample} from './chips-harness-example'; |
| 8 | +import {NoopAnimationsModule} from '@angular/platform-browser/animations'; |
| 9 | +import {MatChipsModule} from '@angular/material/chips'; |
| 10 | + |
| 11 | +describe('ChipsHarnessExample', () => { |
| 12 | + let fixture: ComponentFixture<ChipsHarnessExample>; |
| 13 | + let loader: HarnessLoader; |
| 14 | + beforeAll(() => { |
| 15 | + TestBed.initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting()); |
| 16 | + }); |
| 17 | + beforeEach( |
| 18 | + waitForAsync(() => { |
| 19 | + TestBed.configureTestingModule({ |
| 20 | + imports: [MatChipsModule, NoopAnimationsModule], |
| 21 | + declarations: [ChipsHarnessExample] |
| 22 | + }).compileComponents(); |
| 23 | + fixture = TestBed.createComponent(ChipsHarnessExample); |
| 24 | + fixture.detectChanges(); |
| 25 | + loader = TestbedHarnessEnvironment.loader(fixture); |
| 26 | + }) |
| 27 | + ); |
| 28 | + |
| 29 | + it('should get whether a chip list is disabled', async () => { |
| 30 | + const chipList = await loader.getHarness(MatChipListHarness); |
| 31 | + |
| 32 | + expect(await chipList.isDisabled()).toBeFalse(); |
| 33 | + |
| 34 | + fixture.componentInstance.isDisabled = true; |
| 35 | + fixture.detectChanges(); |
| 36 | + |
| 37 | + expect(await chipList.isDisabled()).toBeTrue(); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should get the orientation of a chip list', async () => { |
| 41 | + const chipList = await loader.getHarness(MatChipListHarness); |
| 42 | + |
| 43 | + expect(await chipList.getOrientation()).toEqual('horizontal'); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should be able to get the selected chips in a list', async () => { |
| 47 | + const chipList = await loader.getHarness(MatChipListHarness); |
| 48 | + const chips = await chipList.getChips(); |
| 49 | + |
| 50 | + expect((await chipList.getChips({selected: true})).length).toBe(0); |
| 51 | + await chips[1].select(); |
| 52 | + |
| 53 | + const selectedChips = await chipList.getChips({selected: true}); |
| 54 | + expect(await Promise.all(selectedChips.map(chip => chip.getText()))).toEqual(['Chip 2']); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should be able to trigger chip removal', async () => { |
| 58 | + const chip = await loader.getHarness(MatChipHarness); |
| 59 | + expect(fixture.componentInstance.remove).not.toHaveBeenCalled(); |
| 60 | + await chip.remove(); |
| 61 | + expect(fixture.componentInstance.remove).toHaveBeenCalled(); |
| 62 | + }); |
| 63 | +}); |
0 commit comments