|
| 1 | +import {TestBed, ComponentFixture, waitForAsync} from '@angular/core/testing'; |
| 2 | +import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed'; |
| 3 | +import {MatSortHarness} from '@angular/material/sort/testing'; |
| 4 | +import {HarnessLoader, parallel} from '@angular/cdk/testing'; |
| 5 | +import {BrowserDynamicTestingModule, platformBrowserDynamicTesting} |
| 6 | + from '@angular/platform-browser-dynamic/testing'; |
| 7 | +import {MatSortModule} from '@angular/material/sort'; |
| 8 | +import {SortHarnessExample} from './sort-harness-example'; |
| 9 | +import {NoopAnimationsModule} from '@angular/platform-browser/animations'; |
| 10 | + |
| 11 | +describe('SortHarnessExample', () => { |
| 12 | + let fixture: ComponentFixture<SortHarnessExample>; |
| 13 | + let loader: HarnessLoader; |
| 14 | + |
| 15 | + beforeAll(() => { |
| 16 | + TestBed.initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting()); |
| 17 | + }); |
| 18 | + |
| 19 | + beforeEach( |
| 20 | + waitForAsync(() => { |
| 21 | + TestBed.configureTestingModule({ |
| 22 | + imports: [MatSortModule, NoopAnimationsModule], |
| 23 | + declarations: [SortHarnessExample] |
| 24 | + }).compileComponents(); |
| 25 | + fixture = TestBed.createComponent(SortHarnessExample); |
| 26 | + fixture.detectChanges(); |
| 27 | + loader = TestbedHarnessEnvironment.loader(fixture); |
| 28 | + }) |
| 29 | + ); |
| 30 | + |
| 31 | + it('should load harness for mat-sort', async () => { |
| 32 | + const sorts = await loader.getAllHarnesses(MatSortHarness); |
| 33 | + expect(sorts.length).toBe(1); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should be able to filter headers by their sorted state', async () => { |
| 37 | + const sort = await loader.getHarness(MatSortHarness); |
| 38 | + let headers = await sort.getSortHeaders({sortDirection: ''}); |
| 39 | + expect(headers.length).toBe(5); |
| 40 | + |
| 41 | + await headers[0].click(); |
| 42 | + |
| 43 | + headers = await sort.getSortHeaders({sortDirection: 'asc'}); |
| 44 | + |
| 45 | + expect(headers.length).toBe(1); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should be able to get the label of a header', async () => { |
| 49 | + const sort = await loader.getHarness(MatSortHarness); |
| 50 | + const headers = await sort.getSortHeaders(); |
| 51 | + const labels = await parallel(() => headers.map(header => header.getLabel())); |
| 52 | + expect(labels).toEqual(['Dessert', 'Calories', 'Fat', 'Carbs', 'Protein']); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should get the disabled state of a header', async () => { |
| 56 | + const sort = await loader.getHarness(MatSortHarness); |
| 57 | + const thirdHeader = (await sort.getSortHeaders())[2]; |
| 58 | + |
| 59 | + expect(await thirdHeader.isDisabled()).toBe(false); |
| 60 | + |
| 61 | + fixture.componentInstance.disableThirdHeader = true; |
| 62 | + fixture.detectChanges(); |
| 63 | + |
| 64 | + expect(await thirdHeader.isDisabled()).toBe(true); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should get the sorted direction of a header', async () => { |
| 68 | + const sort = await loader.getHarness(MatSortHarness); |
| 69 | + const secondHeader = (await sort.getSortHeaders())[1]; |
| 70 | + |
| 71 | + expect(await secondHeader.getSortDirection()).toBe(''); |
| 72 | + |
| 73 | + await secondHeader.click(); |
| 74 | + expect(await secondHeader.getSortDirection()).toBe('asc'); |
| 75 | + |
| 76 | + await secondHeader.click(); |
| 77 | + expect(await secondHeader.getSortDirection()).toBe('desc'); |
| 78 | + }); |
| 79 | +}); |
0 commit comments