Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add code to KeyboardEvent #512

Merged
merged 1 commit into from
Nov 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions projects/spectator/src/lib/event-creators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export function createKeyboardEvent(type: string, keyOrKeyCode: string | number
// Webkit Browsers don't set the keyCode when calling the init function.
// See related bug https://bugs.webkit.org/show_bug.cgi?id=16735
Object.defineProperties(event, {
code: { get: () => keyCode },
keyCode: { get: () => keyCode },
key: { get: () => key },
target: { get: () => target },
Expand Down
1 change: 1 addition & 0 deletions projects/spectator/test/events/events.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ <h1>{{ event }}</h1>
(keyup.control.shift.a)="onPressCtrlShiftA()"
(keyup.dot)="onPressDot()"
(keyup.arrowleft)="onPressArrowLeft($event)"
(keyup.arrowup)="onPressArrowUp($event)"
(keyup.control.shift.arrowright)="onPressArrowRight($event)"
>
5 changes: 5 additions & 0 deletions projects/spectator/test/events/events.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ describe('EventsComponent', () => {
expect(spectator.query('h1')).toHaveText('pressed ArrowLeft:40');
});

it('should include key and code when KeyboardEventOptions are passed', () => {
spectator.dispatchKeyboardEvent('input', 'keyup', { key: 'ArrowUp', keyCode: 38 });
expect(spectator.query('h1')).toHaveText('pressed ArrowUp:38');
});

it('should parse modifiers from KeyboardEventOptions.key', () => {
spectator.dispatchKeyboardEvent('input', 'keyup', { key: 'ctrl.shift.ArrowRight', keyCode: 39 });
expect(spectator.query('h1')).toHaveText('pressed ArrowRight:39');
Expand Down
4 changes: 4 additions & 0 deletions projects/spectator/test/events/events.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,8 @@ export class EventsComponent {
public onPressArrowRight(event: KeyboardEvent): void {
this.event = `pressed ArrowRight:${event.keyCode}`;
}

public onPressArrowUp(event: KeyboardEvent): void {
this.event = `pressed ${event.key}:${event.code}`;
}
}