Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('should focus the first item that starts with a letter', fakeAsync(() => {
keyManager.onKeydown(createKeyboardEvent('keydown', 84, 't')); // types "t"
tick(debounceInterval);
expect(keyManager.activeItem).toBe(itemList.items[1]);
}));
it('DELETE emits the (removed) event', () => {
const DELETE_EVENT = createKeyboardEvent('keydown', DELETE) as KeyboardEvent;
spyOn(testComponent, 'chipRemove');
// Use the delete to remove the chip
chipInstance._handleKeydown(DELETE_EVENT);
fixture.detectChanges();
expect(testComponent.chipRemove).toHaveBeenCalled();
});
it('should focus previous item when press LEFT ARROW', () => {
let nativeChips = chipListboxNativeElement.querySelectorAll('mat-chip-option');
let lastNativeChip = nativeChips[nativeChips.length - 1] as HTMLElement;
let LEFT_EVENT = createKeyboardEvent('keydown', LEFT_ARROW, undefined, lastNativeChip);
let array = chips.toArray();
let lastIndex = array.length - 1;
let lastItem = array[lastIndex];
// Focus the last item in the array
lastItem.focus();
expect(manager.activeItemIndex).toEqual(lastIndex);
// Press the LEFT arrow
chipListboxInstance._keydown(LEFT_EVENT);
chipListboxInstance._blur(); // Simulate focus leaving the listbox and going to the chip.
fixture.detectChanges();
// It focuses the next-to-last item
expect(manager.activeItemIndex).toEqual(lastIndex - 1);
});
it('SPACE ignores selection', () => {
const SPACE_EVENT: KeyboardEvent = createKeyboardEvent('keydown', SPACE) as KeyboardEvent;
spyOn(testComponent, 'chipSelectionChange');
// Use the spacebar to attempt to select the chip
chipInstance._keydown(SPACE_EVENT);
fixture.detectChanges();
expect(chipInstance.selected).toBeFalsy();
expect(testComponent.chipSelectionChange).not.toHaveBeenCalled();
});
beforeEach(() => {
itemList = new FakeQueryList();
fakeKeyEvents = {
downArrow: createKeyboardEvent('keydown', DOWN_ARROW),
upArrow: createKeyboardEvent('keydown', UP_ARROW),
leftArrow: createKeyboardEvent('keydown', LEFT_ARROW),
rightArrow: createKeyboardEvent('keydown', RIGHT_ARROW),
tab: createKeyboardEvent('keydown', TAB),
unsupported: createKeyboardEvent('keydown', 192) // corresponds to the tilde character (~)
};
});
it('should not do anything if a modifier key is pressed', () => {
const rightArrowEvent = createKeyboardEvent('keydown', RIGHT_ARROW);
const enterEvent = createKeyboardEvent('keydown', ENTER);
[rightArrowEvent, enterEvent].forEach(event => {
Object.defineProperty(event, 'shiftKey', {get: () => true});
});
appComponent.tabHeader.focusIndex = 0;
fixture.detectChanges();
expect(appComponent.tabHeader.focusIndex).toBe(0);
dispatchEvent(tabListContainer, rightArrowEvent);
fixture.detectChanges();
expect(appComponent.tabHeader.focusIndex).toBe(0);
expect(rightArrowEvent.defaultPrevented).toBe(false);
expect(appComponent.selectedIndex).toBe(0);
it('should be able to use keyboard select with SPACE', () => {
const testListItem = listOptions[1].nativeElement as HTMLElement;
const SPACE_EVENT = createKeyboardEvent('keydown', SPACE, undefined, testListItem);
const selectList =
selectionList.injector.get(MatSelectionList).selectedOptions;
expect(selectList.selected.length).toBe(0);
dispatchFakeEvent(testListItem, 'focus');
selectionList.componentInstance._keydown(SPACE_EVENT);
fixture.detectChanges();
expect(selectList.selected.length).toBe(1);
expect(SPACE_EVENT.defaultPrevented).toBe(true);
});
it('does not emit (chipEnd) when a non-separator key is pressed', () => {
let ENTER_EVENT = createKeyboardEvent('keydown', ENTER, undefined, inputNativeElement);
spyOn(testChipInput, 'add');
chipInputDirective.separatorKeyCodes = [COMMA];
fixture.detectChanges();
chipInputDirective._keydown(ENTER_EVENT);
expect(testChipInput.add).not.toHaveBeenCalled();
});
it('should not close a dialog via the escape key with a modifier', fakeAsync(() => {
dialog.open(PizzaMsg, {
viewContainerRef: testViewContainerRef
});
const event = createKeyboardEvent('keydown', ESCAPE);
Object.defineProperty(event, 'altKey', {get: () => true});
viewContainerFixture.detectChanges();
flush();
expect(overlayContainerElement.querySelector('mat-dialog-container')).toBeTruthy();
expect(event.defaultPrevented).toBe(false);
}));
it('should check if multiple specific modifier keys are pressed', () => {
const ctrlEvent = createKeyboardEvent('keydown', 0, '', undefined, {control: true});
const ctrlAltShiftEvent = createKeyboardEvent(
'keydown', 0, '', undefined, {control: true, alt: true, shift: true});
expect(hasModifierKey(ctrlEvent, 'altKey', 'shiftKey')).toBe(false);
expect(hasModifierKey(ctrlAltShiftEvent, 'altKey', 'shiftKey')).toBe(true);
});
});