-
Notifications
You must be signed in to change notification settings - Fork 105
/
BaseViewer-test.js
911 lines (770 loc) · 31.6 KB
/
BaseViewer-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
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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
/* eslint-disable no-unused-expressions */
import EventEmitter from 'events';
import BaseViewer from '../BaseViewer';
import Browser from '../../Browser';
import RepStatus from '../../RepStatus';
import fullscreen from '../../Fullscreen';
import * as util from '../../util';
import * as file from '../../file';
import * as constants from '../../constants';
let base;
let containerEl;
let stubs = {};
const sandbox = sinon.sandbox.create();
const ANNOTATION_MODE_ENTER = 'annotationmodeenter';
const ANNOTATION_MODE_EXIT = 'annotationmodeexit';
describe('lib/viewers/BaseViewer', () => {
before(() => {
fixture.setBase('src/lib');
});
beforeEach(() => {
fixture.load('viewers/__tests__/BaseViewer-test.html');
containerEl = document.querySelector('.bp-container');
stubs.browser = sandbox.stub(Browser, 'isMobile').returns(false);
base = new BaseViewer({
container: containerEl,
file: {
id: '0'
}
});
});
afterEach(() => {
if (base && typeof base.destroy === 'function' && !base.destroyed) {
base.destroy();
}
sandbox.verifyAndRestore();
});
describe('setup()', () => {
it('should set options, a container, bind event listeners, and set timeout', () => {
sandbox.stub(base, 'addCommonListeners');
sandbox.stub(base, 'loadAssets').returns(Promise.resolve());
sandbox.stub(base, 'finishLoadingSetup');
base.options.showAnnotations = true;
base.setup();
expect(base.options).to.deep.equal({
container: containerEl,
file: {
id: '0'
},
showAnnotations: true
});
expect(base.containerEl).to.have.class('bp');
expect(base.addCommonListeners).to.be.called;
expect(base.loadTimeout).to.be.a.number;
expect(base.loadAssets).to.be.called;
});
it('should add a mobile class to the container if on mobile', () => {
base.isMobile = true;
sandbox.stub(base, 'loadAssets').returns(Promise.resolve());
sandbox.stub(base, 'finishLoadingSetup');
base.setup();
const container = document.querySelector('.bp');
expect(container).to.have.class('bp-is-mobile');
});
it('should not load annotations assets if global preview showAnnotations option is false', () => {
sandbox.stub(base, 'addCommonListeners');
sandbox.stub(base, 'loadAssets');
sandbox.stub(base, 'finishLoadingSetup');
base.options.showAnnotations = false;
base.setup();
expect(base.loadAssets).to.not.be.called;
});
it('should not load annotations assets if expiring embed is a shared link', () => {
sandbox.stub(base, 'addCommonListeners');
sandbox.stub(base, 'loadAssets');
sandbox.stub(base, 'finishLoadingSetup');
base.options.sharedLink = 'url';
base.setup();
expect(base.loadAssets).to.not.be.called;
});
});
describe('finishLoadingSetup()', () => {
it('should hide the crawler and set the file icon into the icon element', () => {
const container = {
classList: {
add: sandbox.stub()
},
innerHTML: '',
removeEventListener: sandbox.stub()
};
base.fileLoadingIcon = 'icon';
sandbox.stub(containerEl, 'querySelector').returns(container);
base.finishLoadingSetup();
expect(container.innerHTML).to.equal('icon');
expect(container.classList.add).to.be.called;
base.options.container = null;
});
});
describe('debouncedResizeHandler()', () => {
it('should return a resize handler', () => {
expect(base.debouncedResizeHandler()).to.be.a.function;
});
});
describe('load()', () => {
it('should call resetLoadTimeout', () => {
sandbox.stub(base, 'resetLoadTimeout');
base.load();
expect(base.resetLoadTimeout).to.be.called;
});
});
describe('resetLoadTimeout()', () => {
it('should clear timeout and set a new timeout handler', () => {
sandbox.stub(window, 'clearTimeout');
sandbox.spy(window, 'setTimeout');
base.resetLoadTimeout();
base.loaded = true;
expect(window.clearTimeout).to.be.called;
expect(window.setTimeout).to.be.called;
expect(base.loadTimeoutId).to.be.a.number;
// Test cleanup
clearTimeout(base.loadTimeoutId);
});
});
describe('handleAssetError()', () => {
it('should trigger error and set destroyed to true', () => {
sandbox.stub(base, 'triggerError');
base.handleAssetError();
expect(base.triggerError).to.be.called;
expect(base.destroyed).to.be.true;
});
});
describe('triggerError()', () => {
it('should emit error event', () => {
sandbox.stub(base, 'emit');
const err = new Error('blah');
base.triggerError(err);
expect(base.emit).to.be.calledWith('error', err);
});
});
describe('isLoaded()', () => {
it('should return loaded property', () => {
expect(base.isLoaded()).to.equal(base.loaded);
});
});
describe('isDestroyed()', () => {
it('should return loaded property', () => {
expect(base.isDestroyed()).to.equal(base.destroyed);
});
});
describe('appendAuthParams()', () => {
it('should return content url with auth options appended', () => {
const token = 'TOKEN';
const sharedLink = 'https://app.box.com/s/HASH';
const sharedLinkPassword = 'pass';
const url = 'url';
base = new BaseViewer({
token,
sharedLink,
sharedLinkPassword,
container: containerEl,
file: {
id: '0'
}
});
sandbox.stub(util, 'appendAuthParams').returns(url);
const result = base.appendAuthParams('');
expect(result).to.equal(url);
expect(util.appendAuthParams).to.be.calledWith('', token, sharedLink, sharedLinkPassword);
});
});
describe('createContentUrl()', () => {
it('should return content url with no asset path', () => {
const url = 'url{asset_path}';
sandbox.spy(util, 'createContentUrl');
const result = base.createContentUrl(url, '');
expect(result).to.equal('url');
expect(util.createContentUrl).to.be.calledWith(url, '');
});
it('should return content url with asset path from args', () => {
const url = 'url{asset_path}';
base = new BaseViewer({
viewer: { ASSET: 'foo' },
container: containerEl,
file: {
id: '0'
}
});
sandbox.spy(util, 'createContentUrl');
const result = base.createContentUrl(url, 'bar');
expect(result).to.equal('urlbar');
expect(util.createContentUrl).to.be.calledWith(url, 'bar');
});
});
describe('createContentUrlWithAuthParams()', () => {
it('should return content url with no asset path', () => {
sandbox.stub(base, 'createContentUrl').returns('foo');
sandbox.stub(base, 'appendAuthParams').returns('bar');
const result = base.createContentUrlWithAuthParams('boo', 'hoo');
expect(result).to.equal('bar');
expect(base.createContentUrl).to.be.calledWith('boo', 'hoo');
expect(base.appendAuthParams).to.be.calledWith('foo');
});
});
describe('appendAuthHeader()', () => {
it('should return fetch headers', () => {
const token = 'TOKEN';
const sharedLink = 'https://app.box.com/s/HASH';
const sharedLinkPassword = 'pass';
const headers = {};
base = new BaseViewer({
token,
sharedLink,
sharedLinkPassword,
container: containerEl,
file: {
id: '0'
}
});
sandbox.stub(util, 'getHeaders').returns(headers);
const result = base.appendAuthHeader(headers);
expect(result).to.equal(headers);
expect(util.getHeaders).to.be.calledWith(headers, token, sharedLink, sharedLinkPassword);
});
});
describe('addCommonListeners()', () => {
it('should append common event listeners', () => {
sandbox.stub(fullscreen, 'addListener');
sandbox.stub(base, 'addListener');
sandbox.stub(document.defaultView, 'addEventListener');
base.addCommonListeners();
expect(fullscreen.addListener).to.be.calledWith('enter', sinon.match.func);
expect(fullscreen.addListener).to.be.calledWith('exit', sinon.match.func);
expect(document.defaultView.addEventListener).to.be.calledWith('resize', base.debouncedResizeHandler);
expect(base.addListener).to.be.calledWith('load', sinon.match.func);
});
it('should load the annotator when load is emitted with scale', (done) => {
sandbox.stub(fullscreen, 'addListener');
sandbox.stub(document.defaultView, 'addEventListener');
sandbox.stub(base, 'loadAnnotator');
base.containerEl = containerEl;
base.annotationsPromise = {
then: (arg) => {
expect(base.scale).to.equal(1.5);
expect(arg).to.equal(base.loadAnnotator);
done();
}
};
base.addCommonListeners();
expect(base.scale).to.equal(1);
base.emit('load', {
scale: 1.5
});
});
it('should load the annotator when load is emitted without an event', (done) => {
sandbox.stub(fullscreen, 'addListener');
sandbox.stub(document.defaultView, 'addEventListener');
sandbox.stub(base, 'loadAnnotator');
base.containerEl = containerEl;
base.annotationsPromise = {
then: (arg) => {
expect(arg).to.equal(base.loadAnnotator);
done();
}
};
base.addCommonListeners();
base.emit('load');
});
});
describe('toggleFullscreen()', () => {
it('should toggle fullscreen', () => {
sandbox.stub(fullscreen, 'toggle');
base.toggleFullscreen();
expect(fullscreen.toggle).to.be.calledWith(base.containerEl);
});
});
describe('onFullscreenToggled()', () => {
beforeEach(() => {
base.containerEl = document.createElement('div');
sandbox.stub(fullscreen, 'isSupported').returns(false);
sandbox.stub(base, 'resize');
});
it('should toggle the fullscreen class', () => {
base.onFullscreenToggled();
expect(base.containerEl.classList.contains(constants.CLASS_FULLSCREEN)).to.be.true;
base.onFullscreenToggled();
expect(base.containerEl.classList.contains(constants.CLASS_FULLSCREEN)).to.be.false;
});
it('should toggle the unsupported class if the browser does not support the fullscreen API', () => {
base.onFullscreenToggled();
expect(base.containerEl.classList.contains(constants.CLASS_FULLSCREEN_UNSUPPORTED)).to.be.true;
base.onFullscreenToggled();
expect(base.containerEl.classList.contains(constants.CLASS_FULLSCREEN_UNSUPPORTED)).to.be.false;
});
it('should resize the viewer', () => {
base.onFullscreenToggled();
expect(base.resize).to.be.called;
});
});
describe('resize()', () => {
it('should broadcast resize event', () => {
sandbox.stub(base, 'emit');
base.resize();
expect(base.emit).to.be.calledWith('resize');
});
});
describe('allowNavigationArrows()', () => {
it('should return true for base viewer', () => {
expect(base.allowNavigationArrows()).to.be.true;
});
});
describe('destroy()', () => {
it('should clean up rep statuses', () => {
const destroyMock = sandbox.mock().twice();
const removeListenerMock = sandbox.mock().twice();
base.repStatuses = [
{
removeListener: removeListenerMock,
destroy: destroyMock
},
{
removeListener: removeListenerMock,
destroy: destroyMock
}
];
base.destroy();
});
it('should cleanup the base viewer', () => {
sandbox.stub(base, 'loadAssets').returns(Promise.resolve());
sandbox.stub(base, 'loadAnnotator');
sandbox.stub(base, 'finishLoadingSetup');
base.setup();
sandbox.mock(fullscreen).expects('removeAllListeners');
sandbox.stub(base, 'removeAllListeners');
sandbox.stub(base, 'emit');
base.destroy();
expect(base.removeAllListeners).to.be.called;
expect(base.containerEl.innerHTML).to.equal('');
expect(base.destroyed).to.be.true;
expect(base.emit).to.be.calledWith('destroy');
});
it('should clean up annotator', () => {
base.annotator = {
removeAllListeners: sandbox.mock(),
destroy: sandbox.mock()
};
base.destroy();
expect(base.annotator.removeAllListeners).to.be.called;
expect(base.annotator.destroy).to.be.called;
});
});
describe('emit()', () => {
const emitFunc = EventEmitter.prototype.emit;
afterEach(() => {
Object.defineProperty(EventEmitter.prototype, 'emit', { value: emitFunc });
});
it('should pass through the event as well as broadcast it as a viewer event', () => {
const fileId = '1';
const event = 'someEvent';
const data = {};
const viewerName = 'name';
base = new BaseViewer({
viewer: { NAME: viewerName },
file: {
id: fileId
},
container: containerEl
});
const emitStub = sandbox.stub();
Object.defineProperty(EventEmitter.prototype, 'emit', { value: emitStub });
base.emit(event, data);
expect(emitStub).to.be.calledWith(event, data);
expect(emitStub).to.be.calledWithMatch('viewerevent', {
event,
data,
viewerName,
fileId
});
});
});
describe('Pinch to Zoom Handlers', () => {
let event = {};
beforeEach(() => {
base = new BaseViewer({
container: containerEl,
file: {
id: '123'
}
});
sandbox.stub(base, 'loadAssets').returns(Promise.resolve());
sandbox.stub(base, 'loadAnnotator');
sandbox.stub(base, 'finishLoadingSetup');
base.setup();
event = {
preventDefault: sandbox.stub(),
stopPropagation: sandbox.stub(),
touches: [0, 0]
};
stubs.isIOS = sandbox.stub(Browser, 'isIOS');
stubs.sqrt = sandbox.stub(Math, 'sqrt');
base.zoomIn = sandbox.stub();
base.zoomOut = sandbox.stub();
});
afterEach(() => {
stubs = {};
event = {};
base._scaling = false;
base._pincScale = undefined;
base.destroy();
});
describe('mobileZoomStartHandler', () => {
it('should turn on scaling and prevent default behavior if on iOS', () => {
stubs.isIOS.returns(true);
base.mobileZoomStartHandler(event);
expect(base._scaling).to.equal(true);
expect(event.stopPropagation).to.be.called;
expect(event.preventDefault).to.be.called;
});
it('should store the event details if two touches are detected and not on iOS', () => {
stubs.isIOS.returns(false);
base.mobileZoomStartHandler(event);
expect(base._scaling).to.equal(true);
expect(base._pinchScale).to.not.equal(undefined);
expect(event.stopPropagation).to.be.called;
expect(event.preventDefault).to.be.called;
});
it('should do nothing if event did not record two touches and not on iOS', () => {
stubs.isIOS.returns(false);
event.touches = [0];
base.mobileZoomStartHandler(event);
expect(base._scaling).to.equal(false);
expect(base._pinchScale).to.equal(undefined);
expect(event.stopPropagation).to.not.be.called;
expect(event.preventDefault).to.not.be.called;
});
});
describe('mobileZoomChangeHandler', () => {
it('should update the end touch and prevent default if two touches are detected', () => {
base.mobileZoomStartHandler(event);
base.mobileZoomChangeHandler(event);
expect(base._pinchScale.end).to.not.equal(undefined);
});
it('should not do anything if two touches are not recorded', () => {
event.touches = [0];
base.mobileZoomStartHandler(event);
base.mobileZoomChangeHandler(event);
expect(base._pinchScale).to.equal(undefined);
});
it('should not do anything if scaling is not happening', () => {
event.touches = [0];
base.mobileZoomStartHandler(event);
event.touches = [0, 0];
base.mobileZoomChangeHandler(event);
expect(base._pinchScale).to.equal(undefined);
});
});
describe('mobileZoomEndHandler', () => {
it('should zoom in if on iOS and event scale is > 1', () => {
event.scale = 1.5;
stubs.isIOS.returns(true);
base.mobileZoomStartHandler(event);
base.mobileZoomEndHandler(event);
expect(base.zoomIn).to.be.called;
});
it('should zoom out if on iOS and event scale is < 1', () => {
event.scale = 0.75;
stubs.isIOS.returns(true);
base.mobileZoomStartHandler(event);
base.mobileZoomEndHandler(event);
expect(base.zoomOut).to.be.called;
});
it('should zoom in if not on iOS and the scale is > 0', () => {
stubs.sqrt.onCall(0).returns(0).onCall(1).returns(0.5);
stubs.isIOS.returns(false);
base.mobileZoomStartHandler(event);
event.touches = [
{
clientX: 0,
clientY: 0
},
{
clientX: 0,
clientY: 0
}
];
base.mobileZoomChangeHandler(event);
base.mobileZoomEndHandler(event);
expect(base.zoomIn).to.be.called;
expect(base._scaling).to.equal(false);
expect(base._pincScale).to.equal(undefined);
});
it('should zoom out if not on iOS and the scale is < 0', () => {
stubs.sqrt.onCall(0).returns(0.5).onCall(1).returns(0);
stubs.isIOS.returns(false);
base.mobileZoomStartHandler(event);
event.touches = [
{
clientX: 0,
clientY: 0
},
{
clientX: 0,
clientY: 0
}
];
base.mobileZoomChangeHandler(event);
base.mobileZoomEndHandler(event);
expect(base.zoomOut).to.be.called;
expect(base.zoomIn).to.not.be.called;
expect(base._scaling).to.equal(false);
expect(base._pincScale).to.equal(undefined);
});
});
});
describe('getViewerOption', () => {
it('should return the user-defined viewer option with the specified key if it exists', () => {
const baz = 'captain-america';
base.options.viewers = {
Base: {
fooBar: baz
}
};
base.options.viewer = { NAME: 'Base' };
expect(base.getViewerOption('fooBar')).to.equal(baz);
});
});
describe('loadAssets()', () => {
it('should create an asset URL and load the relevant stylesheets and scripts', () => {
base.options.location = {};
const promise = Promise.resolve();
sandbox.stub(util, 'createAssetUrlCreator').returns(() => {});
sandbox.stub(util, 'loadStylesheets');
sandbox.stub(util, 'loadScripts').returns(promise);
const result = base.loadAssets();
expect(util.createAssetUrlCreator).to.be.calledWith(base.options.location);
expect(util.loadStylesheets).to.be.called;
expect(util.loadScripts).to.be.called;
expect(result).to.equal(promise);
});
});
describe('prefetchAssets()', () => {
it('should create an asset URL and prefetch the relevant stylesheets and scripts', () => {
base.options.location = {};
sandbox.stub(util, 'createAssetUrlCreator').returns(() => {});
sandbox.stub(util, 'prefetchAssets');
base.prefetchAssets();
expect(util.createAssetUrlCreator).to.be.calledWith(base.options.location);
expect(util.prefetchAssets).to.be.calledTwice;
});
});
describe('getRepStatus()', () => {
beforeEach(() => {
base.options.representation = {
info: {
url: 'someurl'
}
};
});
it('should create a new rep status, save, and return it', () => {
const repStatus = base.getRepStatus();
expect(base.repStatuses.find((status) => status === repStatus)).to.not.be.undefined;
expect(repStatus).to.be.instanceof(RepStatus);
});
it('should use the passed in representation', () => {
const representation = {
info: {
url: 'someOtherUrl'
}
};
const repStatus = base.getRepStatus(representation);
expect(repStatus.representation).to.equal(representation);
});
});
describe('isRepresentationReady()', () => {
it('should return whether the representation has a successful status', () => {
const representation = {
status: {
state: 'success'
}
};
expect(base.isRepresentationReady(representation)).to.be.true;
representation.status.state = 'viewable';
expect(base.isRepresentationReady(representation)).to.be.true;
representation.status.state = 'error';
expect(base.isRepresentationReady(representation)).to.be.false;
});
});
describe('disableViewerControls()', () => {
it('should disable viewer controls', () => {
base.controls = {
disable: sandbox.stub()
};
base.disableViewerControls();
expect(base.controls.disable).to.be.called;
});
});
describe('enableViewerControls()', () => {
it('should enable viewer controls', () => {
base.controls = {
enable: sandbox.stub()
};
base.enableViewerControls();
expect(base.controls.enable).to.be.called;
});
});
describe('loadAnnotator()', () => {
beforeEach(() => {
base.options.viewer = {
NAME: Document
};
stubs.annotatorConf = {
TYPE: ['point', 'highlight']
};
stubs.areAnnotationsEnabled = sandbox.stub(base, 'areAnnotationsEnabled').returns(true);
sandbox.stub(base, 'initAnnotations');
stubs.checkPermission = sandbox.stub(file, 'checkPermission').returns(false);
});
it('should load the appropriate annotator for the current viewer', () => {
class BoxAnnotations {
determineAnnotator() {
return stubs.annotatorConf;
}
}
stubs.checkPermission.returns(true);
window.BoxAnnotations = BoxAnnotations;
base.loadAnnotator();
expect(base.initAnnotations).to.be.called;
});
it('should not load an annotator if no loader was found', () => {
class BoxAnnotations {
determineAnnotator() {}
}
window.BoxAnnotations = BoxAnnotations;
base.loadAnnotator();
expect(base.initAnnotations).to.not.be.called;
});
it('should not load an annotator if the viewer is not annotatable', () => {
class BoxAnnotations {
determineAnnotator() {
return stubs.annotatorConf;
}
}
window.BoxAnnotations = BoxAnnotations;
stubs.areAnnotationsEnabled.returns(false);
base.loadAnnotator();
expect(base.initAnnotations).to.not.be.called;
});
});
describe('initAnnotations()', () => {
beforeEach(() => {
base.options = {
container: document,
file: {
file_version: {
id: 123
}
},
location: {
locale: 'en-US'
}
};
base.addListener = sandbox.stub();
base.scale = 1.5;
base.annotator = {
init: sandbox.stub(),
addListener: sandbox.stub()
};
base.annotatorConf = {
CONSTRUCTOR: sandbox.stub().returns(base.annotator)
};
base.initAnnotations();
});
it('should initialize the annotator', () => {
expect(base.annotator.init).to.be.calledWith(1.5);
expect(base.addListener).to.be.calledWith('toggleannotationmode', sinon.match.func);
expect(base.addListener).to.be.calledWith('scale', sinon.match.func);
expect(base.annotator.addListener).to.be.calledWith('annotatorevent', sinon.match.func);
});
});
describe('areAnnotationsEnabled()', () => {
beforeEach(() => {
stubs.getViewerOption = sandbox.stub(base, 'getViewerOption').withArgs('annotations').returns(false);
});
it('should return true if viewer option is set to true', () => {
expect(base.areAnnotationsEnabled()).to.equal(false);
stubs.getViewerOption.returns(true);
expect(base.areAnnotationsEnabled()).to.equal(true);
});
it('should use the global show annotationsBoolean if the viewer param is not specified', () => {
stubs.getViewerOption.withArgs('annotations').returns(null);
base.options.showAnnotations = true;
expect(base.areAnnotationsEnabled()).to.equal(true);
base.options.showAnnotations = false;
expect(base.areAnnotationsEnabled()).to.equal(false);
});
});
describe('handleAnnotatorNotifications()', () => {
const ANNOTATION_TYPE_DRAW = 'draw';
const ANNOTATION_TYPE_POINT = 'point';
beforeEach(() => {
sandbox.stub(base, 'emit');
base.annotator = {
isInAnnotationMode: sandbox.stub()
};
stubs.isInAnnotationMode = base.annotator.isInAnnotationMode;
});
it('should disable controls and show point mode notification on annotationmodeenter', () => {
sandbox.stub(base, 'disableViewerControls');
base.handleAnnotatorNotifications({
event: ANNOTATION_MODE_ENTER,
data: ANNOTATION_TYPE_POINT
});
expect(base.disableViewerControls).to.be.called;
expect(base.emit).to.be.calledWith('notificationshow', sinon.match.string);
});
it('should disable controls and show draw mode notification on annotationmodeenter', () => {
sandbox.stub(base, 'disableViewerControls');
base.handleAnnotatorNotifications({
event: ANNOTATION_MODE_ENTER,
data: ANNOTATION_TYPE_DRAW
});
expect(base.disableViewerControls).to.be.called;
expect(base.emit).to.be.calledWith('notificationshow', sinon.match.string);
});
it('should hide notification on annotationmodeexit', () => {
sandbox.stub(base, 'enableViewerControls');
stubs.isInAnnotationMode.returns(true);
base.handleAnnotatorNotifications({
event: ANNOTATION_MODE_EXIT
});
expect(base.enableViewerControls).to.not.be.called;
expect(base.emit).to.be.calledWith('notificationhide');
});
it('should enable controls if not in point annotation mode on annotationmodeexit', () => {
sandbox.stub(base, 'enableViewerControls');
stubs.isInAnnotationMode.returns(false);
base.handleAnnotatorNotifications({
event: ANNOTATION_MODE_EXIT
});
expect(base.enableViewerControls).to.be.called;
expect(base.emit).to.be.calledWith('notificationhide');
});
it('should show a notification on annotationerror', () => {
const data = {
event: 'annotationerror',
data: 'message'
};
base.handleAnnotatorNotifications(data);
expect(base.emit).to.be.calledWith('notificationshow', data.data);
});
it('should scale annotations on annotationsfetched', () => {
base.scale = 1;
base.rotationAngle = 90;
base.handleAnnotatorNotifications({
event: 'annotationsfetched'
});
expect(base.emit).to.be.calledWith('scale', {
scale: base.scale,
rotationAngle: base.rotationAngle
});
});
it('should emit annotatorevent when event does not match', () => {
const data = {
event: 'no match',
data: 'message'
};
base.handleAnnotatorNotifications(data);
expect(base.emit).to.be.calledWith(data.event, data.data);
expect(base.emit).to.be.calledWith('annotatorevent', data);
});
});
});