forked from atom/github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommit-detail-view.test.js
292 lines (245 loc) · 10.6 KB
/
commit-detail-view.test.js
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
import React from 'react';
import {shallow, mount} from 'enzyme';
import moment from 'moment';
import dedent from 'dedent-js';
import CommitDetailView from '../../lib/views/commit-detail-view';
import CommitDetailItem from '../../lib/items/commit-detail-item';
import Commit from '../../lib/models/commit';
import Remote, {nullRemote} from '../../lib/models/remote';
import {cloneRepository, buildRepository} from '../helpers';
import {commitBuilder} from '../builder/commit';
describe('CommitDetailView', function() {
let repository, atomEnv;
beforeEach(async function() {
atomEnv = global.buildAtomEnvironment();
repository = await buildRepository(await cloneRepository('multiple-commits'));
});
afterEach(function() {
atomEnv.destroy();
});
function buildApp(override = {}) {
const props = {
repository,
commit: commitBuilder().setMultiFileDiff().build(),
currentRemote: new Remote('origin', '[email protected]:atom/github'),
messageCollapsible: false,
messageOpen: true,
isCommitPushed: true,
itemType: CommitDetailItem,
workspace: atomEnv.workspace,
commands: atomEnv.commands,
keymaps: atomEnv.keymaps,
tooltips: atomEnv.tooltips,
config: atomEnv.config,
destroy: () => { },
toggleMessage: () => { },
surfaceCommit: () => { },
...override,
};
return <CommitDetailView {...props} />;
}
it('has a MultiFilePatchController that its itemType set', function() {
const wrapper = shallow(buildApp({itemType: CommitDetailItem}));
assert.strictEqual(wrapper.find('MultiFilePatchController').prop('itemType'), CommitDetailItem);
});
it('passes unrecognized props to a MultiFilePatchController', function() {
const extra = Symbol('extra');
const wrapper = shallow(buildApp({extra}));
assert.strictEqual(wrapper.find('MultiFilePatchController').prop('extra'), extra);
});
it('renders commit details properly', function() {
const commit = commitBuilder()
.sha('420')
.addAuthor('[email protected]', 'Forthe Win')
.authorDate(moment().subtract(2, 'days').unix())
.messageSubject('subject')
.messageBody('body')
.setMultiFileDiff()
.build();
const wrapper = shallow(buildApp({commit}));
assert.strictEqual(wrapper.find('.github-CommitDetailView-title').text(), 'subject');
assert.strictEqual(wrapper.find('.github-CommitDetailView-moreText').text(), 'body');
assert.strictEqual(wrapper.find('.github-CommitDetailView-metaText').text(), 'Forthe Win committed 2 days ago');
assert.strictEqual(wrapper.find('.github-CommitDetailView-sha').text(), '420');
assert.strictEqual(wrapper.find('.github-CommitDetailView-sha a').prop('href'), 'https://github.com/atom/github/commit/420');
assert.strictEqual(
wrapper.find('img.github-RecentCommit-avatar').prop('src'),
'https://avatars.githubusercontent.com/u/e?email=very%40nice.com&s=32',
);
});
it('renders multiple avatars for co-authored commit', function() {
const commit = commitBuilder()
.addAuthor('[email protected]', 'blaze')
.addCoAuthor('[email protected]', 'two')
.addCoAuthor('[email protected]', 'three')
.build();
const wrapper = shallow(buildApp({commit}));
assert.deepEqual(
wrapper.find('img.github-RecentCommit-avatar').map(w => w.prop('src')),
[
'https://avatars.githubusercontent.com/u/e?email=blaze%40it.com&s=32',
'https://avatars.githubusercontent.com/u/e?email=two%40coauthor.com&s=32',
'https://avatars.githubusercontent.com/u/e?email=three%40coauthor.com&s=32',
],
);
});
it('handles noreply email addresses', function() {
const commit = commitBuilder()
.addAuthor('[email protected]', 'noreply')
.build();
const wrapper = shallow(buildApp({commit}));
assert.strictEqual(
wrapper.find('img.github-CommitDetailView-avatar').prop('src'),
'https://avatars.githubusercontent.com/u/1234?s=32',
);
});
describe('dotcom link rendering', function() {
it('renders a link to GitHub', function() {
const wrapper = shallow(buildApp({
commit: commitBuilder().sha('0123').build(),
currentRemote: new Remote('dotcom', '[email protected]:atom/github'),
isCommitPushed: true,
}));
const link = wrapper.find('a');
assert.strictEqual(link.text(), '0123');
assert.strictEqual(link.prop('href'), 'https://github.com/atom/github/commit/0123');
});
it('omits the link if there is no current remote', function() {
const wrapper = shallow(buildApp({
commit: commitBuilder().sha('0123').build(),
currentRemote: nullRemote,
isCommitPushed: true,
}));
assert.isFalse(wrapper.find('a').exists());
assert.include(wrapper.find('span').map(w => w.text()), '0123');
});
it('omits the link if the current remote is not a GitHub remote', function() {
const wrapper = shallow(buildApp({
commit: commitBuilder().sha('0123').build(),
currentRemote: new Remote('elsewhere', '[email protected]:atom/github'),
isCommitPushed: true,
}));
assert.isFalse(wrapper.find('a').exists());
assert.include(wrapper.find('span').map(w => w.text()), '0123');
});
it('omits the link if the commit is not pushed', function() {
const wrapper = shallow(buildApp({
commit: commitBuilder().sha('0123').build(),
currentRemote: new Remote('dotcom', '[email protected]:atom/github'),
isCommitPushed: false,
}));
assert.isFalse(wrapper.find('a').exists());
assert.include(wrapper.find('span').map(w => w.text()), '0123');
});
});
describe('getAuthorInfo', function() {
describe('when there are no co-authors', function() {
it('returns only the author', function() {
const commit = commitBuilder()
.addAuthor('[email protected]', 'Steven Universe')
.build();
const wrapper = shallow(buildApp({commit}));
assert.strictEqual(wrapper.instance().getAuthorInfo(), 'Steven Universe');
});
});
describe('when there is one co-author', function() {
it('returns author and the co-author', function() {
const commit = commitBuilder()
.addAuthor('[email protected]', 'Ruby')
.addCoAuthor('[email protected]', 'Sapphire')
.build();
const wrapper = shallow(buildApp({commit}));
assert.strictEqual(wrapper.instance().getAuthorInfo(), 'Ruby and Sapphire');
});
});
describe('when there is more than one co-author', function() {
it('returns the author and number of co-authors', function() {
const commit = commitBuilder()
.addAuthor('[email protected]', 'Amethyst')
.addCoAuthor('[email protected]', 'Peridot')
.addCoAuthor('[email protected]', 'Pearl')
.build();
const wrapper = shallow(buildApp({commit}));
assert.strictEqual(wrapper.instance().getAuthorInfo(), 'Amethyst and 2 others');
});
});
});
describe('commit message collapsibility', function() {
let wrapper, shortMessage, longMessage;
beforeEach(function() {
shortMessage = dedent`
if every pork chop was perfect...
we wouldn't have hot dogs!
🌭🌭🌭🌭🌭🌭🌭
`;
longMessage = 'this message is really really really\n';
while (longMessage.length < Commit.LONG_MESSAGE_THRESHOLD) {
longMessage += 'really really really really really really\n';
}
longMessage += 'really really long.';
});
describe('when messageCollapsible is false', function() {
beforeEach(function() {
const commit = commitBuilder().messageBody(shortMessage).build();
wrapper = shallow(buildApp({commit, messageCollapsible: false}));
});
it('renders the full message body', function() {
assert.strictEqual(wrapper.find('.github-CommitDetailView-moreText').text(), shortMessage);
});
it('does not render a button', function() {
assert.isFalse(wrapper.find('.github-CommitDetailView-moreButton').exists());
});
});
describe('when messageCollapsible is true and messageOpen is false', function() {
beforeEach(function() {
const commit = commitBuilder().messageBody(longMessage).build();
wrapper = shallow(buildApp({commit, messageCollapsible: true, messageOpen: false}));
});
it('renders an abbreviated commit message', function() {
const messageText = wrapper.find('.github-CommitDetailView-moreText').text();
assert.notStrictEqual(messageText, longMessage);
assert.isAtMost(messageText.length, Commit.LONG_MESSAGE_THRESHOLD);
});
it('renders a button to reveal the rest of the message', function() {
const button = wrapper.find('.github-CommitDetailView-moreButton');
assert.lengthOf(button, 1);
assert.strictEqual(button.text(), 'Show More');
});
});
describe('when messageCollapsible is true and messageOpen is true', function() {
let toggleMessage;
beforeEach(function() {
toggleMessage = sinon.spy();
const commit = commitBuilder().messageBody(longMessage).build();
wrapper = shallow(buildApp({commit, messageCollapsible: true, messageOpen: true, toggleMessage}));
});
it('renders the full message', function() {
assert.strictEqual(wrapper.find('.github-CommitDetailView-moreText').text(), longMessage);
});
it('renders a button to collapse the message text', function() {
const button = wrapper.find('.github-CommitDetailView-moreButton');
assert.lengthOf(button, 1);
assert.strictEqual(button.text(), 'Show Less');
});
it('the button calls toggleMessage when clicked', function() {
const button = wrapper.find('.github-CommitDetailView-moreButton');
button.simulate('click');
assert.isTrue(toggleMessage.called);
});
});
});
describe('keyboard bindings', function() {
it('surfaces the recent commit on github:surface', function() {
const surfaceCommit = sinon.spy();
const wrapper = mount(buildApp({surfaceCommit}));
atomEnv.commands.dispatch(wrapper.getDOMNode(), 'github:surface');
assert.isTrue(surfaceCommit.called);
});
it('surfaces from the embedded MultiFilePatchView', function() {
const surfaceCommit = sinon.spy();
const wrapper = mount(buildApp({surfaceCommit}));
atomEnv.commands.dispatch(wrapper.find('.github-FilePatchView').getDOMNode(), 'github:surface');
assert.isTrue(surfaceCommit.called);
});
});
});