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
260 lines (207 loc) · 10.2 KB
/
Copy pathcommit-view.test.js
File metadata and controls
260 lines (207 loc) · 10.2 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
import React from 'react';
import {shallow, mount} from 'enzyme';
import {cloneRepository, buildRepository} from '../helpers';
import Commit, {nullCommit} from '../../lib/models/commit';
import Branch, {nullBranch} from '../../lib/models/branch';
import CommitView from '../../lib/views/commit-view';
describe('CommitView', function() {
let atomEnv, commandRegistry, tooltips, config, lastCommit;
let app;
beforeEach(function() {
atomEnv = global.buildAtomEnvironment();
commandRegistry = atomEnv.commands;
tooltips = atomEnv.tooltips;
config = atomEnv.config;
lastCommit = new Commit({sha: '1234abcd', message: 'commit message'});
const noop = () => {};
const returnTruthyPromise = () => Promise.resolve(true);
app = (
<CommitView
commandRegistry={commandRegistry}
tooltips={tooltips}
config={config}
lastCommit={lastCommit}
currentBranch={nullBranch}
isMerging={false}
stagedChangesExist={false}
mergeConflictsExist={false}
isCommitting={false}
deactivateCommitBox={false}
maximumCharacterLimit={72}
message=""
prepareToCommit={returnTruthyPromise}
commit={noop}
abortMerge={noop}
onChangeMessage={noop}
toggleExpandedCommitMessageEditor={noop}
/>
);
});
afterEach(function() {
atomEnv.destroy();
});
describe('when the repo is loading', function() {
beforeEach(function() {
app = React.cloneElement(app, {lastCommit: nullCommit});
});
it('disables the commit button', function() {
app = React.cloneElement(app, {message: 'even with text'});
const wrapper = shallow(app);
assert.isTrue(wrapper.find('.github-CommitView-commit').prop('disabled'));
});
});
it('displays the remaining characters limit based on which line is being edited', function() {
const wrapper = mount(app);
assert.strictEqual(wrapper.find('.github-CommitView-remaining-characters').text(), '72');
wrapper.setProps({message: 'abcde fghij'});
assert.strictEqual(wrapper.find('.github-CommitView-remaining-characters').text(), '61');
assert.isFalse(wrapper.find('.github-CommitView-remaining-characters').hasClass('is-error'));
assert.isFalse(wrapper.find('.github-CommitView-remaining-characters').hasClass('is-warning'));
wrapper.setProps({message: '\nklmno'});
assert.strictEqual(wrapper.find('.github-CommitView-remaining-characters').text(), '∞');
assert.isFalse(wrapper.find('.github-CommitView-remaining-characters').hasClass('is-error'));
assert.isFalse(wrapper.find('.github-CommitView-remaining-characters').hasClass('is-warning'));
wrapper.setProps({message: 'abcde\npqrst'});
assert.strictEqual(wrapper.find('.github-CommitView-remaining-characters').text(), '∞');
assert.isFalse(wrapper.find('.github-CommitView-remaining-characters').hasClass('is-error'));
assert.isFalse(wrapper.find('.github-CommitView-remaining-characters').hasClass('is-warning'));
wrapper.find('atom-text-editor').getDOMNode().getModel().setCursorBufferPosition([0, 3]);
wrapper.update();
assert.strictEqual(wrapper.find('.github-CommitView-remaining-characters').text(), '67');
assert.isFalse(wrapper.find('.github-CommitView-remaining-characters').hasClass('is-error'));
assert.isFalse(wrapper.find('.github-CommitView-remaining-characters').hasClass('is-warning'));
wrapper.setProps({stagedChangesExist: true, maximumCharacterLimit: 50});
assert.strictEqual(wrapper.find('.github-CommitView-remaining-characters').text(), '45');
assert.isFalse(wrapper.find('.github-CommitView-remaining-characters').hasClass('is-error'));
assert.isFalse(wrapper.find('.github-CommitView-remaining-characters').hasClass('is-warning'));
wrapper.setProps({message: 'a'.repeat(41)}).update();
assert.strictEqual(wrapper.find('.github-CommitView-remaining-characters').text(), '9');
assert.isFalse(wrapper.find('.github-CommitView-remaining-characters').hasClass('is-error'));
assert.isTrue(wrapper.find('.github-CommitView-remaining-characters').hasClass('is-warning'));
wrapper.setProps({message: 'a'.repeat(58)}).update();
assert.strictEqual(wrapper.find('.github-CommitView-remaining-characters').text(), '-8');
assert.isTrue(wrapper.find('.github-CommitView-remaining-characters').hasClass('is-error'));
assert.isFalse(wrapper.find('.github-CommitView-remaining-characters').hasClass('is-warning'));
});
describe('the commit button', function() {
let wrapper;
beforeEach(async function() {
const workdirPath = await cloneRepository('three-files');
const repository = await buildRepository(workdirPath);
app = React.cloneElement(app, {
repository,
stagedChangesExist: true,
mergeConflictsExist: false,
message: 'something',
});
wrapper = mount(app);
});
it('is disabled when no changes are staged', function() {
wrapper.setProps({stagedChangesExist: false});
assert.isTrue(wrapper.find('.github-CommitView-commit').prop('disabled'));
wrapper.setProps({stagedChangesExist: true});
assert.isFalse(wrapper.find('.github-CommitView-commit').prop('disabled'));
});
it('is disabled when there are merge conflicts', function() {
wrapper.setProps({mergeConflictsExist: true});
assert.isTrue(wrapper.find('.github-CommitView-commit').prop('disabled'));
wrapper.setProps({mergeConflictsExist: false});
assert.isFalse(wrapper.find('.github-CommitView-commit').prop('disabled'));
});
it('is disabled when the commit message is empty', function() {
wrapper.setProps({message: ''}).update();
assert.isTrue(wrapper.find('.github-CommitView-commit').prop('disabled'));
wrapper.setProps({message: 'Not empty'}).update();
assert.isFalse(wrapper.find('.github-CommitView-commit').prop('disabled'));
});
it('displays the current branch name', function() {
const currentBranch = new Branch('aw-do-the-stuff');
wrapper.setProps({currentBranch});
assert.strictEqual(wrapper.find('.github-CommitView-commit').text(), 'Commit to aw-do-the-stuff');
});
it('indicates when a commit will be detached', function() {
const currentBranch = Branch.createDetached('master~3');
wrapper.setProps({currentBranch});
assert.strictEqual(wrapper.find('.github-CommitView-commit').text(), 'Create detached commit');
});
it('displays a progress message while committing', function() {
wrapper.setState({showWorking: true});
assert.strictEqual(wrapper.find('.github-CommitView-commit').text(), 'Working...');
});
it('falls back to "commit" with no current branch', function() {
assert.strictEqual(wrapper.find('.github-CommitView-commit').text(), 'Commit');
});
});
describe('committing', function() {
let commit, prepareToCommitResolution;
let wrapper, editorElement, editor, commitButton, workspaceElement;
beforeEach(function() {
const prepareToCommit = () => Promise.resolve(prepareToCommitResolution);
commit = sinon.spy();
app = React.cloneElement(app, {stagedChangesExist: true, prepareToCommit, commit, message: 'Something'});
wrapper = mount(app);
editorElement = wrapper.find('atom-text-editor').getDOMNode();
sinon.spy(editorElement, 'focus');
editor = editorElement.getModel();
// Perform an extra render to ensure the editor text is reflected in the commit button enablement.
// The controller accomplishes this by re-rendering on Repository update.
wrapper.setProps({});
commitButton = wrapper.find('.github-CommitView-commit');
workspaceElement = atomEnv.views.getView(atomEnv.workspace);
});
describe('when props.prepareToCommit() resolves true', function() {
beforeEach(function() {
prepareToCommitResolution = true;
});
it('calls props.commit(message) when the commit button is clicked', async function() {
wrapper.update();
commitButton.simulate('click');
await assert.async.isTrue(commit.calledWith('Something'));
// undo history is cleared
commandRegistry.dispatch(editorElement, 'core:undo');
assert.equal(editor.getText(), '');
});
it('calls props.commit(message) when github:commit is dispatched', async function() {
commandRegistry.dispatch(workspaceElement, 'github:commit');
await assert.async.isTrue(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.simulate('click');
await assert.async.isTrue(editorElement.focus.called);
assert.isFalse(commit.called);
});
it('takes no further action when github:commit is dispatched', async function() {
commandRegistry.dispatch(workspaceElement, 'github:commit');
await assert.async.isTrue(editorElement.focus.called);
assert.isFalse(commit.called);
});
});
});
it('shows the "Abort Merge" button when props.isMerging is true', function() {
app = React.cloneElement(app, {isMerging: true});
const wrapper = shallow(app);
assert.isTrue(wrapper.find('.github-CommitView-abortMerge').exists());
wrapper.setProps({isMerging: false});
assert.isFalse(wrapper.find('.github-CommitView-abortMerge').exists());
});
it('calls props.abortMerge() when the "Abort Merge" button is clicked', function() {
const abortMerge = sinon.stub().resolves();
app = React.cloneElement(app, {abortMerge, stagedChangesExist: true, isMerging: true});
const wrapper = shallow(app);
wrapper.find('.github-CommitView-abortMerge').simulate('click');
assert.isTrue(abortMerge.calledOnce);
});
describe('restoring focus', function() {
it('to the commit button', function() {
const wrapper = mount(app);
sinon.spy(wrapper.instance().refCommitButton.get(), 'focus');
wrapper.instance().setFocus(CommitView.focus.COMMIT_BUTTON);
assert.isTrue(wrapper.instance().refCommitButton.get().focus.called);
});
});
});