Skip to content

Instantly share code, notes, and snippets.

@florestankorp
Last active June 20, 2023 12:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save florestankorp/132e0167f1c031c797e3e85b86ae1f5f to your computer and use it in GitHub Desktop.
Save florestankorp/132e0167f1c031c797e3e85b86ae1f5f to your computer and use it in GitHub Desktop.
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