Skip to content
This repository was archived by the owner on Sep 22, 2022. It is now read-only.

Commit 63eb86a

Browse files
committed
Construct events in tests better
1 parent c3924a4 commit 63eb86a

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

test/test.js

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -81,19 +81,19 @@ describe('details-dialog-element', function() {
8181
dialog.toggle(true)
8282
await waitForToggleEvent(details)
8383
assert(details.open)
84-
pressEscape(details)
84+
triggerKeydownEvent(details, 'Escape')
8585
assert(!details.open)
8686
})
8787

8888
it('manages focus', async function() {
8989
summary.click()
9090
await waitForToggleEvent(details)
9191
assert.equal(document.activeElement, dialog)
92-
pressTab(details, true)
92+
triggerKeydownEvent(details, 'Tab', true)
9393
assert.equal(document.activeElement, document.querySelector(`[${CLOSE_ATTR}]`))
94-
pressTab(details)
94+
triggerKeydownEvent(details, 'Tab')
9595
assert.equal(document.activeElement, document.querySelector(`[data-button]`))
96-
pressTab(details)
96+
triggerKeydownEvent(details, 'Tab')
9797
assert.equal(document.activeElement, document.querySelector(`[${CLOSE_ATTR}]`))
9898
})
9999

@@ -124,7 +124,7 @@ describe('details-dialog-element', function() {
124124
assert(details.open)
125125
assert.equal(closeRequestCount, 2)
126126

127-
pressEscape(details)
127+
triggerKeydownEvent(details, 'Escape')
128128
assert(details.open)
129129
assert.equal(closeRequestCount, 3)
130130

@@ -165,7 +165,7 @@ describe('details-dialog-element', function() {
165165
assert(details.open)
166166
assert.equal(closeRequestCount, 1)
167167

168-
pressEscape(details)
168+
triggerKeydownEvent(details, 'Escape')
169169
assert(details.open)
170170
assert.equal(closeRequestCount, 2)
171171

@@ -199,7 +199,7 @@ describe('details-dialog-element', function() {
199199
dialog.toggle(true)
200200
await waitForToggleEvent(details)
201201
assert(details.open)
202-
pressEscape(details)
202+
triggerKeydownEvent(details, 'Escape')
203203
assert(!details.open)
204204
})
205205
})
@@ -232,7 +232,7 @@ describe('details-dialog-element', function() {
232232
dialog.preload = true
233233
assert(dialog.hasAttribute('preload'))
234234
assert.notOk(includeFragment.getAttribute('src'))
235-
triggerEvent(details, 'mouseover')
235+
triggerMouseoverEvent(details)
236236
assert.equal(includeFragment.getAttribute('src'), '/404')
237237
})
238238
})
@@ -262,7 +262,7 @@ describe('details-dialog-element', function() {
262262
it('transfers src on mouseover', async function() {
263263
assert(!details.open)
264264
assert.notOk(includeFragment.getAttribute('src'))
265-
triggerEvent(details, 'mouseover')
265+
triggerMouseoverEvent(details)
266266
assert.equal(includeFragment.getAttribute('src'), '/404')
267267
})
268268
})
@@ -281,22 +281,21 @@ function waitForToggleEvent(details) {
281281
})
282282
}
283283

284-
function triggerEvent(element, name, key, shiftKey) {
285-
const eventklass = name.match(/^mouse/) ? MouseEvent : KeyboardEvent
284+
function triggerMouseoverEvent(element) {
286285
element.dispatchEvent(
287-
new eventklass(name, {
286+
new MouseEvent('mouseover', {
287+
bubbles: true,
288+
cancelable: true
289+
})
290+
)
291+
}
292+
function triggerKeydownEvent(element, key, shiftKey) {
293+
element.dispatchEvent(
294+
new KeyboardEvent('keydown', {
288295
bubbles: true,
289296
cancelable: true,
290297
key,
291298
shiftKey
292299
})
293300
)
294301
}
295-
296-
function pressEscape(details) {
297-
triggerEvent(details, 'keydown', 'Escape')
298-
}
299-
300-
function pressTab(details, shift) {
301-
triggerEvent(details, 'keydown', 'Tab', shift)
302-
}

0 commit comments

Comments
 (0)