Repository navigation
Expand file tree
/
Copy pathdecoration.test.js
More file actions
340 lines (277 loc) · 11.3 KB
/
Copy pathdecoration.test.js
File metadata and controls
340 lines (277 loc) · 11.3 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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import React from 'react';
import {mount} from 'enzyme';
import {Range} from 'atom';
import path from 'path';
import Decoration from '../../lib/atom/decoration';
import AtomTextEditor from '../../lib/atom/atom-text-editor';
import Marker from '../../lib/atom/marker';
import MarkerLayer from '../../lib/atom/marker-layer';
import ErrorBoundary from '../../lib/error-boundary';
describe('Decoration', function() {
let atomEnv, workspace, editor, marker;
beforeEach(async function() {
atomEnv = global.buildAtomEnvironment();
workspace = atomEnv.workspace;
editor = await workspace.open(__filename);
marker = editor.markBufferRange([[2, 0], [6, 0]]);
});
afterEach(function() {
atomEnv.destroy();
});
it('decorates its marker on render', function() {
const app = (
<Decoration
editor={editor}
decorable={marker}
type="line"
position="head"
className="something"
/>
);
mount(app);
assert.lengthOf(editor.getLineDecorations({position: 'head', class: 'something'}), 1);
});
describe('with a subtree', function() {
beforeEach(function() {
sinon.spy(editor, 'decorateMarker');
});
it('creates a block decoration', function() {
const app = (
<Decoration editor={editor} decorable={marker} type="block" className="parent">
<div className="decoration-subtree">
This is a subtree
</div>
</Decoration>
);
mount(app);
const args = editor.decorateMarker.firstCall.args;
assert.equal(args[0], marker);
assert.equal(args[1].type, 'block');
const element = args[1].item.getElement();
assert.strictEqual(element.className, 'react-atom-decoration parent');
const child = element.firstElementChild;
assert.equal(child.className, 'decoration-subtree');
assert.equal(child.textContent, 'This is a subtree');
});
it('creates an overlay decoration', function() {
const app = (
<Decoration editor={editor} decorable={marker} type="overlay">
<div className="decoration-subtree">
This is a subtree
</div>
</Decoration>
);
mount(app);
const args = editor.decorateMarker.firstCall.args;
assert.equal(args[0], marker);
assert.equal(args[1].type, 'overlay');
const child = args[1].item.getElement().firstElementChild;
assert.equal(child.className, 'decoration-subtree');
assert.equal(child.textContent, 'This is a subtree');
});
it('decorates a gutter after it has been created in the editor', function() {
const gutterName = 'rubin-starset-memorial-gutter';
const app = (
<Decoration editor={editor} decorable={marker} type="gutter" gutterName={gutterName}>
<div className="decoration-subtree">
This is a subtree
</div>
</Decoration>
);
mount(app);
// the gutter has not been created yet at this point
assert.isNull(editor.gutterWithName(gutterName));
assert.isFalse(editor.decorateMarker.called);
editor.addGutter({name: gutterName});
assert.isTrue(editor.decorateMarker.called);
const args = editor.decorateMarker.firstCall.args;
assert.equal(args[0], marker);
assert.equal(args[1].type, 'gutter');
assert.equal(args[1].gutterName, 'rubin-starset-memorial-gutter');
const child = args[1].item.getElement().firstElementChild;
assert.equal(child.className, 'decoration-subtree');
assert.equal(child.textContent, 'This is a subtree');
});
it('does not decorate a non-existent gutter', function() {
const gutterName = 'rubin-starset-memorial-gutter';
const app = (
<Decoration editor={editor} decorable={marker} type="gutter" gutterName={gutterName}>
<div className="decoration-subtree">
This is a subtree
</div>
</Decoration>
);
mount(app);
assert.isNull(editor.gutterWithName(gutterName));
assert.isFalse(editor.decorateMarker.called);
editor.addGutter({name: 'another-gutter-name'});
assert.isFalse(editor.decorateMarker.called);
assert.isNull(editor.gutterWithName(gutterName));
});
describe('throws an error', function() {
let errors;
// This consumes the error rather than printing it to console.
const onError = function(e) {
if (e.message === 'Uncaught Error: You are trying to decorate a gutter but did not supply gutterName prop.') {
errors.push(e.error);
e.preventDefault();
}
};
beforeEach(function() {
errors = [];
window.addEventListener('error', onError);
});
afterEach(function() {
errors = [];
window.removeEventListener('error', onError);
});
it('if `gutterName` prop is not supplied for gutter decorations', function() {
const app = (
<ErrorBoundary>
<Decoration editor={editor} decorable={marker} type="gutter">
<div className="decoration-subtree">
This is a subtree
</div>
</Decoration>
</ErrorBoundary>
);
mount(app);
assert.strictEqual(errors[0].message, 'You are trying to decorate a gutter but did not supply gutterName prop.');
});
});
});
describe('when props update', function() {
it('creates a new decoration on a different TextEditor and marker', async function() {
const wrapper = mount(<Decoration editor={editor} decorable={marker} type="line" className="pretty" />);
assert.lengthOf(editor.getLineDecorations({class: 'pretty'}), 1);
const [original] = editor.getLineDecorations({class: 'pretty'});
const newEditor = await workspace.open(path.join(__dirname, 'marker.test.js'));
const newMarker = newEditor.markBufferRange([[0, 0], [1, 0]]);
wrapper.setProps({editor: newEditor, decorable: newMarker});
assert.isTrue(original.isDestroyed());
assert.lengthOf(editor.getLineDecorations({class: 'pretty'}), 0);
assert.lengthOf(newEditor.getLineDecorations({class: 'pretty'}), 1);
const [newDecoration] = newEditor.getLineDecorations({class: 'pretty'});
assert.strictEqual(newDecoration.getMarker(), newMarker);
});
it('creates a new decoration on a different Marker', function() {
const wrapper = mount(<Decoration editor={editor} decorable={marker} type="line" className="pretty" />);
assert.lengthOf(editor.getLineDecorations({class: 'pretty'}), 1);
const [original] = editor.getLineDecorations({class: 'pretty'});
const newMarker = editor.markBufferRange([[1, 0], [3, 0]]);
wrapper.setProps({decorable: newMarker});
assert.isTrue(original.isDestroyed());
assert.lengthOf(editor.getLineDecorations({class: 'pretty'}), 1);
const [newDecoration] = editor.getLineDecorations({class: 'pretty'});
assert.strictEqual(newDecoration.getMarker(), newMarker);
});
it('destroys and re-creates its decoration', function() {
const wrapper = mount(<Decoration editor={editor} decorable={marker} type="line" className="pretty" />);
assert.lengthOf(editor.getLineDecorations({class: 'pretty'}), 1);
const [original] = editor.getLineDecorations({class: 'pretty'});
wrapper.setProps({type: 'line-number', className: 'prettier'});
assert.isTrue(original.isDestroyed());
assert.lengthOf(editor.getLineNumberDecorations({class: 'prettier'}), 1);
});
it('does not create a decoration when the Marker is not on the TextEditor', async function() {
const wrapper = mount(<Decoration editor={editor} decorable={marker} type="line" className="pretty" />);
assert.lengthOf(editor.getLineDecorations({class: 'pretty'}), 1);
const [original] = editor.getLineDecorations({class: 'pretty'});
const newEditor = await workspace.open(path.join(__dirname, 'marker.test.js'));
wrapper.setProps({editor: newEditor});
assert.isTrue(original.isDestroyed());
assert.lengthOf(editor.getLineDecorations({class: 'pretty'}), 0);
assert.lengthOf(newEditor.getLineDecorations({class: 'pretty'}), 0);
});
});
it('destroys its decoration on unmount', function() {
const app = (
<Decoration
editor={editor}
decorable={marker}
type="line"
className="whatever"
/>
);
const wrapper = mount(app);
assert.lengthOf(editor.getLineDecorations({class: 'whatever'}), 1);
wrapper.unmount();
assert.lengthOf(editor.getLineDecorations({class: 'whatever'}), 0);
});
it('decorates a parent Marker on a parent TextEditor', function() {
const wrapper = mount(
<AtomTextEditor workspace={workspace}>
<Marker bufferRange={Range.fromObject([[0, 0], [0, 0]])}>
<Decoration type="line" className="whatever" position="head" />
</Marker>
</AtomTextEditor>,
);
const theEditor = wrapper.instance().getModel();
assert.lengthOf(theEditor.getLineDecorations({position: 'head', class: 'whatever'}), 1);
});
it('decorates a parent MarkerLayer on a parent TextEditor', function() {
let layerID = null;
const wrapper = mount(
<AtomTextEditor workspace={workspace}>
<MarkerLayer handleID={id => { layerID = id; }}>
<Marker bufferRange={Range.fromObject([[0, 0], [0, 0]])} />
<Decoration type="line" className="something" />
</MarkerLayer>
</AtomTextEditor>,
);
const theEditor = wrapper.instance().getModel();
const theLayer = theEditor.getMarkerLayer(layerID);
const layerMap = theEditor.decorationManager.layerDecorationsByMarkerLayer;
const decorationSet = layerMap.get(theLayer);
assert.strictEqual(decorationSet.size, 1);
});
it('decorates a parent Marker on a prop-provided TextEditor', function() {
mount(
<Marker editor={editor} bufferRange={Range.fromObject([[0, 0], [1, 0]])}>
<Decoration editor={editor} type="line" className="something" />
</Marker>,
);
assert.lengthOf(editor.getLineDecorations({class: 'something'}), 1);
});
it('decorates a parent MarkerLayer on a prop-provided TextEditor', function() {
let layerID = null;
mount(
<MarkerLayer editor={editor} handleID={id => { layerID = id; }}>
<Marker bufferRange={Range.fromObject([[0, 0], [1, 0]])} />
<Decoration editor={editor} type="line" className="something" />
</MarkerLayer>,
);
const theLayer = editor.getMarkerLayer(layerID);
const layerMap = editor.decorationManager.layerDecorationsByMarkerLayer;
const decorationSet = layerMap.get(theLayer);
assert.strictEqual(decorationSet.size, 1);
});
it('does not attempt to decorate a destroyed Marker', function() {
marker.destroy();
const app = (
<Decoration
editor={editor}
decorable={marker}
type="line"
position="head"
className="something"
/>
);
mount(app);
assert.lengthOf(editor.getLineDecorations(), 0);
});
it('does not attempt to decorate a destroyed TextEditor', function() {
editor.destroy();
const app = (
<Decoration
editor={editor}
decorable={marker}
type="line"
position="head"
className="something"
/>
);
mount(app);
assert.lengthOf(editor.getLineDecorations(), 0);
});
});