Last active
June 20, 2023 12:24
-
-
Save florestankorp/132e0167f1c031c797e3e85b86ae1f5f to your computer and use it in GitHub Desktop.
ES2022 compatible version of: https://material.angular.io/components/stepper/examples#stepper-harness
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { HarnessLoader, parallel } from '@angular/cdk/testing'; | |
import { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed'; | |
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing'; | |
import { MatStepperModule } from '@angular/material/stepper'; | |
import { | |
MatStepHarness, | |
MatStepperHarness, | |
MatStepperNextHarness, | |
MatStepperPreviousHarness, | |
} from '@angular/material/stepper/testing'; | |
import { NoopAnimationsModule } from '@angular/platform-browser/animations'; | |
import { TestComponent } from './test.component'; | |
describe('TestComponent', () => { | |
let fixture: ComponentFixture<TestComponent>; | |
let loader: HarnessLoader; | |
let stepper: MatStepperHarness; | |
let steps: MatStepHarness[]; | |
beforeEach(waitForAsync(() => { | |
TestBed.configureTestingModule({ | |
imports: [NoopAnimationsModule, MatStepperModule], | |
declarations: [TestComponent], | |
}) | |
.compileComponents() | |
.then(() => { | |
fixture = TestBed.createComponent(TestComponent); | |
loader = TestbedHarnessEnvironment.loader(fixture); | |
loader.getHarness(MatStepperHarness).then(async (harness) => { | |
stepper = harness; | |
steps = await stepper.getSteps(); | |
}); | |
}); | |
})); | |
it('should get the steps of a stepper', () => { | |
expect(steps.length).toEqual(3); | |
}); | |
it('should be able to get the template-based label of a step', async () => { | |
expect( | |
await parallel(() => { | |
return steps.map((step) => step.getLabel()); | |
}) | |
).toEqual(['One', 'Two', 'Three']); | |
}); | |
it('should go forward when pressing the next button', async () => { | |
const [, secondStep] = steps; | |
const nextButton = await secondStep.getHarness(MatStepperNextHarness); | |
await secondStep.select(); | |
expect( | |
await parallel(() => steps.map((step) => step.isSelected())) | |
).toEqual([false, true, false]); | |
await nextButton.click(); | |
expect( | |
await parallel(() => steps.map((step) => step.isSelected())) | |
).toEqual([false, false, true]); | |
}); | |
it('should go back when pressing the back button', async () => { | |
const [, secondStep] = steps; | |
const backButton = await secondStep.getHarness(MatStepperPreviousHarness); | |
await secondStep.select(); | |
expect( | |
await parallel(() => steps.map((step) => step.isSelected())) | |
).toEqual([false, true, false]); | |
await backButton.click(); | |
expect( | |
await parallel(() => steps.map((step) => step.isSelected())) | |
).toEqual([true, false, false]); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment