-
Notifications
You must be signed in to change notification settings - Fork 44
/
CreateHighlightDialog-test.js
166 lines (142 loc) · 5.75 KB
/
CreateHighlightDialog-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
/* eslint-disable no-unused-expressions */
import CreateHighlightDialog from '../CreateHighlightDialog';
import { CREATE_EVENT, TYPES } from '../../constants';
import * as util from '../../util';
import * as docUtil from '../docUtil';
const html = '<div class="ba-create-highlight-dialog-container"></div>';
describe('doc/CreateHighlightDialog', () => {
let rootElement;
let dialog;
let parentEl;
const localized = {
highlightToggle: 'highlight toggle',
highlightComment: 'highlight comment'
};
beforeEach(() => {
rootElement = document.createElement('div');
rootElement.innerHTML = html;
document.body.appendChild(rootElement);
parentEl = document.createElement('div');
parentEl.classList.add('ba-create-dialog-container');
dialog = new CreateHighlightDialog(parentEl, {
allowHighlight: true,
allowComment: true,
localized
});
dialog.annotatedElement = rootElement;
dialog.renderAnnotationPopover = jest.fn();
dialog.unmountPopover = jest.fn();
dialog.emit = jest.fn();
util.shouldDisplayMobileUI = jest.fn().mockReturnValue(false);
});
afterEach(() => {
document.body.removeChild(rootElement);
if (typeof dialog.destroy === 'function') {
dialog.destroy();
dialog = null;
}
parentEl = null;
});
describe('contructor()', () => {
it('should default to disable highlights and comments if no config passed in', () => {
const instance = new CreateHighlightDialog(document.createElement('div'));
expect(instance.allowHighlight).toBeFalsy();
expect(instance.allowComment).toBeFalsy();
});
it('should take config falsey value to disable highlights and comments, when passed in', () => {
const config = {
allowHighlight: true,
allowComment: false,
localized
};
const instance = new CreateHighlightDialog(document.createElement('div'), config);
expect(instance.allowHighlight).toBeTruthy();
expect(instance.allowComment).toBeFalsy();
});
});
describe('show()', () => {
const selection = { anchorNode: {} };
const pageInfo = { page: 1, pageEl: {} };
beforeEach(() => {
dialog.pageInfo = null;
dialog.setPosition = jest.fn();
dialog.renderAnnotationPopover = jest.fn();
util.getPageInfo = jest.fn().mockReturnValue(pageInfo);
});
it('should do nothing if no selection is passed in', () => {
dialog.show();
expect(dialog.renderAnnotationPopover).not.toBeCalled();
});
it('should set the position and render the popover on desktop', () => {
dialog.show(selection);
expect(dialog.pageInfo).toEqual(pageInfo);
expect(dialog.setPosition).toBeCalled();
expect(dialog.renderAnnotationPopover).toBeCalled();
});
it('should only render the popover on mobile', () => {
util.shouldDisplayMobileUI = jest.fn().mockReturnValue(true);
dialog.show(selection);
expect(dialog.pageInfo).toEqual(pageInfo);
expect(dialog.setPosition).not.toBeCalled();
expect(dialog.renderAnnotationPopover).toBeCalled();
});
});
describe('setPosition()', () => {
const selection = {
getRangeAt: jest.fn(),
rangeCount: 1
};
const pageEl = {
getBoundingClientRect: jest.fn().mockReturnValue({
left: 1,
top: 2
})
};
beforeEach(() => {
util.getPageInfo = jest.fn();
dialog.updatePosition = jest.fn();
docUtil.getDialogCoordsFromRange = jest.fn();
});
it('should do nothing if the selection is invalid', () => {
selection.rangeCount = undefined;
dialog.setPosition(selection);
expect(selection.getRangeAt).not.toBeCalled();
});
it('should do nothing if no page element is calculated from the selection', () => {
util.getPageInfo = jest.fn().mockReturnValue({});
dialog.setPosition(selection);
expect(pageEl.getBoundingClientRect).not.toBeCalled();
});
});
describe('updatePosition()', () => {
beforeEach(() => {
util.showElement = jest.fn();
dialog.pageInfo = { page: 1, pageEl: document.createElement('div') };
util.findElement = jest.fn().mockReturnValue(rootElement);
});
it('should do nothing on mobile devices', () => {
util.shouldDisplayMobileUI = jest.fn().mockReturnValue(true);
dialog.updatePosition();
expect(util.findElement).not.toBeCalled();
});
it('should update the positioning of the popover element', () => {
const x = 50;
util.repositionCaret = jest.fn().mockReturnValue(x);
dialog.updatePosition();
expect(util.findElement).toBeCalled();
});
});
describe('onCreate()', () => {
it('should invoke the "plain" highlight event', () => {
dialog.onCreate(TYPES.highlight);
expect(dialog.emit).toBeCalledWith(CREATE_EVENT.plain);
});
});
describe('onCommentClick()', () => {
it('should create a plain highlight and render the popover', () => {
dialog.onCommentClick({ preventDefault: jest.fn(), stopPropagation: jest.fn() });
expect(dialog.emit).toBeCalledWith(CREATE_EVENT.comment);
expect(dialog.renderAnnotationPopover).toBeCalled();
});
});
});