Asserts that keyboard interaction works correctly.
(
fixture: ComponentFixture<{stepper: MatStepper}>,
stepHeaders: DebugElement[],
orientation: StepperOrientation,
)
| 1642 | |
| 1643 | /** Asserts that keyboard interaction works correctly. */ |
| 1644 | function 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 |
no test coverage detected