Skip to content

Commit

Permalink
better click event
Browse files Browse the repository at this point in the history
  • Loading branch information
nichoth committed Sep 27, 2024
1 parent 8732e94 commit 77c3604
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 32 deletions.
48 changes: 25 additions & 23 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,48 +279,50 @@ export function waitFor (
/**
* Dispatch the `click`` method on an element specified by selector.
*
* @param {string|HTMLElement|Element} selector - A CSS selector string, or
* @param {string|Element} selector - A CSS selector string, or
* an instance of an HTMLElement.
* @returns {Promise<void>}
*
* @example
* ```js
* await t.click('.class button', 'Click a button')
* await click('.class button', 'Click a button')
* ```
*/
export async function click (selector:HTMLElement|Element|string):Promise<void> {
const el = toElement(selector) as HTMLElement
export async function click (selector:Element|string):Promise<void> {
const element = toElement(selector)

if (globalThis.HTMLElement && !(el instanceof globalThis.HTMLElement)) {
throw new Error('selector needs to be instance of HTMLElement or resolve to one')
}
event(new globalThis.MouseEvent('click', {
bubbles: true,
cancelable: true,
button: 0
}), element)

el!.click()
await requestAnimationFrame()
}

/**
* Dispatch an event from the given element.
* @param {string|Event} event The event to dispatch
* @param {Element|window} [element] - The element to dispatch from, or
* will use `window` if none given.
* @returns {void}
*
* @param {{
* event: string | Event,
* element?: HTMLElement | Element | typeof window
* }} args
* @throws {Error} Throws an error if the `event` is not a string that can be
* converted to a CustomEvent or not an instance of Event.
*/
export function event (args:{
event:string|Event;
element?:HTMLElement|Element|typeof window
}):void {
let {
event,
element = window
} = args
export function event (
event:string|InstanceType<typeof Event>|InstanceType<typeof CustomEvent<any>>,
element?:Element|Window
):void {
element = (element instanceof Window ? element : toElement(element))

if (typeof event === 'string') {
event = new window.CustomEvent(event)
event = new globalThis.CustomEvent(event)
}

if (typeof event !== 'object') {
if (
!(event instanceof Event) &&
!((event as any) instanceof CustomEvent)
) {
throw new Error('event should be of type Event')
}

Expand Down
9 changes: 8 additions & 1 deletion src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@
* HTMLElement, Element, or Window.
*
*/
export function toElement (_selector:string|HTMLElement|Element) {
export function toElement (
_selector?:string|HTMLElement|Element
):Element|InstanceType<typeof Window> {
if (!_selector) return window

let selector:string|Element|null = _selector

if (globalThis.document) {
if (typeof selector === 'string') {
selector = globalThis.document.querySelector(selector)
Expand All @@ -28,6 +33,8 @@ export function toElement (_selector:string|HTMLElement|Element) {
}

return selector
} else {
return window
}
}

Expand Down
16 changes: 8 additions & 8 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,23 +233,23 @@ test('dom.event', t => {
t.plan(3)

dom.qs('#test-two')!.addEventListener('hello', ev => {
t.ok(ev, 'should get the event')
t.ok(ev, 'should get the custom event')
})

dom.qs('#test-two')!.addEventListener('testing-event', (ev) => {
t.ok(ev, 'should get another event')
t.ok(ev, 'should get another custom event')
t.equal((ev as CustomEvent).detail, 'test',
'has the right event properties')
'Can pass in properties to customize the event')
})

dom.event({ event: 'hello', element: dom.qs('#test-two')! })
dom.event({
event: new CustomEvent('testing-event', {
dom.event('hello', dom.qs('#test-two')!)
dom.event(
new CustomEvent('testing-event', {
bubbles: true,
detail: 'test'
}),
element: dom.qs('#test-two')!
})
dom.qs('#test-two')!
)
})

test('waitForText', async t => {
Expand Down

0 comments on commit 77c3604

Please sign in to comment.