/
animationListModuleTest.js
582 lines (505 loc) · 22.5 KB
/
animationListModuleTest.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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
import {combineReducers} from 'redux';
import sinon from 'sinon';
import reducer, {
START_LOADING_FROM_SOURCE,
DONE_LOADING_FROM_SOURCE,
animationSourceUrl,
setInitialAnimationList,
deleteAnimation,
cloneAnimation,
addBlankAnimation,
addLibraryAnimation,
withAbsoluteSourceUrls,
appendBlankFrame,
appendLibraryFrames,
appendCustomFrames,
saveAnimation
} from '@cdo/apps/gamelab/animationListModule';
import animationTab from '@cdo/apps/gamelab/AnimationTab/animationTabModule';
import {EMPTY_IMAGE} from '@cdo/apps/gamelab/constants';
import {createStore} from '../../util/redux';
import {expect} from '../../util/configuredChai';
import {setExternalGlobals} from '../../util/testUtils';
const project = require('@cdo/apps/code-studio/initApp/project');
describe('animationListModule', function () {
setExternalGlobals(beforeEach, afterEach);
describe('animationSourceUrl', function () {
const key = 'foo';
it(`returns the sourceUrl from props if it exists`, function () {
const props = {sourceUrl: 'bar'};
expect(animationSourceUrl(key, props)).to.equal('bar');
expect(animationSourceUrl(null, props)).to.equal('bar');
});
it(`returns the sourceUrl passed through the media proxy if it's an aboslute url`, function () {
const insecure = {sourceUrl: 'http://bar'};
expect(animationSourceUrl(key, insecure))
.to.equal(`//${document.location.host}/media?u=http%3A%2F%2Fbar`);
const secure = {sourceUrl: 'https://bar'};
expect(animationSourceUrl(key, secure))
.to.equal(`//${document.location.host}/media?u=https%3A%2F%2Fbar`);
});
it(`constructs a sourceUrl from key and project if one isn't provided in props`, function () {
const props = {sourceUrl: null};
expect(animationSourceUrl(key, props)).to.equal('/v3/animations/fake_id/foo.png');
});
it(`appends version query param if props has a version id and version flag is passed`, function () {
const props = {sourceUrl: null, version: 'baz'};
expect(animationSourceUrl(key, props, true)).to.equal('/v3/animations/fake_id/foo.png?version=baz');
});
});
describe('loadAnimationFromSource', function () {
// Note: I'm basically unable to test loadAnimationFromSource right now,
// because it makes an external request for a Blob and sinon can't fake
// a request for a blob under PhantomJS (grr) so I need to do some refactoring
// to allow me to stub a request method. in the meantime, I'll show
// that the reducer responds as expected.
const key = 'foo';
let store;
beforeEach(function () {
store = createStore(combineReducers({animationList: reducer}), {
animationList: {
orderedKeys: [key],
propsByKey: {
[key]: {
sourceUrl: 'anything (we stub this)'
}
}
}
});
});
it('sets the loadedFromSource flag', function () {
// TODO run loadAnimationFromSource action here when we can stub the request
expect(store.getState().animationList.propsByKey[key].loadedFromSource).to.be.undefined;
store.dispatch({type: START_LOADING_FROM_SOURCE, key});
expect(store.getState().animationList.propsByKey[key].loadedFromSource).to.be.false;
store.dispatch({
type: DONE_LOADING_FROM_SOURCE,
key
});
expect(store.getState().animationList.propsByKey[key].loadedFromSource).to.be.true;
});
it('populates animation blob', function () {
// TODO run loadAnimationFromSource action here when we can stub the request
expect(store.getState().animationList.propsByKey[key].blob).to.be.undefined;
store.dispatch({type: START_LOADING_FROM_SOURCE, key});
expect(store.getState().animationList.propsByKey[key].blob).to.be.undefined;
store.dispatch({
type: DONE_LOADING_FROM_SOURCE,
key,
blob: new Blob([])
});
expect(store.getState().animationList.propsByKey[key].blob).not.to.be.undefined;
});
it('populates animation dataURI', function () {
// TODO run loadAnimationFromSource action here when we can stub the request
expect(store.getState().animationList.propsByKey[key].dataURI).to.be.undefined;
store.dispatch({type: START_LOADING_FROM_SOURCE, key});
expect(store.getState().animationList.propsByKey[key].dataURI).to.be.undefined;
store.dispatch({
type: DONE_LOADING_FROM_SOURCE,
key,
dataURI: EMPTY_IMAGE
});
expect(store.getState().animationList.propsByKey[key].dataURI).not.to.be.undefined;
});
it('populates animation sourceSize', function () {
// TODO run loadAnimationFromSource action here when we can stub the request
expect(store.getState().animationList.propsByKey[key].sourceSize).to.be.undefined;
store.dispatch({type: START_LOADING_FROM_SOURCE, key});
expect(store.getState().animationList.propsByKey[key].sourceSize).to.be.undefined;
store.dispatch({
type: DONE_LOADING_FROM_SOURCE,
key,
sourceSize: {x: 1, y: 1}
});
expect(store.getState().animationList.propsByKey[key].sourceSize).not.to.be.undefined;
});
});
let createAnimationList = function (count) {
let orderedKeys = [];
let propsByKey = {};
let baseKey = 'animation';
for (let i = 1; i <= count; i++) {
let key = baseKey + '_' + i;
orderedKeys.push(key);
propsByKey[key] = {
name: key,
sourceUrl: null,
frameSize: {x: 100, y: 100},
frameCount: 1,
looping: true,
frameDelay: 4,
version: null
};
}
return {orderedKeys: orderedKeys, propsByKey: propsByKey};
};
describe('action: set initial animationList', function () {
let server, store;
beforeEach(function () {
project.getCurrentId.returns('');
server = sinon.fakeServer.create();
server.respondWith('imageBody');
store = createStore(combineReducers({animationList: reducer, animationTab}), {});
});
afterEach(function () {
server.restore();
});
it('when animationList has 1 item, selectedAnimation should be the animation', function () {
const key0 = 'animation_1';
let animationList = createAnimationList(1);
store.dispatch(setInitialAnimationList(animationList));
expect(store.getState().animationTab.selectedAnimation).to.equal(key0);
});
it('when animationList has multiple items, selectedAnimation should be the first animation', function () {
const key0 = 'animation_1';
let animationList = createAnimationList(2);
store.dispatch(setInitialAnimationList(animationList));
expect(store.getState().animationTab.selectedAnimation).to.equal(key0);
});
it('when animationList has 0 items, selectedAnimation should be the empty string', function () {
let animationList = {
orderedKeys: [],
propsByKey: {}
};
store.dispatch(setInitialAnimationList(animationList));
expect(store.getState().animationTab.selectedAnimation).to.equal('');
});
it('should not initialize with multiple animations of the same name', function () {
let animationList = createAnimationList(3);
animationList.propsByKey["animation_1"].name = 'cat';
animationList.propsByKey["animation_3"].name = 'cat';
store.dispatch(setInitialAnimationList(animationList));
expect(store.getState().animationList.propsByKey["animation_1"].name).to.equal('cat');
expect(store.getState().animationList.propsByKey["animation_2"].name).to.equal('animation_2');
expect(store.getState().animationList.propsByKey["animation_3"].name).to.equal('cat_1');
});
it('should not initialize with multiple animations of the same name with non alpha characters', function () {
let animationList = createAnimationList(3);
animationList.propsByKey["animation_1"].name = 'images (1).jpg_1';
animationList.propsByKey["animation_2"].name = 'images (1).jpg_1';
animationList.propsByKey["animation_3"].name = 'images (1).jpg_1';
store.dispatch(setInitialAnimationList(animationList));
expect(store.getState().animationList.propsByKey["animation_1"].name).to.equal('images (1).jpg_1');
expect(store.getState().animationList.propsByKey["animation_2"].name).to.equal('images (1).jpg_1_1');
expect(store.getState().animationList.propsByKey["animation_3"].name).to.equal('images (1).jpg_1_2');
});
});
describe('action: delete animation', function () {
let server;
beforeEach(function () {
project.getCurrentId.returns('');
sinon.stub(project, 'projectChanged').returns('');
server = sinon.fakeServer.create();
server.respondWith('imageBody');
});
afterEach(function () {
server.restore();
project.projectChanged.restore();
});
it('deleting the first animation reselects the next animation in the animationList', function () {
const key0 = 'animation_1';
const key1 = 'animation_2';
let animationList = createAnimationList(2);
let store = createStore(combineReducers({animationList: reducer, animationTab}), {});
store.dispatch(setInitialAnimationList(animationList));
store.dispatch(deleteAnimation(key0));
expect(store.getState().animationTab.selectedAnimation).to.equal(key1);
});
it('deleting an animation reselects the previous animation in the animationList', function () {
const key0 = 'animation_1';
const key1 = 'animation_2';
let animationList = createAnimationList(2);
let store = createStore(combineReducers({animationList: reducer, animationTab}), {});
store.dispatch(setInitialAnimationList(animationList));
store.dispatch(deleteAnimation(key1));
expect(store.getState().animationTab.selectedAnimation).to.equal(key0);
});
it('deleting an animation deselects when there are no other animations in the animationList', function () {
const key0 = 'animation_1';
let animationList = createAnimationList(1);
let store = createStore(combineReducers({animationList: reducer, animationTab}), {});
store.dispatch(setInitialAnimationList(animationList));
store.dispatch(deleteAnimation(key0));
expect(store.getState().animationTab.selectedAnimation).to.equal('');
});
});
describe('action: clone animation', function () {
let server;
beforeEach(function () {
project.getCurrentId.returns('');
sinon.stub(project, 'projectChanged').returns('');
server = sinon.fakeServer.create();
server.respondWith('imageBody');
});
afterEach(function () {
server.restore();
project.projectChanged.restore();
});
it('cloning animation creates an animation with the same props, and unique name', function () {
const key0 = 'animation_1';
const animationList = createAnimationList(1);
let store = createStore(combineReducers({animationList: reducer, animationTab}), {});
store.dispatch(setInitialAnimationList(animationList));
store.dispatch(cloneAnimation(key0));
expect(store.getState().animationList.orderedKeys.length).to.equal(2);
const clonedAnimationKey = store.getState().animationList.orderedKeys[1];
const clonedAnimation = store.getState().animationList.propsByKey[clonedAnimationKey];
const orignalAnimation = store.getState().animationList.propsByKey[key0];
expect(clonedAnimation.name).to.not.equal(orignalAnimation.name);
expect(clonedAnimation.frameSize).to.equal(orignalAnimation.frameSize);
expect(clonedAnimation.frameCount).to.equal(orignalAnimation.frameCount);
expect(clonedAnimation.frameDelay).to.equal(orignalAnimation.frameDelay);
expect(clonedAnimation.looping).to.equal(orignalAnimation.looping);
expect(clonedAnimation.sourceUrl).to.equal(orignalAnimation.sourceUrl);
});
it('cloning an animation twice creates two animations with unique names', function () {
const key0 = 'animation_1';
const animationList = createAnimationList(1);
let store = createStore(combineReducers({animationList: reducer, animationTab}), {});
store.dispatch(setInitialAnimationList(animationList));
store.dispatch(cloneAnimation(key0));
store.dispatch(cloneAnimation(key0));
const orignalAnimation = store.getState().animationList.propsByKey[key0];
const clonedAnimationKey2 = store.getState().animationList.orderedKeys[1];
const clonedAnimation2 = store.getState().animationList.propsByKey[clonedAnimationKey2];
const clonedAnimationKey1 = store.getState().animationList.orderedKeys[2];
const clonedAnimation1 = store.getState().animationList.propsByKey[clonedAnimationKey1];
expect(store.getState().animationList.orderedKeys.length).to.equal(3);
expect(orignalAnimation.name).to.not.equal(clonedAnimation1.name);
expect(orignalAnimation.name).to.not.equal(clonedAnimation2.name);
expect(clonedAnimation1.name).to.not.equal(clonedAnimation2.name);
});
});
describe('action: add blank animation', function () {
let server, store;
beforeEach(function () {
project.getCurrentId.returns('');
sinon.stub(project, 'projectChanged').returns('');
server = sinon.fakeServer.create();
server.respondWith('imageBody');
store = createStore(combineReducers({animationList: reducer, animationTab}), {});
});
afterEach(function () {
server.restore();
project.projectChanged.restore();
});
it('new blank animations get name animation_1 when it is the first blank animation', function () {
store.dispatch(addBlankAnimation());
let blankAnimationKey = store.getState().animationList.orderedKeys[0];
expect(store.getState().animationList.propsByKey[blankAnimationKey].name).to.equal('animation_1');
});
it('new blank animations get name next available number appended', function () {
let animationList = createAnimationList(2);
store.dispatch(setInitialAnimationList(animationList));
store.dispatch(addBlankAnimation());
let blankAnimationKey = store.getState().animationList.orderedKeys[2];
expect(store.getState().animationList.propsByKey[blankAnimationKey].name).to.equal('animation_3');
});
it('new blank animations get name next available number appended when available number is in the middle of the list', function () {
const key1 = 'animation_2';
let animationList = createAnimationList(3);
store.dispatch(setInitialAnimationList(animationList));
store.dispatch(deleteAnimation(key1));
expect(store.getState().animationList.orderedKeys.length).to.equal(2);
store.dispatch(addBlankAnimation());
expect(store.getState().animationList.orderedKeys.length).to.equal(3);
let blankAnimationKey = store.getState().animationList.orderedKeys[2];
expect(store.getState().animationList.propsByKey[blankAnimationKey].name).to.equal('animation_2');
});
});
describe('action: add library animation', function () {
let server, store;
beforeEach(function () {
project.getCurrentId.returns('');
sinon.stub(project, 'projectChanged').returns('');
server = sinon.fakeServer.create();
server.respondWith('imageBody');
store = createStore(combineReducers({animationList: reducer, animationTab}), {});
});
afterEach(function () {
server.restore();
project.projectChanged.restore();
});
it('new animations get name _# appended to the name in order of numbers available', function () {
const libraryAnimProps = {
name: 'library_animation',
sourceUrl: 'url',
frameSize: {x: 100, y: 100},
frameCount: 1,
looping: true,
frameDelay: 4,
version: null
};
store.dispatch(addLibraryAnimation(libraryAnimProps));
let blankAnimationKey1 = store.getState().animationList.orderedKeys[0];
expect(store.getState().animationList.propsByKey[blankAnimationKey1].name).to.equal('library_animation_1');
store.dispatch(addLibraryAnimation(libraryAnimProps));
let blankAnimationKey2 = store.getState().animationList.orderedKeys[1];
expect(store.getState().animationList.propsByKey[blankAnimationKey2].name).to.equal('library_animation_2');
store.dispatch(addLibraryAnimation(libraryAnimProps));
let blankAnimationKey3 = store.getState().animationList.orderedKeys[2];
expect(store.getState().animationList.propsByKey[blankAnimationKey3].name).to.equal('library_animation_3');
store.dispatch(deleteAnimation(blankAnimationKey2));
store.dispatch(addLibraryAnimation(libraryAnimProps));
let blankAnimationKey4 = store.getState().animationList.orderedKeys[2];
expect(store.getState().animationList.propsByKey[blankAnimationKey4].name).to.equal('library_animation_2');
});
});
describe('withAbsoluteSourceUrls', function () {
function expectDeepEqual(a, b) {
expect(a).to.deep.equal(b,
"Expected to be deeply equal:\n" +
"<<<<<<<<\n" +
JSON.stringify(a, null, 2) + "\n" +
"--------\n" +
JSON.stringify(b, null, 2) + "\n" +
">>>>>>>>\n");
}
it('generates absolute source URLs for animations with no source URL', function () {
const serializedList = {
orderedKeys: ['foo'],
propsByKey: {
'foo': {}
}
};
expectDeepEqual(withAbsoluteSourceUrls(serializedList), {
orderedKeys: ['foo'],
propsByKey: {
'foo': {
sourceUrl: `${document.location.origin}/v3/animations/fake_id/foo.png`
}
}
});
});
it('converts relative source URLs to absolute URLs', function () {
const serializedList = {
orderedKeys: ['foo'],
propsByKey: {
'foo': {
sourceUrl: '/some-origin-relative-url'
}
}
};
expectDeepEqual(withAbsoluteSourceUrls(serializedList), {
orderedKeys: ['foo'],
propsByKey: {
'foo': {
sourceUrl: `${document.location.origin}/some-origin-relative-url`
}
}
});
});
it('Uses media proxy for absolute URLs', function () {
const serializedList = {
orderedKeys: ['foo'],
propsByKey: {
'foo': {
sourceUrl: 'http://host.com/some-absolute-url'
}
}
};
expectDeepEqual(withAbsoluteSourceUrls(serializedList), {
orderedKeys: ['foo'],
propsByKey: {
'foo': {
sourceUrl: `${document.location.origin}/media?u=http%3A%2F%2Fhost.com%2Fsome-absolute-url`
}
}
});
});
});
describe('action: add blank frame', function () {
let server, store;
beforeEach(function () {
project.getCurrentId.returns('');
sinon.stub(project, 'projectChanged').returns('');
server = sinon.fakeServer.create();
server.respondWith('imageBody');
store = createStore(combineReducers({animationList: reducer, animationTab}), {});
});
afterEach(function () {
server.restore();
project.projectChanged.restore();
});
it('new blank frame gets added to pendingFrames and original animation is unchanged', function () {
const animationList = createAnimationList(1);
store.dispatch(setInitialAnimationList(animationList));
const animationKey = store.getState().animationList.orderedKeys[0];
store.dispatch(appendBlankFrame());
expect(store.getState().animationList.propsByKey[animationKey].frameCount).to.equal(1);
expect(store.getState().animationList.pendingFrames.key).to.equal(animationKey);
expect(store.getState().animationList.pendingFrames.props.blankFrame).to.equal(true);
});
it('new blank pending frame uses the selectedAnimation key', function () {
const animationList = createAnimationList(2);
store.dispatch(setInitialAnimationList(animationList));
const selectedAnimation = store.getState().animationTab.selectedAnimation;
store.dispatch(appendBlankFrame());
expect(store.getState().animationList.pendingFrames.key).to.equal(selectedAnimation);
});
});
describe('action: append non blank frames', function () {
let server, store, selectedAnimation, libraryAnimProps;
beforeEach(function () {
project.getCurrentId.returns('');
sinon.stub(project, 'projectChanged').returns('');
server = sinon.fakeServer.create();
server.respondWith('imageBody');
store = createStore(combineReducers({animationList: reducer, animationTab}), {});
const animationList = createAnimationList(2);
store.dispatch(setInitialAnimationList(animationList));
selectedAnimation = store.getState().animationTab.selectedAnimation;
libraryAnimProps = {
name: 'library_animation',
sourceUrl: 'url',
frameSize: {x: 100, y: 100},
frameCount: 1,
looping: true,
frameDelay: 4,
version: null
};
});
afterEach(function () {
server.restore();
project.projectChanged.restore();
});
it('append library frames adds props to pendingFrames for selectedAnimation', function () {
store.dispatch(appendLibraryFrames(libraryAnimProps));
expect(store.getState().animationList.pendingFrames.key).to.equal(selectedAnimation);
expect(store.getState().animationList.pendingFrames.props).to.deep.equal(libraryAnimProps);
});
it('append custom frames adds props to pendingFrames for selected animation', function () {
store.dispatch(appendCustomFrames(libraryAnimProps));
expect(store.getState().animationList.pendingFrames.key).to.equal(selectedAnimation);
expect(store.getState().animationList.pendingFrames.props).to.deep.equal(libraryAnimProps);
});
});
describe('action: save animation', function () {
let xhr, requests;
beforeEach(function () {
xhr = sinon.useFakeXMLHttpRequest();
requests = [];
xhr.onCreate = xhr => requests.push(xhr);
});
afterEach(function () {
xhr.restore();
});
it('sends a save request', function () {
const libraryAnimProps = {
name: 'animation_1',
sourceUrl: 'url',
frameSize: {x: 100, y: 100},
frameCount: 1,
looping: true,
frameDelay: 4,
version: null
};
saveAnimation('animation_1', libraryAnimProps);
expect(requests.length).to.equal(1);
expect(requests[0].method).to.equal('PUT');
expect(requests[0].url).to.equal("/v3/animations/fake_id/animation_1.png");
expect(requests[0].requestHeaders['Content-type']).to.equal("image/png;charset=utf-8");
});
});
});