This repository was archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 401
/
Copy pathtabbable.test.js
146 lines (120 loc) · 4.65 KB
/
tabbable.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import React from 'react';
import {shallow, mount} from 'enzyme';
import {makeTabbable, TabbableSelect} from '../../lib/views/tabbable';
import TabGroup from '../../lib/tab-group';
describe('makeTabbable', function() {
let atomEnv, tabGroup;
const fakeEvent = {
stopPropagation() {},
};
beforeEach(function() {
atomEnv = global.buildAtomEnvironment();
tabGroup = new TabGroup();
});
afterEach(function() {
atomEnv.destroy();
});
it('accepts an HTML tag', function() {
const TabbableDiv = makeTabbable('div');
const wrapper = shallow(
<TabbableDiv
tabGroup={tabGroup}
commands={atomEnv.commands}
other="value"
/>,
);
// Rendered element properties
const div = wrapper.find('div');
assert.isUndefined(div.prop('tabGroup'));
assert.isUndefined(div.prop('commands'));
assert.strictEqual(div.prop('tabIndex'), -1);
assert.strictEqual(div.prop('other'), 'value');
// Command registration
const commands = wrapper.find('Commands');
const element = Symbol('element');
commands.prop('target').setter(element);
sinon.stub(tabGroup, 'focusAfter');
commands.find('Command[command="core:focus-next"]').prop('callback')(fakeEvent);
assert.isTrue(tabGroup.focusAfter.called);
sinon.stub(tabGroup, 'focusBefore');
commands.find('Command[command="core:focus-previous"]').prop('callback')(fakeEvent);
assert.isTrue(tabGroup.focusBefore.called);
});
it('accepts a React component', function() {
const TabbableExample = makeTabbable(Example);
const wrapper = shallow(
<TabbableExample
tabGroup={tabGroup}
commands={atomEnv.commands}
other="value"
/>,
);
// Rendered component element properties
const example = wrapper.find(Example);
assert.isUndefined(example.prop('tabGroup'));
assert.isUndefined(example.prop('commands'));
assert.strictEqual(example.prop('other'), 'value');
});
it('adds a ref to the TabGroup', function() {
sinon.stub(tabGroup, 'appendElement');
sinon.stub(tabGroup, 'removeElement');
const TabbableExample = makeTabbable(Example);
const wrapper = mount(<TabbableExample tabGroup={tabGroup} commands={atomEnv.commands} />);
const instance = wrapper.find(Example).instance();
assert.isTrue(tabGroup.appendElement.calledWith(instance, false));
wrapper.unmount();
assert.isTrue(tabGroup.removeElement.calledWith(instance));
});
it('customizes the ref used to register commands', function() {
const TabbableExample = makeTabbable(Example, {rootRefProp: 'arbitraryName'});
const wrapper = shallow(<TabbableExample tabGroup={tabGroup} commands={atomEnv.commands} />);
const rootRef = wrapper.find(Example).prop('arbitraryName');
assert.strictEqual(wrapper.find('Commands').prop('target'), rootRef);
});
it('passes commands to the wrapped component', function() {
const TabbableExample = makeTabbable(Example, {passCommands: true});
const wrapper = shallow(<TabbableExample tabGroup={tabGroup} commands={atomEnv.commands} />);
assert.strictEqual(wrapper.find(Example).prop('commands'), atomEnv.commands);
});
describe('TabbableSelect', function() {
it('proxies keydown events', function() {
const wrapper = mount(<TabbableSelect tabGroup={tabGroup} commands={atomEnv.commands} />);
const div = wrapper.find('div.github-TabbableWrapper').getDOMNode();
const select = wrapper.find('Select').instance();
let lastCode = null;
sinon.stub(select, 'handleKeyDown').callsFake(e => {
lastCode = e.keyCode;
});
const codes = new Map([
['github:selectbox-down', 40],
['github:selectbox-up', 38],
['github:selectbox-enter', 13],
['github:selectbox-tab', 9],
['github:selectbox-backspace', 8],
['github:selectbox-pageup', 33],
['github:selectbox-pagedown', 34],
['github:selectbox-end', 35],
['github:selectbox-home', 36],
['github:selectbox-delete', 46],
['github:selectbox-escape', 27],
]);
for (const [command, code] of codes) {
lastCode = null;
atomEnv.commands.dispatch(div, command);
assert.strictEqual(lastCode, code);
}
});
it('passes focus() to the Select component', function() {
const wrapper = mount(<TabbableSelect tabGroup={tabGroup} commands={atomEnv.commands} />);
const select = wrapper.find('Select').instance();
sinon.stub(select, 'focus');
wrapper.instance().elementRef.get().focus();
assert.isTrue(select.focus.called);
});
});
});
class Example extends React.Component {
render() {
return <div />;
}
}