MCPcopy Create free account
hub / github.com/angular/components / assertCorrectKeyboardInteraction

Function assertCorrectKeyboardInteraction

src/material/stepper/stepper.spec.ts:1644–1742  ·  view source on GitHub ↗

Asserts that keyboard interaction works correctly.

(
  fixture: ComponentFixture<{stepper: MatStepper}>,
  stepHeaders: DebugElement[],
  orientation: StepperOrientation,
)

Source from the content-addressed store, hash-verified

1642
1643/** Asserts that keyboard interaction works correctly. */
1644function assertCorrectKeyboardInteraction(
1645 fixture: ComponentFixture<{stepper: MatStepper}>,
1646 stepHeaders: DebugElement[],
1647 orientation: StepperOrientation,
1648) {
1649 const stepper = fixture.componentInstance.stepper;
1650 const nextKey = orientation === 'vertical' ? DOWN_ARROW : RIGHT_ARROW;
1651 const prevKey = orientation === 'vertical' ? UP_ARROW : LEFT_ARROW;
1652
1653 expect(stepper._getFocusIndex()).toBe(0);
1654 expect(stepper.selectedIndex).toBe(0);
1655
1656 let stepHeaderEl = stepHeaders[0].nativeElement;
1657 dispatchKeyboardEvent(stepHeaderEl, 'keydown', nextKey);
1658 fixture.detectChanges();
1659
1660 expect(stepper._getFocusIndex())
1661 .withContext('Expected index of focused step to increase by 1 after pressing the next key.')
1662 .toBe(1);
1663 expect(stepper.selectedIndex)
1664 .withContext('Expected index of selected step to remain unchanged after pressing the next key.')
1665 .toBe(0);
1666
1667 stepHeaderEl = stepHeaders[1].nativeElement;
1668 dispatchKeyboardEvent(stepHeaderEl, 'keydown', ENTER);
1669 fixture.detectChanges();
1670
1671 expect(stepper._getFocusIndex())
1672 .withContext('Expected index of focused step to remain unchanged after ENTER event.')
1673 .toBe(1);
1674 expect(stepper.selectedIndex)
1675 .withContext(
1676 'Expected index of selected step to change to index of focused step ' + 'after ENTER event.',
1677 )
1678 .toBe(1);
1679
1680 stepHeaderEl = stepHeaders[1].nativeElement;
1681 dispatchKeyboardEvent(stepHeaderEl, 'keydown', prevKey);
1682 fixture.detectChanges();
1683
1684 expect(stepper._getFocusIndex())
1685 .withContext(
1686 'Expected index of focused step to decrease by 1 after pressing the ' + 'previous key.',
1687 )
1688 .toBe(0);
1689 expect(stepper.selectedIndex)
1690 .withContext(
1691 'Expected index of selected step to remain unchanged after pressing the ' + 'previous key.',
1692 )
1693 .toBe(1);
1694
1695 // When the focus is on the last step and right arrow key is pressed, the focus should cycle
1696 // through to the first step.
1697 (stepper as any)._keyManager.updateActiveItem(2);
1698 stepHeaderEl = stepHeaders[2].nativeElement;
1699 dispatchKeyboardEvent(stepHeaderEl, 'keydown', nextKey);
1700 fixture.detectChanges();
1701

Callers 1

stepper.spec.tsFile · 0.85

Calls 3

dispatchKeyboardEventFunction · 0.85
_getFocusIndexMethod · 0.80
updateActiveItemMethod · 0.80

Tested by

no test coverage detected