Skip to content

feat: Add event modifiers #590

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

Merged
merged 5 commits into from
Jul 28, 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
4 changes: 2 additions & 2 deletions src/baseWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { textContent } from './utils'
import type { TriggerOptions } from './createDomEvent'
import { nextTick } from 'vue'
import { createDOMEvent } from './createDomEvent'
import { DomEventName } from './constants/dom-event-types'
import { DomEventName, DomEventNameWithModifier } from './constants/dom-events'

export default class BaseWrapper<ElementType extends Element> {
private readonly wrapperElement: ElementType
Expand Down Expand Up @@ -66,7 +66,7 @@ export default class BaseWrapper<ElementType extends Element> {
}

async trigger(
eventString: DomEventName,
eventString: DomEventNameWithModifier,
options?: TriggerOptions
): Promise<void>
async trigger(eventString: string, options?: TriggerOptions): Promise<void>
Expand Down
44 changes: 44 additions & 0 deletions src/constants/dom-event-types.ts → src/constants/dom-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,50 @@ export interface DomEvent {

export type DomEventName = keyof typeof domEvents

export const ignorableKeyModifiers = [
'stop',
'prevent',
'self',
'exact',
'prevent',
'capture'
]
export const systemKeyModifiers = ['ctrl', 'shift', 'alt', 'meta'] as const
export const mouseKeyModifiers = ['left', 'middle', 'right'] as const

export const keyCodesByKeyName = {
backspace: 8,
tab: 9,
enter: 13,
esc: 27,
space: 32,
pageup: 33,
pagedown: 34,
end: 35,
home: 36,
left: 37,
up: 38,
right: 39,
down: 40,
insert: 45,
delete: 46
} as const

export type KeyName = keyof typeof keyCodesByKeyName
export type Modifier =
| typeof systemKeyModifiers[number]
| typeof mouseKeyModifiers[number]

export type DomEventNameWithModifier =
| DomEventName
| `${DomEventName}.${typeof systemKeyModifiers[number]}`
| `click.${typeof mouseKeyModifiers[number]}`
| `click.${typeof systemKeyModifiers[number]}.${typeof mouseKeyModifiers[number]}`
| `${'keydown' | 'keyup'}.${keyof typeof keyCodesByKeyName}`
| `${
| 'keydown'
| 'keyup'}.${typeof systemKeyModifiers[number]}.${keyof typeof keyCodesByKeyName}`

const domEvents = {
abort: {
eventInterface: 'Event',
Expand Down
48 changes: 13 additions & 35 deletions src/createDomEvent.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,14 @@
import domEvents, {
DomEvent,
DomEventName,
EventInterface
} from './constants/dom-event-types'

const keyCodesByKeyName = {
backspace: 8,
tab: 9,
enter: 13,
esc: 27,
space: 32,
pageup: 33,
pagedown: 34,
end: 35,
home: 36,
left: 37,
up: 38,
right: 39,
down: 40,
insert: 45,
delete: 46
} as const

// modifiers to keep an eye on
const ignorableKeyModifiers = ['stop', 'prevent', 'self', 'exact']
const systemKeyModifiers = ['ctrl', 'shift', 'alt', 'meta'] as const
const mouseKeyModifiers = ['left', 'middle', 'right'] as const

type KeyName = keyof typeof keyCodesByKeyName
type Modifier =
| typeof systemKeyModifiers[number]
| typeof mouseKeyModifiers[number]
DomEventNameWithModifier,
KeyName,
Modifier,
ignorableKeyModifiers,
systemKeyModifiers,
mouseKeyModifiers,
keyCodesByKeyName
} from './constants/dom-events'

interface TriggerOptions {
code?: String
Expand All @@ -40,7 +18,7 @@ interface TriggerOptions {
}

interface EventParams {
eventType: DomEventName | string
eventType: DomEventName
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why was this also a string?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure 🤔

modifiers: KeyName[]
options?: TriggerOptions
}
Expand Down Expand Up @@ -104,7 +82,7 @@ function getEventProperties(eventParams: EventParams) {
}
}

const meta: DomEvent = domEvents[eventType as DomEventName] || {
const meta: DomEvent = domEvents[eventType] || {
eventInterface: 'Event',
cancelable: true,
bubbles: true
Expand Down Expand Up @@ -163,14 +141,14 @@ function createEvent(eventParams: EventParams) {
}

function createDOMEvent(
eventString: DomEventName | string,
eventString: DomEventNameWithModifier | string,
options?: TriggerOptions
) {
// split eventString like `keydown.ctrl.shift.c` into `keydown` and array of modifiers
// split eventString like `keydown.ctrl.shift` into `keydown` and array of modifiers
const [eventType, ...modifiers] = eventString.split('.')

const eventParams: EventParams = {
eventType,
eventType: eventType as DomEventName,
modifiers: modifiers as KeyName[],
options
}
Expand Down
2 changes: 1 addition & 1 deletion src/vueWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ShapeFlags } from '@vue/shared'
import pretty from 'pretty'

import { config } from './config'
import domEvents from './constants/dom-event-types'
import domEvents from './constants/dom-events'
import { DOMWrapper } from './domWrapper'
import {
FindAllComponentsSelector,
Expand Down
1 change: 1 addition & 0 deletions tests/trigger.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ describe('trigger', () => {
expect(keydownHandler).toHaveBeenCalledTimes(2)
expect(keydownHandler.mock.calls[1][0].key).toBe('Enter')

// is correctly parsed when using modifier
await wrapper.trigger('keydown.enter')
expect(keydownHandler).toHaveBeenCalledTimes(3)
expect(keydownHandler.mock.calls[2][0].key).toBe('enter')
Expand Down