forked from atom/github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-dialog-controller.test.js
284 lines (220 loc) · 11.4 KB
/
create-dialog-controller.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
import React from 'react';
import {shallow} from 'enzyme';
import path from 'path';
import {BareCreateDialogController} from '../../lib/controllers/create-dialog-controller';
import CreateDialogView from '../../lib/views/create-dialog-view';
import {dialogRequests} from '../../lib/controllers/dialogs-controller';
import {userBuilder} from '../builder/graphql/user';
import userQuery from '../../lib/controllers/__generated__/createDialogController_user.graphql';
describe('CreateDialogController', function() {
let atomEnv;
beforeEach(function() {
atomEnv = global.buildAtomEnvironment();
atomEnv.config.set('core.projectHome', path.join('/home/me/src'));
atomEnv.config.set('github.sourceRemoteName', 'origin');
atomEnv.config.set('github.remoteFetchProtocol', 'https');
});
afterEach(function() {
atomEnv.destroy();
});
function buildApp(override = {}) {
return (
<BareCreateDialogController
user={userBuilder(userQuery).build()}
request={dialogRequests.create()}
isLoading={false}
inProgress={false}
currentWindow={atomEnv.getCurrentWindow()}
workspace={atomEnv.workspace}
commands={atomEnv.commands}
config={atomEnv.config}
{...override}
/>
);
}
it('synchronizes the source remote name from Atom configuration', function() {
const wrapper = shallow(buildApp());
const buffer = wrapper.find(CreateDialogView).prop('sourceRemoteName');
assert.strictEqual(buffer.getText(), 'origin');
atomEnv.config.set('github.sourceRemoteName', 'upstream');
assert.strictEqual(buffer.getText(), 'upstream');
buffer.setText('home');
assert.strictEqual(atomEnv.config.get('github.sourceRemoteName'), 'home');
sinon.spy(atomEnv.config, 'set');
buffer.setText('home');
assert.isFalse(atomEnv.config.set.called);
wrapper.unmount();
});
it('synchronizes the source protocol from Atom configuration', async function() {
const wrapper = shallow(buildApp());
assert.strictEqual(wrapper.find(CreateDialogView).prop('selectedProtocol'), 'https');
atomEnv.config.set('github.remoteFetchProtocol', 'ssh');
assert.strictEqual(wrapper.find(CreateDialogView).prop('selectedProtocol'), 'ssh');
await wrapper.find(CreateDialogView).prop('didChangeProtocol')('https');
assert.strictEqual(atomEnv.config.get('github.remoteFetchProtocol'), 'https');
sinon.spy(atomEnv.config, 'set');
await wrapper.find(CreateDialogView).prop('didChangeProtocol')('https');
assert.isFalse(atomEnv.config.set.called);
});
it('begins with an empty owner ID while loading', function() {
const wrapper = shallow(buildApp({user: null, isLoading: true}));
assert.strictEqual(wrapper.find(CreateDialogView).prop('selectedOwnerID'), '');
});
it('begins with the owner ID as the viewer ID', function() {
const user = userBuilder(userQuery)
.id('user0')
.build();
const wrapper = shallow(buildApp({user}));
assert.strictEqual(wrapper.find(CreateDialogView).prop('selectedOwnerID'), 'user0');
});
describe('initial repository name', function() {
it('is empty if the initial local path is unspecified', function() {
const request = dialogRequests.create();
const wrapper = shallow(buildApp({request}));
assert.isTrue(wrapper.find(CreateDialogView).prop('repoName').isEmpty());
});
it('is the base name of the initial local path', function() {
const request = dialogRequests.publish({localDir: path.join('/local/directory')});
const wrapper = shallow(buildApp({request}));
assert.strictEqual(wrapper.find(CreateDialogView).prop('repoName').getText(), 'directory');
});
});
describe('initial local path', function() {
it('is the project home directory if unspecified', function() {
const request = dialogRequests.create();
const wrapper = shallow(buildApp({request}));
assert.strictEqual(wrapper.find(CreateDialogView).prop('localPath').getText(), path.join('/home/me/src'));
});
it('is the provided path from the dialog request', function() {
const request = dialogRequests.publish({localDir: path.join('/local/directory')});
const wrapper = shallow(buildApp({request}));
assert.strictEqual(wrapper.find(CreateDialogView).prop('localPath').getText(), path.join('/local/directory'));
});
});
describe('repository name and local path name feedback', function() {
it('matches the repository name to the local path basename when the local path is modified and the repository name is not', function() {
const wrapper = shallow(buildApp());
assert.isTrue(wrapper.find(CreateDialogView).prop('repoName').isEmpty());
wrapper.find(CreateDialogView).prop('localPath').setText(path.join('/local/directory'));
assert.strictEqual(wrapper.find(CreateDialogView).prop('repoName').getText(), 'directory');
});
it('leaves the repository name unchanged if it has been modified', function() {
const wrapper = shallow(buildApp());
wrapper.find(CreateDialogView).prop('repoName').setText('repo-name');
wrapper.find(CreateDialogView).prop('localPath').setText(path.join('/local/directory'));
assert.strictEqual(wrapper.find(CreateDialogView).prop('repoName').getText(), 'repo-name');
});
it('matches the local path basename to the repository name when the repository name is modified and the local path is not', function() {
const wrapper = shallow(buildApp());
assert.strictEqual(wrapper.find(CreateDialogView).prop('localPath').getText(), path.join('/home/me/src'));
wrapper.find(CreateDialogView).prop('repoName').setText('the-repo');
assert.strictEqual(wrapper.find(CreateDialogView).prop('localPath').getText(), path.join('/home/me/src/the-repo'));
wrapper.find(CreateDialogView).prop('repoName').setText('different-name');
assert.strictEqual(wrapper.find(CreateDialogView).prop('localPath').getText(), path.join('/home/me/src/different-name'));
});
it('leaves the local path unchanged if it has been modified', function() {
const wrapper = shallow(buildApp());
wrapper.find(CreateDialogView).prop('localPath').setText(path.join('/some/local/directory'));
wrapper.find(CreateDialogView).prop('repoName').setText('the-repo');
assert.strictEqual(wrapper.find(CreateDialogView).prop('localPath').getText(), path.join('/some/local/directory'));
});
});
describe('accept enablement', function() {
it('enabled the accept button when all data is present and non-empty', function() {
const wrapper = shallow(buildApp());
wrapper.find(CreateDialogView).prop('repoName').setText('the-repo');
wrapper.find(CreateDialogView).prop('localPath').setText(path.join('/local/path'));
assert.isTrue(wrapper.find(CreateDialogView).prop('acceptEnabled'));
});
it('disables the accept button if the repo name is empty', function() {
const wrapper = shallow(buildApp());
wrapper.find(CreateDialogView).prop('repoName').setText('zzz');
wrapper.find(CreateDialogView).prop('repoName').setText('');
wrapper.find(CreateDialogView).prop('localPath').setText(path.join('/local/path'));
assert.isFalse(wrapper.find(CreateDialogView).prop('acceptEnabled'));
});
it('disables the accept button if the local path is empty', function() {
const wrapper = shallow(buildApp());
wrapper.find(CreateDialogView).prop('repoName').setText('the-repo');
wrapper.find(CreateDialogView).prop('localPath').setText('');
assert.isFalse(wrapper.find(CreateDialogView).prop('acceptEnabled'));
});
it('disables the accept button if the source remote name is empty', function() {
const wrapper = shallow(buildApp());
wrapper.find(CreateDialogView).prop('sourceRemoteName').setText('');
assert.isFalse(wrapper.find(CreateDialogView).prop('acceptEnabled'));
});
it('disables the accept button if user data has not loaded yet', function() {
const wrapper = shallow(buildApp({user: null}));
assert.isFalse(wrapper.find(CreateDialogView).prop('acceptEnabled'));
});
it('enables the accept button when user data loads', function() {
const wrapper = shallow(buildApp({user: null}));
wrapper.find(CreateDialogView).prop('repoName').setText('the-repo');
wrapper.find(CreateDialogView).prop('localPath').setText(path.join('/local/path'));
assert.isFalse(wrapper.find(CreateDialogView).prop('acceptEnabled'));
wrapper.setProps({user: userBuilder(userQuery).build()});
assert.isTrue(wrapper.find(CreateDialogView).prop('acceptEnabled'));
wrapper.setProps({});
assert.isTrue(wrapper.find(CreateDialogView).prop('acceptEnabled'));
});
});
describe('acceptance', function() {
it('does nothing if insufficient data is available', async function() {
const accept = sinon.spy();
const request = dialogRequests.create();
request.onAccept(accept);
const wrapper = shallow(buildApp({request}));
wrapper.find(CreateDialogView).prop('repoName').setText('');
await wrapper.find(CreateDialogView).prop('accept')();
assert.isFalse(accept.called);
});
it('uses the user ID if the selected owner ID was never changed', async function() {
const accept = sinon.spy();
const request = dialogRequests.create();
request.onAccept(accept);
const wrapper = shallow(buildApp({request, user: null, isLoading: true}));
assert.strictEqual(wrapper.find(CreateDialogView).prop('selectedOwnerID'), '');
wrapper.setProps({
user: userBuilder(userQuery).id('my-id').build(),
isLoading: false,
});
wrapper.find(CreateDialogView).prop('repoName').setText('repo-name');
wrapper.find(CreateDialogView).prop('didChangeVisibility')('PRIVATE');
wrapper.find(CreateDialogView).prop('localPath').setText(path.join('/local/path'));
wrapper.find(CreateDialogView).prop('didChangeProtocol')('ssh');
wrapper.find(CreateDialogView).prop('sourceRemoteName').setText('upstream');
assert.strictEqual(wrapper.find(CreateDialogView).prop('selectedOwnerID'), '');
await wrapper.find(CreateDialogView).prop('accept')();
assert.isTrue(accept.calledWith({
ownerID: 'my-id',
name: 'repo-name',
visibility: 'PRIVATE',
localPath: path.join('/local/path'),
protocol: 'ssh',
sourceRemoteName: 'upstream',
}));
});
it('resolves onAccept with the populated data', async function() {
const accept = sinon.spy();
const request = dialogRequests.create();
request.onAccept(accept);
const wrapper = shallow(buildApp({request}));
wrapper.find(CreateDialogView).prop('didChangeOwnerID')('org-id');
wrapper.find(CreateDialogView).prop('repoName').setText('repo-name');
wrapper.find(CreateDialogView).prop('didChangeVisibility')('PRIVATE');
wrapper.find(CreateDialogView).prop('localPath').setText(path.join('/local/path'));
wrapper.find(CreateDialogView).prop('didChangeProtocol')('ssh');
wrapper.find(CreateDialogView).prop('sourceRemoteName').setText('upstream');
await wrapper.find(CreateDialogView).prop('accept')();
assert.isTrue(accept.calledWith({
ownerID: 'org-id',
name: 'repo-name',
visibility: 'PRIVATE',
localPath: path.join('/local/path'),
protocol: 'ssh',
sourceRemoteName: 'upstream',
}));
});
});
});