forked from github/hotkey
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
60 lines (52 loc) · 2.05 KB
/
test.js
File metadata and controls
60 lines (52 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/* global hotkey */
let buttonsClicked = []
function buttonClickHandler(event) {
buttonsClicked.push(event.target.id)
}
describe('hotkey', function() {
beforeEach(function() {
document.body.innerHTML = `
<button id="button1" data-hotkey="b">Button 1</button>
<button id="button2">Button 2</button>
<button id="button3" data-hotkey="Control+b">Button 3</button>
<input id="textfield" />
`
for (const button of document.querySelectorAll('button')) {
button.addEventListener('click', buttonClickHandler)
}
for (const button of document.querySelectorAll('[data-hotkey]')) {
hotkey.install(button)
}
})
afterEach(function() {
for (const button of document.querySelectorAll('button')) {
button.removeEventListener('click', buttonClickHandler)
}
for (const button of document.querySelectorAll('[data-hotkey]')) {
hotkey.uninstall(button)
}
document.body.innerHTML = ''
buttonsClicked = []
})
it('triggers buttons that have `data-hotkey` as a attribute', function() {
document.dispatchEvent(new KeyboardEvent('keydown', {key: 'b'}))
assert.include(buttonsClicked, 'button1')
})
it("doesn't trigger buttons that don't have `data-hotkey` as a attribute", function() {
document.dispatchEvent(new KeyboardEvent('keydown', {key: 'b'}))
assert.notInclude(buttonsClicked, 'button2')
})
it("doesn't trigger when user is focused on a form field", function() {
document.getElementById('textfield').dispatchEvent(new KeyboardEvent('keydown', {key: 'b'}))
assert.deepEqual(buttonsClicked, [])
})
it('handles multiple keys in a hotkey combination', function() {
document.dispatchEvent(new KeyboardEvent('keydown', {key: 'b', ctrlKey: true}))
assert.include(buttonsClicked, 'button3')
})
it("doesn't trigger elements where the hotkey library has been uninstalled", function() {
hotkey.uninstall(document.querySelector('#button1'))
document.dispatchEvent(new KeyboardEvent('keydown', {key: 'b'}))
assert.deepEqual(buttonsClicked, [])
})
})