Repository navigation
Expand file tree
/
Copy pathpane-item.test.js
More file actions
313 lines (258 loc) · 9.42 KB
/
Copy pathpane-item.test.js
File metadata and controls
313 lines (258 loc) · 9.42 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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import React from 'react';
import {shallow, mount} from 'enzyme';
import PropTypes from 'prop-types';
import PaneItem from '../../lib/atom/pane-item';
import StubItem from '../../lib/items/stub-item';
class Component extends React.Component {
static propTypes = {
text: PropTypes.string.isRequired,
didFocus: PropTypes.func,
}
static defaultProps = {
didFocus: () => {},
}
render() {
return (
<div>{this.props.text}</div>
);
}
getTitle() {
return `Component with: ${this.props.text}`;
}
getText() {
return this.props.text;
}
focus() {
return this.props.didFocus();
}
}
describe('PaneItem', function() {
let atomEnv, workspace;
beforeEach(function() {
atomEnv = global.buildAtomEnvironment();
workspace = atomEnv.workspace;
});
afterEach(function() {
atomEnv.destroy();
});
describe('opener', function() {
it('registers an opener on the workspace', function() {
sinon.spy(workspace, 'addOpener');
shallow(
<PaneItem workspace={workspace} uriPattern="atom-github://pattern">
{() => <Component text="a prop" />}
</PaneItem>,
);
assert.isTrue(workspace.addOpener.called);
});
it('disposes the opener on unmount', function() {
const sub = {
dispose: sinon.spy(),
};
sinon.stub(workspace, 'addOpener').returns(sub);
const wrapper = shallow(
<PaneItem workspace={workspace} uriPattern="atom-github://pattern">
{() => <Component text="a prop" />}
</PaneItem>,
);
wrapper.unmount();
assert.isTrue(sub.dispose.called);
});
it('renders no children', function() {
const wrapper = shallow(
<PaneItem workspace={workspace} uriPattern="atom-github://pattern">
{() => <Component text="a prop" />}
</PaneItem>,
);
assert.lengthOf(wrapper.find('Component'), 0);
});
});
describe('when opened with a matching URI', function() {
it('calls its render prop', async function() {
let called = false;
mount(
<PaneItem workspace={workspace} uriPattern="atom-github://pattern">
{() => {
called = true;
return <Component text="a prop" />;
}}
</PaneItem>,
);
assert.isFalse(called);
await workspace.open('atom-github://pattern');
assert.isTrue(called);
});
it('uses the child component as the workspace item', async function() {
mount(
<PaneItem workspace={workspace} uriPattern="atom-github://pattern">
{({itemHolder}) => <Component ref={itemHolder.setter} text="a prop" />}
</PaneItem>,
);
const item = await workspace.open('atom-github://pattern');
assert.strictEqual(item.getTitle(), 'Component with: a prop');
});
it('adds a CSS class to the root element', async function() {
mount(
<PaneItem workspace={workspace} uriPattern="atom-github://pattern" className="root">
{({itemHolder}) => <Component ref={itemHolder.setter} text="a prop" />}
</PaneItem>,
);
const item = await workspace.open('atom-github://pattern');
assert.isTrue(item.getElement().classList.contains('root'));
});
it('renders a child item', async function() {
const wrapper = mount(
<PaneItem workspace={workspace} uriPattern="atom-github://pattern">
{() => <Component text="a prop" />}
</PaneItem>,
);
await workspace.open('atom-github://pattern');
assert.lengthOf(wrapper.update().find('Component'), 1);
});
it('renders a different child item for each matching URI', async function() {
const wrapper = mount(
<PaneItem workspace={workspace} uriPattern="atom-github://pattern/{id}">
{() => <Component text="a prop" />}
</PaneItem>,
);
await workspace.open('atom-github://pattern/1');
await workspace.open('atom-github://pattern/2');
assert.lengthOf(wrapper.update().find('Component'), 2);
});
it('renders a different child item for each item in a different Pane', async function() {
const wrapper = mount(
<PaneItem workspace={workspace} uriPattern="atom-github://pattern/{id}">
{() => <Component text="a prop" />}
</PaneItem>,
);
const item0 = await workspace.open('atom-github://pattern/1');
const pane0 = workspace.paneForItem(item0);
pane0.splitRight({copyActiveItem: true});
assert.lengthOf(wrapper.update().find('Component'), 2);
});
it('passes matched parameters to its render prop', async function() {
let calledWith = null;
mount(
<PaneItem workspace={workspace} uriPattern="atom-github://pattern/{id}">
{({params}) => {
calledWith = params;
return <Component text="a prop" />;
}}
</PaneItem>,
);
assert.isNull(calledWith);
await workspace.open('atom-github://pattern/123');
assert.deepEqual(calledWith, {id: '123'});
calledWith = null;
await workspace.open('atom-github://pattern/456');
assert.deepEqual(calledWith, {id: '456'});
});
it('passes the URI itself', async function() {
let calledWith = null;
mount(
<PaneItem workspace={workspace} uriPattern="atom-github://pattern/{id}">
{({uri}) => {
calledWith = uri;
return <Component text="a prop" />;
}}
</PaneItem>,
);
assert.isNull(calledWith);
await workspace.open('atom-github://pattern/123');
assert.strictEqual(calledWith, 'atom-github://pattern/123');
calledWith = null;
await workspace.open('atom-github://pattern/456');
assert.strictEqual(calledWith, 'atom-github://pattern/456');
});
it('calls focus() on the child item when focus() is called', async function() {
const didFocus = sinon.spy();
mount(
<PaneItem workspace={workspace} uriPattern="atom-github://pattern">
{({itemHolder}) => <Component ref={itemHolder.setter} text="a prop" didFocus={didFocus} />}
</PaneItem>,
);
const item = await workspace.open('atom-github://pattern');
item.getElement().dispatchEvent(new FocusEvent('focus'));
assert.isTrue(didFocus.called);
});
it('removes a child when its pane is destroyed', async function() {
const wrapper = mount(
<PaneItem workspace={workspace} uriPattern="atom-github://pattern/{id}">
{() => <Component text="a prop" />}
</PaneItem>,
);
await workspace.open('atom-github://pattern/0');
const item1 = await workspace.open('atom-github://pattern/1');
assert.lengthOf(wrapper.update().find('Component'), 2);
assert.isTrue(await workspace.paneForItem(item1).destroyItem(item1));
assert.lengthOf(wrapper.update().find('Component'), 1);
});
});
describe('when StubItems are present', function() {
it('renders children into the stubs', function() {
const stub0 = StubItem.create(
'some-component',
{title: 'Component'},
'atom-github://pattern/root/10',
);
workspace.getActivePane().addItem(stub0);
const stub1 = StubItem.create(
'other-component',
{title: 'Other Component'},
'atom-github://other/pattern',
);
workspace.getActivePane().addItem(stub1);
const wrapper = mount(
<PaneItem workspace={workspace} uriPattern="atom-github://pattern/root/{id}">
{({params}) => <Component text={params.id} />}
</PaneItem>,
);
assert.lengthOf(wrapper.find('Component'), 1);
assert.strictEqual(wrapper.find('Component').prop('text'), '10');
});
it('adopts the real item into the stub', function() {
const stub = StubItem.create(
'some-component',
{title: 'Component'},
'atom-github://pattern/root/10',
);
workspace.getActivePane().addItem(stub);
mount(
<PaneItem workspace={workspace} uriPattern="atom-github://pattern/root/{id}">
{({params, itemHolder}) => <Component ref={itemHolder.setter} text={params.id} />}
</PaneItem>,
);
assert.strictEqual(stub.getText(), '10');
});
it('passes additional props from the stub to the real component', function() {
const extra = Symbol('extra');
const stub = StubItem.create(
'some-component',
{title: 'Component', extra},
'atom-github://pattern/root/10',
);
workspace.getActivePane().addItem(stub);
const wrapper = mount(
<PaneItem workspace={workspace} uriPattern="atom-github://pattern/root/{id}">
{({itemHolder, deserialized}) => <Component ref={itemHolder.setter} extra={deserialized.extra} text="nah" />}
</PaneItem>,
);
assert.lengthOf(wrapper.find('Component'), 1);
assert.strictEqual(wrapper.find('Component').prop('extra'), extra);
});
it('adds a CSS class to the stub root', function() {
const stub = StubItem.create(
'some-component',
{title: 'Component'},
'atom-github://pattern/root/10',
);
workspace.getActivePane().addItem(stub);
mount(
<PaneItem workspace={workspace} uriPattern="atom-github://pattern/root/{id}" className="added">
{({params, itemHolder}) => <Component ref={itemHolder.setter} text={params.id} />}
</PaneItem>,
);
assert.isTrue(stub.getElement().classList.contains('added'));
});
});
});