-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
79 lines (54 loc) · 1.42 KB
/
test.js
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import test from 'ava';
import React from 'react';
import ReactDOM from 'react-dom';
import shortkey from './index';
let node;
test.beforeEach(() => {
node = document.createElement('div');
document.body.appendChild(node);
});
test.afterEach(() => {
document.body.removeChild(node);
node = null;
});
test.serial('DOM addEventListener()', t => {
let key;
const handler = shortkey({
onEscape: e => (key = e.key),
});
const event = new KeyboardEvent('keydown', {
key: 'Escape',
});
node.addEventListener('keydown', handler);
node.dispatchEvent(event);
t.true(key === 'Escape');
});
test.serial('DOM removeEventListener()', t => {
let key = 0;
const handler = shortkey({
onEscape: e => ++key,
});
const event = new KeyboardEvent('keydown', {
key: 'Escape',
});
node.addEventListener('keydown', handler);
node.dispatchEvent(event);
t.true(key === 1);
node.removeEventListener('keydown', handler);
node.dispatchEvent(event);
t.true(key === 1);
});
test.serial('React and ReactDOM', t => {
let key;
const handler = shortkey({
onEscape: e => (key = e.key),
});
const Component = () => <input onKeyDown={handler} />;
ReactDOM.render(<Component />, node);
const event = new KeyboardEvent('keydown', {
key: 'Escape',
bubbles: true /* get react's event delegation to work */,
});
node.firstChild.dispatchEvent(event);
t.true(key === 'Escape');
});