Skip to content

Commit 7a43f07

Browse files
authored
feat: added typehint to the trigger method (#551)
`dom-event-types` is no longer a dependency: as the package has no types, it was inlined in the codebase. `wrapper.trigger()` now accepts a `string` (as it was) and a `DomEventName` (which a union type of all possible DOM events, like 'click', 'input', etc...). The method has now 2 overloads which should provide a better experience when developing, as `wrapper.trigger('cli|` should offer `click` in the autocomplete of your IDE.
1 parent dd5767a commit 7a43f07

File tree

6 files changed

+976
-37
lines changed

6 files changed

+976
-37
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
"@vue/compiler-sfc": "3.0.11",
2626
"babel-jest": "^26.6.3",
2727
"babel-preset-jest": "^26.6.2",
28-
"dom-event-types": "^1.0.0",
2928
"husky": "^6.0.0",
3029
"jest": "25.5.4",
3130
"jsdom": "^16.5.3",

src/baseWrapper.ts

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { textContent } from './utils'
2-
import { createDOMEvent } from './createDomEvent'
32
import type { TriggerOptions } from './createDomEvent'
43
import { nextTick } from 'vue'
4+
import { createDOMEvent } from './createDomEvent'
5+
import { DomEventName } from './constants/dom-event-types'
56

67
export default class BaseWrapper<ElementType extends Element> {
78
private readonly wrapperElement: ElementType
@@ -44,6 +45,31 @@ export default class BaseWrapper<ElementType extends Element> {
4445
return true
4546
}
4647

48+
protected isDisabled = () => {
49+
const validTagsToBeDisabled = [
50+
'BUTTON',
51+
'COMMAND',
52+
'FIELDSET',
53+
'KEYGEN',
54+
'OPTGROUP',
55+
'OPTION',
56+
'SELECT',
57+
'TEXTAREA',
58+
'INPUT'
59+
]
60+
const hasDisabledAttribute = this.attributes().disabled !== undefined
61+
const elementCanBeDisabled = validTagsToBeDisabled.includes(
62+
this.element.tagName
63+
)
64+
65+
return hasDisabledAttribute && elementCanBeDisabled
66+
}
67+
68+
async trigger(
69+
eventString: DomEventName,
70+
options?: TriggerOptions
71+
): Promise<void>
72+
async trigger(eventString: string, options?: TriggerOptions): Promise<void>
4773
async trigger(eventString: string, options?: TriggerOptions) {
4874
if (options && options['target']) {
4975
throw Error(
@@ -53,27 +79,7 @@ export default class BaseWrapper<ElementType extends Element> {
5379
)
5480
}
5581

56-
const isDisabled = () => {
57-
const validTagsToBeDisabled = [
58-
'BUTTON',
59-
'COMMAND',
60-
'FIELDSET',
61-
'KEYGEN',
62-
'OPTGROUP',
63-
'OPTION',
64-
'SELECT',
65-
'TEXTAREA',
66-
'INPUT'
67-
]
68-
const hasDisabledAttribute = this.attributes().disabled !== undefined
69-
const elementCanBeDisabled = validTagsToBeDisabled.includes(
70-
this.element.tagName
71-
)
72-
73-
return hasDisabledAttribute && elementCanBeDisabled
74-
}
75-
76-
if (this.element && !isDisabled()) {
82+
if (this.element && !this.isDisabled()) {
7783
const event = createDOMEvent(eventString, options)
7884
this.element.dispatchEvent(event)
7985
}

0 commit comments

Comments
 (0)