This repository was archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 402
/
Copy pathopen-commit-dialog.test.js
150 lines (121 loc) · 4.63 KB
/
open-commit-dialog.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
import React from 'react';
import {shallow} from 'enzyme';
import OpenCommitDialog, {openCommitDetailItem} from '../../lib/views/open-commit-dialog';
import {dialogRequests} from '../../lib/controllers/dialogs-controller';
import CommitDetailItem from '../../lib/items/commit-detail-item';
import {GitError} from '../../lib/git-shell-out-strategy';
import {TabbableTextEditor} from '../../lib/views/tabbable';
import * as reporterProxy from '../../lib/reporter-proxy';
describe('OpenCommitDialog', function() {
let atomEnv;
beforeEach(function() {
atomEnv = global.buildAtomEnvironment();
});
afterEach(function() {
atomEnv.destroy();
});
function isValidRef(ref) {
return Promise.resolve(/^abcd/.test(ref));
}
function buildApp(overrides = {}) {
const request = dialogRequests.commit();
return (
<OpenCommitDialog
request={request}
inProgress={false}
isValidRef={isValidRef}
workspace={atomEnv.workspace}
commands={atomEnv.commands}
{...overrides}
/>
);
}
describe('open button enablement', function() {
it('disables the open button with no commit ref', function() {
const wrapper = shallow(buildApp());
assert.isFalse(wrapper.find('DialogView').prop('acceptEnabled'));
});
it('enables the open button when commit sha box is populated', function() {
const wrapper = shallow(buildApp());
wrapper.find(TabbableTextEditor).prop('buffer').setText('abcd1234');
assert.isTrue(wrapper.find('DialogView').prop('acceptEnabled'));
wrapper.find(TabbableTextEditor).prop('buffer').setText('abcd6789');
assert.isTrue(wrapper.find('DialogView').prop('acceptEnabled'));
});
});
it('calls the acceptance callback with the entered ref', function() {
const accept = sinon.spy();
const request = dialogRequests.commit();
request.onAccept(accept);
const wrapper = shallow(buildApp({request}));
wrapper.find(TabbableTextEditor).prop('buffer').setText('abcd1234');
wrapper.find('DialogView').prop('accept')();
assert.isTrue(accept.calledWith('abcd1234'));
wrapper.unmount();
});
it('does nothing on accept if the ref is empty', async function() {
const accept = sinon.spy();
const request = dialogRequests.commit();
request.onAccept(accept);
const wrapper = shallow(buildApp({request}));
await wrapper.find('DialogView').prop('accept')();
assert.isFalse(accept.called);
});
it('calls the cancellation callback', function() {
const cancel = sinon.spy();
const request = dialogRequests.commit();
request.onCancel(cancel);
const wrapper = shallow(buildApp({request}));
wrapper.find('DialogView').prop('cancel')();
assert.isTrue(cancel.called);
});
describe('openCommitDetailItem()', function() {
let repository;
beforeEach(function() {
sinon.stub(atomEnv.workspace, 'open').resolves('item');
sinon.stub(reporterProxy, 'addEvent');
repository = {
getWorkingDirectoryPath() {
return __dirname;
},
getCommit(ref) {
if (ref === 'abcd1234') {
return Promise.resolve('ok');
}
if (ref === 'bad') {
const e = new GitError('bad ref');
e.code = 128;
return Promise.reject(e);
}
return Promise.reject(new GitError('other error'));
},
};
});
it('opens a CommitDetailItem with the chosen valid ref and records an event', async function() {
assert.strictEqual(await openCommitDetailItem('abcd1234', {workspace: atomEnv.workspace, repository}), 'item');
assert.isTrue(atomEnv.workspace.open.calledWith(
CommitDetailItem.buildURI(__dirname, 'abcd1234'),
{searchAllPanes: true},
));
assert.isTrue(reporterProxy.addEvent.calledWith(
'open-commit-in-pane',
{package: 'github', from: OpenCommitDialog.name},
));
});
it('raises a friendly error if the ref is invalid', async function() {
const e = await openCommitDetailItem('bad', {workspace: atomEnv.workspace, repository}).then(
() => { throw new Error('unexpected success'); },
error => error,
);
assert.strictEqual(e.userMessage, 'There is no commit associated with that reference.');
});
it('passes other errors through directly', async function() {
const e = await openCommitDetailItem('nope', {workspace: atomEnv.workspace, repository}).then(
() => { throw new Error('unexpected success'); },
error => error,
);
assert.isUndefined(e.userMessage);
assert.strictEqual(e.message, 'other error');
});
});
});