-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-diff-spec.js
More file actions
313 lines (272 loc) · 9.98 KB
/
Copy pathgit-diff-spec.js
File metadata and controls
313 lines (272 loc) · 9.98 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
const path = require('path');
const fs = require('fs-plus');
const temp = require('temp').track();
describe('GitDiff package', () => {
let editor, editorElement, projectPath, screenUpdates;
beforeEach(() => {
screenUpdates = 0;
spyOn(window, 'requestAnimationFrame').andCallFake(fn => {
fn();
screenUpdates++;
});
spyOn(window, 'cancelAnimationFrame').andCallFake(i => null);
projectPath = temp.mkdirSync('git-diff-spec-');
const otherPath = temp.mkdirSync('some-other-path-');
fs.copySync(path.join(__dirname, 'fixtures', 'working-dir'), projectPath);
fs.moveSync(
path.join(projectPath, 'git.git'),
path.join(projectPath, '.git')
);
atom.project.setPaths([otherPath, projectPath]);
jasmine.attachToDOM(atom.workspace.getElement());
waitsForPromise(async () => {
await atom.workspace.open(path.join(projectPath, 'sample.js'));
await atom.packages.activatePackage('git-diff');
});
runs(() => {
editor = atom.workspace.getActiveTextEditor();
editorElement = atom.views.getView(editor);
});
});
afterEach(() => {
temp.cleanup();
});
describe('when the editor has no changes', () => {
it("doesn't mark the editor", () => {
waitsFor(() => screenUpdates > 0);
runs(() => expect(editor.getMarkers().length).toBe(0));
});
});
describe('when the editor has modified lines', () => {
it('highlights the modified lines', () => {
expect(editorElement.querySelectorAll('.git-line-modified').length).toBe(
0
);
editor.insertText('a');
advanceClock(editor.getBuffer().stoppedChangingDelay);
waitsFor(() => editor.getMarkers().length > 0);
runs(() => {
expect(
editorElement.querySelectorAll('.git-line-modified').length
).toBe(1);
expect(editorElement.querySelector('.git-line-modified')).toHaveData(
'buffer-row',
0
);
});
});
});
describe('when the editor has added lines', () => {
it('highlights the added lines', () => {
expect(editorElement.querySelectorAll('.git-line-added').length).toBe(0);
editor.moveToEndOfLine();
editor.insertNewline();
editor.insertText('a');
advanceClock(editor.getBuffer().stoppedChangingDelay);
waitsFor(() => editor.getMarkers().length > 0);
runs(() => {
expect(editorElement.querySelectorAll('.git-line-added').length).toBe(
1
);
expect(editorElement.querySelector('.git-line-added')).toHaveData(
'buffer-row',
1
);
});
});
});
describe('when the editor has removed lines', () => {
it('highlights the line preceeding the deleted lines', () => {
expect(editorElement.querySelectorAll('.git-line-added').length).toBe(0);
editor.setCursorBufferPosition([5]);
editor.deleteLine();
advanceClock(editor.getBuffer().stoppedChangingDelay);
waitsFor(() => editor.getMarkers().length > 0);
runs(() => {
expect(editorElement.querySelectorAll('.git-line-removed').length).toBe(
1
);
expect(editorElement.querySelector('.git-line-removed')).toHaveData(
'buffer-row',
4
);
});
});
});
describe('when the editor has removed the first line', () => {
it('highlights the line preceeding the deleted lines', () => {
expect(editorElement.querySelectorAll('.git-line-added').length).toBe(0);
editor.setCursorBufferPosition([0, 0]);
editor.deleteLine();
advanceClock(editor.getBuffer().stoppedChangingDelay);
waitsFor(() => editor.getMarkers().length > 0);
runs(() => {
expect(
editorElement.querySelectorAll('.git-previous-line-removed').length
).toBe(1);
expect(
editorElement.querySelector('.git-previous-line-removed')
).toHaveData('buffer-row', 0);
});
});
});
describe('when a modified line is restored to the HEAD version contents', () => {
it('removes the diff highlight', () => {
expect(editorElement.querySelectorAll('.git-line-modified').length).toBe(
0
);
editor.insertText('a');
advanceClock(editor.getBuffer().stoppedChangingDelay);
waitsFor(
() => editorElement.querySelectorAll('.git-line-modified').length > 0
);
runs(() => {
expect(
editorElement.querySelectorAll('.git-line-modified').length
).toBe(1);
editor.backspace();
advanceClock(editor.getBuffer().stoppedChangingDelay);
});
waitsFor(
() => editorElement.querySelectorAll('.git-line-modified').length < 1
);
runs(() => {
expect(
editorElement.querySelectorAll('.git-line-modified').length
).toBe(0);
});
});
});
describe('when a modified file is opened', () => {
it('highlights the changed lines', () => {
fs.writeFileSync(
path.join(projectPath, 'sample.txt'),
'Some different text.'
);
waitsForPromise(() =>
atom.workspace.open(path.join(projectPath, 'sample.txt'))
);
runs(() => {
editor = atom.workspace.getActiveTextEditor();
editorElement = editor.getElement();
});
waitsFor(() => editor.getMarkers().length > 0);
runs(() => {
expect(
editorElement.querySelectorAll('.git-line-modified').length
).toBe(1);
expect(editorElement.querySelector('.git-line-modified')).toHaveData(
'buffer-row',
0
);
});
});
});
describe('when the project paths change', () => {
it("doesn't try to use the destroyed git repository", () => {
editor.deleteLine();
atom.project.setPaths([temp.mkdirSync('no-repository')]);
advanceClock(editor.getBuffer().stoppedChangingDelay);
waitsFor(() => editor.getMarkers().length === 0);
runs(() => {
expect(editor.getMarkers().length).toBe(0);
});
});
});
describe('move-to-next-diff/move-to-previous-diff events', () => {
it('moves the cursor to first character of the next/previous diff line', () => {
editor.insertText('a');
waitsFor(() => editor.getMarkers().length > 0);
runs(() => {
editor.setCursorBufferPosition([5]);
editor.deleteLine();
advanceClock(editor.getBuffer().stoppedChangingDelay);
editor.setCursorBufferPosition([0]);
atom.commands.dispatch(editorElement, 'git-diff:move-to-next-diff');
expect(editor.getCursorBufferPosition()).toEqual([4, 4]);
atom.commands.dispatch(editorElement, 'git-diff:move-to-previous-diff');
expect(editor.getCursorBufferPosition()).toEqual([0, 0]);
});
});
it('wraps around to the first/last diff in the file', () => {
editor.insertText('a');
waitsFor(() => editor.getMarkers().length > 0);
runs(() => {
editor.setCursorBufferPosition([5]);
editor.deleteLine();
advanceClock(editor.getBuffer().stoppedChangingDelay);
editor.setCursorBufferPosition([0]);
atom.commands.dispatch(editorElement, 'git-diff:move-to-next-diff');
expect(editor.getCursorBufferPosition().toArray()).toEqual([4, 4]);
atom.commands.dispatch(editorElement, 'git-diff:move-to-next-diff');
expect(editor.getCursorBufferPosition().toArray()).toEqual([0, 0]);
atom.commands.dispatch(editorElement, 'git-diff:move-to-previous-diff');
expect(editor.getCursorBufferPosition().toArray()).toEqual([4, 4]);
});
});
describe('when the wrapAroundOnMoveToDiff config option is false', () => {
beforeEach(() =>
atom.config.set('git-diff.wrapAroundOnMoveToDiff', false)
);
it('does not wraps around to the first/last diff in the file', () => {
editor.insertText('a');
editor.setCursorBufferPosition([5]);
editor.deleteLine();
advanceClock(editor.getBuffer().stoppedChangingDelay);
waitsFor(() => editor.getMarkers().length > 0);
runs(() => {
editor.setCursorBufferPosition([0]);
atom.commands.dispatch(editorElement, 'git-diff:move-to-next-diff');
expect(editor.getCursorBufferPosition()).toEqual([4, 4]);
atom.commands.dispatch(editorElement, 'git-diff:move-to-next-diff');
expect(editor.getCursorBufferPosition()).toEqual([4, 4]);
atom.commands.dispatch(
editorElement,
'git-diff:move-to-previous-diff'
);
expect(editor.getCursorBufferPosition()).toEqual([0, 0]);
atom.commands.dispatch(
editorElement,
'git-diff:move-to-previous-diff'
);
expect(editor.getCursorBufferPosition()).toEqual([0, 0]);
});
});
});
});
describe('when the showIconsInEditorGutter config option is true', () => {
beforeEach(() => {
atom.config.set('git-diff.showIconsInEditorGutter', true);
});
it('the gutter has a git-diff-icon class', () => {
waitsFor(() => screenUpdates > 0);
runs(() => {
expect(editorElement.querySelector('.gutter')).toHaveClass(
'git-diff-icon'
);
});
});
it('keeps the git-diff-icon class when editor.showLineNumbers is toggled', () => {
waitsFor(() => screenUpdates > 0);
runs(() => {
atom.config.set('editor.showLineNumbers', false);
expect(editorElement.querySelector('.gutter')).not.toHaveClass(
'git-diff-icon'
);
atom.config.set('editor.showLineNumbers', true);
expect(editorElement.querySelector('.gutter')).toHaveClass(
'git-diff-icon'
);
});
});
it('removes the git-diff-icon class when the showIconsInEditorGutter config option set to false', () => {
waitsFor(() => screenUpdates > 0);
runs(() => {
atom.config.set('git-diff.showIconsInEditorGutter', false);
expect(editorElement.querySelector('.gutter')).not.toHaveClass(
'git-diff-icon'
);
});
});
});
});