forked from atom/github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommit-view.test.js
More file actions
218 lines (171 loc) · 7.95 KB
/
Copy pathcommit-view.test.js
File metadata and controls
218 lines (171 loc) · 7.95 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
import {cloneRepository, buildRepository} from '../helpers';
import etch from 'etch';
import until from 'test-until';
import CommitView from '../../lib/views/commit-view';
describe('CommitView', function() {
let atomEnv, commandRegistry, lastCommit;
beforeEach(function() {
atomEnv = global.buildAtomEnvironment();
commandRegistry = atomEnv.commands;
lastCommit = {sha: '1234abcd', message: 'commit message', unbornRef: false};
});
afterEach(function() {
atomEnv.destroy();
});
it('displays the remaining characters limit based on which line is being edited', async function() {
const view = new CommitView({
commandRegistry, lastCommit,
stagedChangesExist: true, maximumCharacterLimit: 72, message: '',
});
assert.equal(view.refs.remainingCharacters.textContent, '72');
await view.update({message: 'abcde fghij'});
assert.equal(view.refs.remainingCharacters.textContent, '61');
assert(!view.refs.remainingCharacters.classList.contains('is-error'));
assert(!view.refs.remainingCharacters.classList.contains('is-warning'));
await view.update({message: '\nklmno'});
assert.equal(view.refs.remainingCharacters.textContent, '∞');
assert(!view.refs.remainingCharacters.classList.contains('is-error'));
assert(!view.refs.remainingCharacters.classList.contains('is-warning'));
await view.update({message: 'abcde\npqrst'});
assert.equal(view.refs.remainingCharacters.textContent, '∞');
assert(!view.refs.remainingCharacters.classList.contains('is-error'));
assert(!view.refs.remainingCharacters.classList.contains('is-warning'));
view.editor.setCursorBufferPosition([0, 3]);
await etch.getScheduler().getNextUpdatePromise();
assert.equal(view.refs.remainingCharacters.textContent, '67');
assert(!view.refs.remainingCharacters.classList.contains('is-error'));
assert(!view.refs.remainingCharacters.classList.contains('is-warning'));
await view.update({stagedChangesExist: true, maximumCharacterLimit: 50});
assert.equal(view.refs.remainingCharacters.textContent, '45');
assert(!view.refs.remainingCharacters.classList.contains('is-error'));
assert(!view.refs.remainingCharacters.classList.contains('is-warning'));
await view.update({message: 'a'.repeat(41)});
assert.equal(view.refs.remainingCharacters.textContent, '9');
assert(!view.refs.remainingCharacters.classList.contains('is-error'));
assert(view.refs.remainingCharacters.classList.contains('is-warning'));
await view.update({message: 'a'.repeat(58)});
assert.equal(view.refs.remainingCharacters.textContent, '-8');
assert(view.refs.remainingCharacters.classList.contains('is-error'));
assert(!view.refs.remainingCharacters.classList.contains('is-warning'));
});
describe('the commit button', function() {
let view, editor, commitButton;
beforeEach(async function() {
const workdirPath = await cloneRepository('three-files');
const repository = await buildRepository(workdirPath);
const viewState = {};
view = new CommitView({
repository, commandRegistry, lastCommit,
stagedChangesExist: true, mergeConflictsExist: false, viewState,
});
editor = view.refs.editor;
commitButton = view.refs.commitButton;
editor.setText('something');
await etch.getScheduler().getNextUpdatePromise();
});
it('is disabled when no changes are staged', async function() {
await view.update({stagedChangesExist: false});
assert.isTrue(commitButton.disabled);
await view.update({stagedChangesExist: true});
assert.isFalse(commitButton.disabled);
});
it('is disabled when there are merge conflicts', async function() {
await view.update({mergeConflictsExist: false});
assert.isFalse(commitButton.disabled);
await view.update({mergeConflictsExist: true});
assert.isTrue(commitButton.disabled);
});
it('is disabled when the commit message is empty', async function() {
editor.setText('');
await etch.getScheduler().getNextUpdatePromise();
assert.isTrue(commitButton.disabled);
editor.setText('Not empty');
await etch.getScheduler().getNextUpdatePromise();
assert.isFalse(commitButton.disabled);
});
});
describe('committing', function() {
let view, commit, prepareToCommitResolution;
let editor, commitButton, workspaceElement;
beforeEach(async function() {
const prepareToCommit = () => Promise.resolve(prepareToCommitResolution);
commit = sinon.spy();
view = new CommitView({
commandRegistry, lastCommit,
stagedChangesExist: true, prepareToCommit, commit, message: 'Something',
});
sinon.spy(view, 'focus');
editor = view.refs.editor;
commitButton = view.refs.commitButton;
workspaceElement = atomEnv.views.getView(atomEnv.workspace);
await view.update();
});
describe('when props.prepareToCommit() resolves true', function() {
beforeEach(function() { prepareToCommitResolution = true; });
it('calls props.commit(message) when the commit button is clicked', async function() {
commitButton.dispatchEvent(new MouseEvent('click'));
await until('props.commit() is called', () => commit.calledWith('Something'));
// undo history is cleared
commandRegistry.dispatch(editor.element, 'core:undo');
assert.equal(editor.getText(), '');
});
it('calls props.commit(message) when github:commit is dispatched', async function() {
commandRegistry.dispatch(workspaceElement, 'github:commit');
await until('props.commit() is called', () => commit.calledWith('Something'));
});
});
describe('when props.prepareToCommit() resolves false', function() {
beforeEach(function() { prepareToCommitResolution = false; });
it('takes no further action when the commit button is clicked', async function() {
commitButton.dispatchEvent(new MouseEvent('click'));
await until('focus() is called', () => view.focus.called);
assert.isFalse(commit.called);
});
it('takes no further action when github:commit is dispatched', async function() {
commandRegistry.dispatch(workspaceElement, 'github:commit');
await until('focus() is called', () => view.focus.called);
assert.isFalse(commit.called);
});
});
});
it('shows the "Abort Merge" button when props.isMerging is true', async function() {
const view = new CommitView({commandRegistry, lastCommit, stagedChangesExist: true, isMerging: false});
assert.isUndefined(view.refs.abortMergeButton);
await view.update({isMerging: true});
assert.isDefined(view.refs.abortMergeButton);
await view.update({isMerging: false});
assert.isUndefined(view.refs.abortMergeButton);
});
it('calls props.abortMerge() when the "Abort Merge" button is clicked', function() {
const abortMerge = sinon.spy(() => Promise.resolve());
const view = new CommitView({
commandRegistry, lastCommit,
stagedChangesExist: true, isMerging: true, abortMerge,
});
const {abortMergeButton} = view.refs;
abortMergeButton.dispatchEvent(new MouseEvent('click'));
assert(abortMerge.calledOnce);
});
describe('amending', function() {
it('calls props.setAmending() when the box is checked or unchecked', function() {
const setAmending = sinon.spy();
const view = new CommitView({
commandRegistry,
stagedChangesExist: false, lastCommit: {message: 'previous commit\'s message'}, setAmending,
});
const {amend} = view.refs;
amend.click();
assert.deepEqual(setAmending.args, [[true]]);
amend.click();
assert.deepEqual(setAmending.args, [[true], [false]]);
});
it('is hidden when HEAD is an unborn ref', function() {
const view = new CommitView({
commandRegistry,
stagedChangesExist: false,
lastCommit: {unbornRef: true},
});
assert.isUndefined(view.refs.amend);
});
});
});