forked from box/box-annotations
/
AnnotationThread-test.js
640 lines (551 loc) · 22.1 KB
/
AnnotationThread-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
/* eslint-disable no-unused-expressions */
import AnnotationThread from '../AnnotationThread';
import * as util from '../util';
import {
STATES,
TYPES,
CLASS_ANNOTATION_POINT_MARKER,
DATA_TYPE_ANNOTATION_INDICATOR,
THREAD_EVENT
} from '../constants';
let thread;
const html = '<div class="annotated-element"></div>';
describe('AnnotationThread', () => {
let rootElement;
let api = {
user: {
id: '1'
}
};
beforeEach(() => {
rootElement = document.createElement('div');
rootElement.innerHTML = html;
document.body.appendChild(rootElement);
thread = new AnnotationThread({
annotatedElement: rootElement,
annotations: [],
api,
fileVersionId: '1',
isMobile: false,
location: {},
threadID: '2',
threadNumber: '1',
type: 'point'
});
thread.emit = jest.fn();
util.shouldDisplayMobileUI = jest.fn().mockReturnValue(false);
});
afterEach(() => {
document.body.removeChild(rootElement);
thread = null;
});
describe('destroy()', () => {
beforeEach(() => {
thread.state = STATES.pending;
thread.unbindDOMListeners = jest.fn();
thread.unmountPopover = jest.fn();
});
it('should unbind listeners and remove thread element and broadcast that the thread was deleted', () => {
thread.destroy();
expect(thread.unbindDOMListeners).toBeCalled();
expect(thread.unmountPopover).toBeCalled();
});
});
describe('unmountPopover', () => {
beforeEach(() => {
thread.popoverComponent = {};
thread.reset = jest.fn();
thread.toggleFlippedThreadEl = jest.fn();
thread.container = {
querySelectorAll: jest.fn().mockReturnValue([rootElement])
};
});
it('should there are no popover layers', () => {
thread.container = {
querySelectorAll: jest.fn().mockReturnValue([])
};
thread.unmountPopover();
expect(thread.popoverComponent).not.toBeNull();
expect(thread.reset).toBeCalled();
expect(thread.toggleFlippedThreadEl).toBeCalled();
});
it('should unmount any visible popovers', () => {
thread.unmountPopover();
expect(thread.popoverComponent).toBeNull();
expect(thread.reset).toBeCalled();
expect(thread.toggleFlippedThreadEl).toBeCalled();
});
});
describe('hide()', () => {
it('should hide the thread element', () => {
thread.unmountPopover = jest.fn();
thread.hide();
expect(thread.unmountPopover).toBeCalled();
expect(thread.state).toEqual(STATES.inactive);
});
});
describe('reset()', () => {
it('should set the thread state to inactive', () => {
thread.reset();
expect(thread.state).toEqual(STATES.inactive);
});
});
describe('save()', () => {
beforeEach(() => {
thread.getThreadEventData = jest.fn().mockReturnValue({});
thread.handleThreadSaveError = jest.fn();
thread.updateTemporaryAnnotation = jest.fn();
thread.renderAnnotationPopover = jest.fn();
});
it('should save an annotation with the specified type and text', (done) => {
thread.api.create = jest.fn().mockResolvedValue({});
const promise = thread.save('point', 'blah');
promise.then(() => {
expect(thread.updateTemporaryAnnotation).toBeCalled();
done();
});
expect(thread.api.create).toBeCalled();
});
it('should delete the temporary annotation and broadcast an error if there was an error saving', (done) => {
thread.api.create = jest.fn().mockRejectedValue({});
const promise = thread.save('point', 'blah');
promise.then(() => {
expect(thread.handleThreadSaveError).toBeCalled();
done();
});
expect(thread.api.create).toBeCalled();
expect(thread.updateTemporaryAnnotation).not.toBeCalled();
});
});
describe('updateTemporaryAnnotation()', () => {
const tempAnnotation = { id: 1 };
const serverAnnotation = {
id: 456,
threadNumber: 1,
message: 'comment'
};
beforeEach(() => {
thread.api.create = jest.fn();
thread.getThreadEventData = jest.fn().mockReturnValue({});
thread.renderAnnotationPopover = jest.fn();
thread.comments = [tempAnnotation];
});
it('should save annotation to thread if it does not exist in annotations array', () => {
thread.updateTemporaryAnnotation(tempAnnotation.id, serverAnnotation);
expect(thread.comments).toContain(serverAnnotation);
});
it('should overwrite a local annotation to the thread if it does exist as an associated annotation', () => {
thread.updateTemporaryAnnotation(tempAnnotation.id, serverAnnotation);
expect(thread.comments).not.toContain(tempAnnotation);
expect(thread.comments).toContain(serverAnnotation);
});
it('should emit an annotationsaved event on success', () => {
thread.threadNumber = undefined;
thread.updateTemporaryAnnotation(tempAnnotation.id, serverAnnotation);
expect(thread.emit).toBeCalledWith(THREAD_EVENT.save);
});
it('should only render popover on desktop', () => {
thread.updateTemporaryAnnotation(tempAnnotation.id, serverAnnotation);
expect(thread.renderAnnotationPopover).toBeCalled();
expect(thread.state).toEqual(STATES.inactive);
});
it('should only render popover on mobile', () => {
util.shouldDisplayMobileUI = jest.fn().mockReturnValue(true);
thread.updateTemporaryAnnotation(tempAnnotation.id, serverAnnotation);
expect(thread.state).toEqual(STATES.active);
expect(thread.renderAnnotationPopover).toBeCalled();
});
});
describe('delete()', () => {
let annotation;
let annotation2;
const threadPromise = Promise.resolve();
beforeEach(() => {
api = {
user: {
id: 1
},
delete: jest.fn().mockResolvedValue(threadPromise)
};
annotation = {
id: 'someID',
permissions: {
can_delete: true
},
threadID: 1
};
annotation2 = {
id: 'someID2',
permissions: {
can_delete: false
},
threadID: 1
};
thread.api = api;
thread.comments = [annotation];
util.isPlainHighlight = jest.fn();
thread.cancelFirstComment = jest.fn();
thread.destroy = jest.fn();
thread.renderAnnotationPopover = jest.fn();
thread.getThreadEventData = jest.fn().mockReturnValue({
threadNumber: 1
});
thread.emit = jest.fn();
});
it('should destroy the thread if the deleted annotation was the last annotation in the thread', () => {
const promise = thread.delete('someID', false);
promise.then(() => {
threadPromise.then(() => {
expect(thread.destroy).toBeCalled();
expect(thread.renderAnnotationPopover).not.toBeCalled();
});
});
});
it('should remove the relevant annotation from its dialog if the deleted annotation was not the last one', () => {
thread.comments.push(annotation2);
const promise = thread.delete('someID', false);
promise.then(() => {
expect(thread.renderAnnotationPopover).toBeCalled();
});
});
it('should make a server call to delete an annotation with the specified ID if useServer is true', () => {
thread.comments.push(annotation2);
const promise = thread.delete('someID', true);
promise.then(() => {
expect(thread.emit).not.toBeCalledWith(THREAD_EVENT.threadCleanup);
expect(api.delete).toBeCalledWith('someID');
});
});
it('should also delete blank highlight comment from the server when removing the last comment on a highlight thread', () => {
annotation2.permissions.can_delete = false;
thread.comments.push(annotation2);
util.isPlainHighlight = jest.fn().mockReturnValue(true);
const promise = thread.delete('someID', true);
promise.then(() => {
expect(api.delete).toBeCalledWith('someID');
});
});
it('should not make a server call to delete an annotation with the specified ID if useServer is false', () => {
const promise = thread.delete('someID', false);
promise.then(() => {
expect(api.delete).not.toBeCalled();
});
});
it('should broadcast an error if there was an error deleting from server', () => {
api.delete = jest.fn().mockRejectedValue();
thread.api = api;
const promise = thread.delete('someID', true);
promise.catch(() => {
expect(api.delete).toBeCalled();
});
});
it('should toggle highlight dialogs with the delete of the last comment if user does not have permission to delete the entire annotation', () => {
thread.comments.push(annotation2);
util.isPlainHighlight = jest.fn().mockReturnValue(true);
const promise = thread.delete('someID');
promise.then(() => {
expect(thread.cancelFirstComment).toBeCalled();
expect(thread.destroy).not.toBeCalled();
});
});
it('should destroy the annotation with the delete of the last comment if the user has permissions', () => {
annotation2.permissions.can_delete = true;
thread.comments.push(annotation2);
util.isPlainHighlight = jest.fn().mockReturnValue(true);
const promise = thread.delete('someID');
promise.then(() => {
expect(thread.emit).toBeCalledWith(THREAD_EVENT.threadCleanup);
expect(thread.emit).toBeCalledWith(THREAD_EVENT.delete);
expect(thread.cancelFirstComment).not.toBeCalled();
expect(thread.destroy).toBeCalled();
});
});
});
describe('cleanupAnnotationOnDelete()', () => {
let comment;
beforeEach(() => {
comment = {
id: '123'
};
thread.comments = [comment];
thread.threadID = '123abc';
thread.renderAnnotationPopover = jest.fn();
thread.canDelete = false;
});
it('should delete the appropriate comment', () => {
thread.cleanupAnnotationOnDelete(comment.id);
expect(thread.comments.length).toEqual(0);
});
it('should not destroy the thread if the user does not have canDelete permissions', () => {
thread.cleanupAnnotationOnDelete(comment.id);
expect(thread.threadID).not.toBeNull();
});
it('should destroy the the thread if the comment was the last one in the annotation', () => {
thread.canDelete = true;
thread.cleanupAnnotationOnDelete(comment.id);
expect(thread.threadID).toBeNull();
});
it('should re-render the popover if the comment was NOTthe last one in the annotation', () => {
thread.cleanupAnnotationOnDelete(comment.id);
expect(thread.threadID).not.toBeNull();
expect(thread.renderAnnotationPopover).toBeCalled();
});
});
describe('deleteSuccessHAndler()', () => {
beforeEach(() => {
thread.renderAnnotationPopover = jest.fn();
thread.destroy = jest.fn();
thread.emit = jest.fn();
});
it('should re-render the popover if the thread is still valid', () => {
thread.threadID = '123';
thread.deleteSuccessHandler();
expect(thread.renderAnnotationPopover).toBeCalled();
expect(thread.emit).not.toBeCalledWith(THREAD_EVENT.delete);
expect(thread.destroy).not.toBeCalled();
});
it('should properly destroy the thread if the thread should be completely deleted', () => {
thread.threadID = null;
thread.deleteSuccessHandler();
expect(thread.renderAnnotationPopover).not.toBeCalled();
expect(thread.emit).toBeCalledWith(THREAD_EVENT.delete);
expect(thread.destroy).toBeCalled();
});
});
describe('scrollIntoView()', () => {
it('should scroll to annotation page and center annotation in viewport', () => {
thread.scrollToPage = jest.fn();
thread.centerAnnotation = jest.fn();
thread.scrollIntoView();
expect(thread.scrollToPage);
expect(thread.centerAnnotation).toBeCalledWith(expect.any(Number));
});
});
describe('scrollToPage()', () => {
let pageEl;
beforeEach(() => {
pageEl = {
scrollIntoView: jest.fn()
};
thread.getPopoverParent = jest.fn().mockReturnValue(pageEl);
});
it('should do nothing if annotation does not have a location or page', () => {
thread.location = {};
thread.scrollToPage();
thread.location = null;
thread.scrollToPage();
expect(pageEl.scrollIntoView).not.toBeCalled();
});
it('should scroll annotation\'s page into view', () => {
thread.location = {
page: 1
};
thread.scrollToPage();
expect(pageEl.scrollIntoView).toBeCalled();
});
});
describe('centerAnnotation', () => {
beforeEach(() => {
thread.annotatedElement = {
scrollHeight: 100,
scrollTop: 0,
scrollBottom: 200
};
});
it('should scroll so annotation is vertically centered in viewport', () => {
thread.centerAnnotation(50);
expect(thread.annotatedElement.scrollTop).toEqual(50);
});
it('should scroll so annotation is vertically centered in viewport', () => {
thread.centerAnnotation(150);
expect(thread.annotatedElement.scrollTop).toEqual(200);
});
});
describe('location()', () => {
it('should get location', () => {
expect(thread.location).toEqual(thread.location);
});
});
describe('threadID()', () => {
it('should get threadID', () => {
expect(thread.threadID).toEqual(thread.threadID);
});
});
describe('thread()', () => {
it('should get thread', () => {
expect(thread.thread).toEqual(thread.thread);
});
});
describe('type()', () => {
it('should get type', () => {
expect(thread.type).toEqual(thread.type);
});
});
describe('state()', () => {
it('should get state', () => {
expect(thread.state).toEqual(thread.state);
});
});
describe('setup()', () => {
beforeEach(() => {
thread.setupElement = jest.fn();
});
it('should set state to pending for unsaved annotations', () => {
thread.threadNumber = undefined;
thread.setup();
expect(thread.state).toEqual(STATES.pending);
});
it('should set state to inactive for saved annotations', () => {
thread.threadNumber = 1;
thread.setup();
expect(thread.state).toEqual(STATES.inactive);
});
});
describe('setupElement()', () => {
it('should create element and bind listeners', () => {
thread.bindDOMListeners = jest.fn();
thread.setupElement();
expect(thread.element instanceof HTMLElement).toBeTruthy();
expect(thread.element.classList).toContain(CLASS_ANNOTATION_POINT_MARKER);
expect(thread.bindDOMListeners).toBeCalled();
});
});
describe('bindDOMListeners()', () => {
beforeEach(() => {
thread.element = document.createElement('div');
thread.element.addEventListener = jest.fn();
});
it('should bind DOM listeners', () => {
thread.bindDOMListeners();
expect(thread.element.addEventListener).toBeCalledWith('click', expect.any(Function));
});
it('should not add mouseleave listener for mobile browsers', () => {
util.shouldDisplayMobileUI = jest.fn().mockReturnValue(true);
thread.bindDOMListeners();
expect(thread.element.addEventListener).toBeCalledWith('click', expect.any(Function));
});
});
describe('unbindDOMListeners()', () => {
beforeEach(() => {
thread.element = document.createElement('div');
thread.element.removeEventListener = jest.fn();
});
it('should unbind DOM listeners', () => {
thread.unbindDOMListeners();
expect(thread.element.removeEventListener).toBeCalledWith('click', expect.any(Function));
});
it('should not add mouseleave listener for mobile browsers', () => {
util.shouldDisplayMobileUI = jest.fn().mockReturnValue(true);
thread.unbindDOMListeners();
expect(thread.element.removeEventListener).toBeCalledWith('click', expect.any(Function));
});
});
describe('cancelUnsavedAnnotation()', () => {
beforeEach(() => {
thread.destroy = jest.fn();
thread.unmountPopover = jest.fn();
});
it('should destroy thread if in a pending state', () => {
thread.state = STATES.pending;
thread.cancelUnsavedAnnotation();
expect(thread.destroy).toBeCalled();
expect(thread.emit).toBeCalledWith(THREAD_EVENT.cancel);
expect(thread.unmountPopover).not.toBeCalled();
});
it('should not destroy thread if not in a pending state', () => {
thread.state = STATES.inactive;
thread.cancelUnsavedAnnotation();
expect(thread.destroy).not.toBeCalled();
expect(thread.emit).not.toBeCalledWith(THREAD_EVENT.cancel);
expect(thread.unmountPopover).toBeCalled();
});
});
describe('getThreadEventData()', () => {
it('should return thread type and threadID', () => {
thread.api.user = {
id: -1
};
thread.threadNumber = undefined;
const data = thread.getThreadEventData();
expect(data).toStrictEqual({
type: thread.type,
threadID: thread.threadID
});
});
it('should also return annotator\'s user id', () => {
thread.api.user = {
id: 1
};
thread.threadNumber = undefined;
const data = thread.getThreadEventData();
expect(data).toStrictEqual({
type: thread.type,
threadID: thread.threadID,
userId: 1
});
});
it('should return thread type and threadID', () => {
thread.api.user = {
id: -1
};
thread.threadNumber = 1;
const data = thread.getThreadEventData();
expect(data).toStrictEqual({
type: thread.type,
threadID: thread.threadID,
threadNumber: 1
});
});
});
describe('createElement()', () => {
it('should create an element with the right class and attribute', () => {
const element = thread.createElement();
expect(element.classList).toContain(CLASS_ANNOTATION_POINT_MARKER);
expect(element.dataset.type).toEqual(DATA_TYPE_ANNOTATION_INDICATOR);
});
});
describe('createAnnotation()', () => {
it('should create a new point annotation', () => {
thread.save = jest.fn();
thread.createAnnotation('bleh');
expect(thread.save).toBeCalledWith(TYPES.point, 'bleh');
});
});
describe('regenerateBoundary()', () => {
it('should do nothing if a valid location does not exist', () => {
thread.location = undefined;
thread.regenerateBoundary();
thread.location = {};
thread.regenerateBoundary();
thread.location = {
x: 'something'
};
thread.regenerateBoundary();
thread.location = {
y: 'something'
};
thread.regenerateBoundary();
expect(thread.minX).toBeUndefined();
expect(thread.minY).toBeUndefined();
});
it('should set the min/max x/y values to the thread location', () => {
thread.location = {
x: 1,
y: 2
};
thread.regenerateBoundary();
expect(thread.minX).toEqual(1);
expect(thread.minY).toEqual(2);
expect(thread.maxX).toEqual(1);
expect(thread.maxY).toEqual(2);
});
});
describe('handleThreadSaveError()', () => {
it('should delete temp annotation and emit event', () => {
thread.delete = jest.fn();
thread.handleThreadSaveError(new Error(), 1);
expect(thread.delete).toBeCalledWith({ id: 1 }, false);
expect(thread.emit).toBeCalledWith(THREAD_EVENT.createError);
});
});
});