-
Notifications
You must be signed in to change notification settings - Fork 482
/
importTest.js
423 lines (390 loc) · 14.8 KB
/
importTest.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/* eslint no-unused-vars: "error" */
import sinon from 'sinon';
import {expect} from '../../util/configuredChai';
import {allowConsoleErrors} from '../../util/testUtils';
import designMode from '@cdo/apps/applab/designMode';
import * as elementUtils from '@cdo/apps/applab/designElements/elementUtils';
import {assets as assetsApi} from '@cdo/apps/clientApi';
import {
getImportableProject,
importScreensAndAssets
} from '@cdo/apps/applab/import';
describe('The applab/import module', () => {
allowConsoleErrors();
var designModeViz;
beforeEach(() => {
designModeViz = document.createElement('div');
designModeViz.id = 'designModeViz';
document.body.appendChild(designModeViz);
sinon.stub(designMode, 'changeScreen');
sinon.stub(designMode, 'resetPropertyTab');
sinon.stub(assetsApi, 'copyAssets');
});
afterEach(() => {
designModeViz.parentNode.removeChild(designModeViz);
designMode.changeScreen.restore();
designMode.resetPropertyTab.restore();
assetsApi.copyAssets.restore();
});
function getProjectWithHTML(html) {
return {
channel: {
name: 'Some Other Project!',
id: 'some-other-project'
},
sources: {
html: `<div>${html}</div>`
},
assets: [],
existingAssets: []
};
}
function setExistingHTML(existingHTML) {
designModeViz.innerHTML = existingHTML;
}
describe('The getImportableProject function', () => {
describe('When doing an import into an empty project, the importable project', () => {
var importable;
beforeEach(() => {
setExistingHTML(``);
importable = getImportableProject(
getProjectWithHTML(`
<div class="screen" id="screen1"></div>
<div class="screen" id="screen2"></div>
`)
);
});
it('should have an id and name property pulled from the channel', () => {
expect(importable.id).to.equal('some-other-project');
expect(importable.name).to.equal('Some Other Project!');
});
it('should contain both of the screens in the given html', () => {
expect(importable.screens).to.have.length(2);
expect(importable.screens[0].id).to.equal('screen1');
expect(importable.screens[1].id).to.equal('screen2');
});
it('should list all of the screens as importable', () => {
importable.screens.forEach(
screen => expect(screen.canBeImported).to.be.true
);
});
it('should list all of the screens as not replacing existing screens', () => {
importable.screens.forEach(
screen => expect(screen.willReplace).to.be.false
);
});
it('should list all of the screens as having no assets to replace', () => {
importable.screens.forEach(screen =>
expect(screen.assetsToReplace).to.deep.equal([])
);
});
it('should list all of the screens as having no assets to import', () => {
importable.screens.forEach(screen =>
expect(screen.assetsToImport).to.deep.equal([])
);
});
it('should list all of the screens as having no conflicting element ids', () => {
importable.screens.forEach(screen =>
expect(screen.assetsToReplace).to.deep.equal([])
);
});
it('should have an empty otherAssets', () => {
expect(importable.otherAssets).to.be.empty;
});
});
describe('When doing an import into a project with conflicting screens, the importable project', () => {
var importable;
beforeEach(() => {
setExistingHTML(`
<div class="screen" id="design_screen1"></div>
`);
importable = getImportableProject(
getProjectWithHTML(`
<div class="screen" id="screen1"></div>
<div class="screen" id="screen2"></div>
`)
);
});
it('should mark the conflicting screen id with willReplace=True', () => {
expect(importable.screens[0].willReplace).to.be.true;
expect(importable.screens[1].willReplace).to.be.false;
});
});
describe('When doing an import into a project with conflicting assets, the importable project', () => {
var importable;
beforeEach(() => {
setExistingHTML(`
<div class="screen" id="design_screen1">
<img src="/v3/assets/some-project/asset1.png"
data-canonical-image-url="asset1.png"
id="design_img1">
</div>
`);
importable = getImportableProject({
channel: {
name: 'Some Other Project!',
id: 'some-other-project'
},
sources: {
html: `
<div>
<div
class="screen"
id="screen1"
data-canonical-image-url="background-asset.png"
style="background-image: url("/v2/assets/some-other-project/background-asset.png")">
<img src="/v3/assets/some-other-project/asset1.png"
data-canonical-image-url="asset1.png"
id="img2">
<img src="/v3/assets/some-other-project/asset2.png"
data-canonical-image-url="asset2.png"
id="img3">
</div>
<div class="screen" id="screen2"></div>
</div>`
},
assets: [
{filename: 'asset1.png', category: 'image'},
{filename: 'asset2.png', category: 'image'},
{filename: 'asset3.png', category: 'image'},
{filename: 'asset4.png', category: 'image'},
{filename: 'background-asset.png', category: 'image'}
],
existingAssets: [
{filename: 'asset1.png', category: 'image'},
{filename: 'asset3.png', category: 'image'}
]
});
});
it('should list the assets to replace', () => {
expect(importable.screens[0].assetsToReplace).to.deep.equal([
'asset1.png'
]);
});
it('should list the assets to import without replacing', () => {
expect(importable.screens[0].assetsToImport).to.deep.equal([
'asset2.png',
'background-asset.png'
]);
});
it('should list the other assets not used in the screens', () => {
expect(importable.otherAssets).to.have.length(2);
expect(importable.otherAssets[0].filename).to.equal('asset3.png');
expect(importable.otherAssets[0].category).to.equal('image');
expect(importable.otherAssets[0].willReplace).to.be.true;
expect(importable.otherAssets[1].filename).to.equal('asset4.png');
expect(importable.otherAssets[1].category).to.equal('image');
expect(importable.otherAssets[1].willReplace).to.be.false;
});
});
describe('When doing an import into a project that uses the same element IDs, the importable project', () => {
var importable;
beforeEach(() => {
setExistingHTML(`
<div class="screen" id="design_screen1">
<input id="design_input1">
<input id="design_input2">
</div>
`);
importable = getImportableProject(
getProjectWithHTML(`
<div class="screen" id="screen1">
<input id="input1">
</div>
<div class="screen" id="screen2">
<input id="input2">
</div>
<div class="screen" id="screen3">
<input id="input3">
</div>
`)
);
});
it('should mark the screens with the conflicting element IDs as not importable', () => {
expect(importable.screens[0].canBeImported).to.be.true;
expect(importable.screens[1].canBeImported).to.be.false;
expect(importable.screens[2].canBeImported).to.be.true;
});
it('should include the list of conflicting element ids.', () => {
// note that screen2 has conflicting ids, even though in theory
// screen1 resolves that conflict since it replaces the destination screen causing
// the conflict. We explicitly choose not to handle that logic automatically.
expect(importable.screens[0].conflictingIds).to.deep.equal([]);
expect(importable.screens[1].conflictingIds).to.deep.equal(['input2']);
expect(importable.screens[2].conflictingIds).to.deep.equal([]);
});
});
});
describe('The importScreensAndAssets function', () => {
it('will add the specified screens', () => {
setExistingHTML(``);
var project = getImportableProject(
getProjectWithHTML(`
<div class="screen" id="screen1"></div>
<div class="screen" id="screen2"></div>
`)
);
expect(designMode.getAllScreenIds()).to.deep.equal([]);
importScreensAndAssets(project.id, [project.screens[0]], []);
expect(designMode.getAllScreenIds()).to.deep.equal(['screen1']);
importScreensAndAssets(project.id, [project.screens[1]], []);
expect(designMode.getAllScreenIds()).to.deep.equal([
'screen1',
'screen2'
]);
});
it('will replace screens with the same screen id', () => {
setExistingHTML(`
<div class="screen" id="design_screen1">
<input id="design_input1">
</div>
`);
var project = getImportableProject(
getProjectWithHTML(`
<div class="screen" id="screen1">
<input id="importedInput">
</div>
<div class="screen" id="screen2"></div>
`)
);
expect(designMode.getAllScreenIds()).to.deep.equal(['screen1']);
expect(elementUtils.getPrefixedElementById('input1')).not.to.be.null;
expect(elementUtils.getPrefixedElementById('importedInput')).to.be.null;
importScreensAndAssets(project.id, [project.screens[0]], []);
expect(designMode.getAllScreenIds()).to.deep.equal(['screen1']);
expect(elementUtils.getPrefixedElementById('input1')).to.be.null;
expect(elementUtils.getPrefixedElementById('importedInput')).not.to.be
.null;
});
it('can run through the same import twice without getting conflicts', () => {
setExistingHTML(`
<div class="screen" id="design_screen1">
<input id="design_input1">
</div>
`);
var project = getImportableProject(
getProjectWithHTML(`
<div class="screen" id="screen1">
<input id="importedInput">
</div>
<div class="screen" id="screen2">
<input id="importedInput2">
<input id="importedInput3">
</div>
`)
);
expect(designMode.getAllScreenIds()).to.deep.equal(['screen1']);
expect(elementUtils.getPrefixedElementById('input1')).not.to.be.null;
expect(elementUtils.getPrefixedElementById('importedInput')).to.be.null;
expect(elementUtils.getPrefixedElementById('importedInput2')).to.be.null;
expect(elementUtils.getPrefixedElementById('importedInput3')).to.be.null;
importScreensAndAssets(
project.id,
[project.screens[0], project.screens[1]],
[]
);
expect(designMode.getAllScreenIds()).to.deep.equal([
'screen1',
'screen2'
]);
expect(elementUtils.getPrefixedElementById('input1')).to.be.null;
expect(elementUtils.getPrefixedElementById('importedInput')).not.to.be
.null;
expect(elementUtils.getPrefixedElementById('importedInput2')).not.to.be
.null;
expect(elementUtils.getPrefixedElementById('importedInput3')).not.to.be
.null;
project = getImportableProject(
getProjectWithHTML(`
<div class="screen" id="screen1">
<input id="importedInput">
</div>
<div class="screen" id="screen2">
<input id="importedInput2">
<input id="importedInput3">
</div>
`)
);
expect(project.screens[0].conflictingIds).to.deep.equal([]);
expect(project.screens[1].conflictingIds).to.deep.equal([]);
});
describe('when replacing assets in imported screens', () => {
var project, onResolve, onReject, promise;
beforeEach(() => {
setExistingHTML(`
<div class="screen" id="design_screen1">
<input id="design_input1">
<img src="/v3/assets/some-project/asset1.png"
data-canonical-image-url="asset1.png"
id="design_img1">
</div>
`);
project = getImportableProject(
getProjectWithHTML(`
<div class="screen" id="screen1">
<input id="importedInput">
<img src="/v3/assets/some-other-project/asset1.png"
data-canonical-image-url="asset1.png"
id="img1">
<img src="/v3/assets/some-other-project/asset2.png"
data-canonical-image-url="asset2.png"
id="img2">
</div>
<div class="screen" id="screen2"></div>
`)
);
onResolve = sinon.spy();
onReject = sinon.spy();
promise = importScreensAndAssets(
project.id,
[project.screens[0], project.screens[1]],
[{filename: 'asset3.png'}, {filename: 'asset4.png'}]
).then(onResolve, onReject);
});
it('will import the specified screens', () => {
var [, , success] = assetsApi.copyAssets.lastCall.args;
success();
expect(designMode.getAllScreenIds()).to.deep.equal([
'screen1',
'screen2'
]);
expect(elementUtils.getPrefixedElementById('input1')).to.be.null;
expect(elementUtils.getPrefixedElementById('importedInput')).not.to.be
.null;
expect(elementUtils.getPrefixedElementById('img1')).not.to.be.null;
});
it('will copy the assets that are going to be replaced', () => {
expect(assetsApi.copyAssets).to.have.been.called;
var [
projectId,
allAssetsToReplace
] = assetsApi.copyAssets.firstCall.args;
expect(projectId).to.equal(project.id);
expect(allAssetsToReplace).to.have.members([
'asset1.png',
'asset2.png',
'asset3.png',
'asset4.png'
]);
});
it('will call resolve if the operation was successful', () => {
var [, , success] = assetsApi.copyAssets.lastCall.args;
expect(onResolve).not.to.have.been.called;
success();
return promise.then(() => {
expect(onResolve.called).to.be.true;
expect(onReject).not.to.have.been.called;
});
});
it('will call reject if the operation was not successful', () => {
var [, , , failure] = assetsApi.copyAssets.lastCall.args;
expect(onReject).not.to.have.been.called;
failure();
return promise.then(() => {
expect(onResolve).not.to.have.been.called;
expect(onReject).to.have.been.called;
});
});
});
});
});