Skip to content

Commit f377218

Browse files
committed
Fix some of the failing test in events and utils
1 parent 7019e13 commit f377218

File tree

2 files changed

+30
-28
lines changed

2 files changed

+30
-28
lines changed

test/UI/event.spec.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe('UI::event', function () {
1414
beforeEach(function () {
1515
svg = mockSVGContainer();
1616
text = mockTextAnnotation();
17-
17+
1818
document.body.appendChild(svg);
1919
svg.appendChild(text);
2020
svg.style.width = '100px';
@@ -45,22 +45,24 @@ describe('UI::event', function () {
4545
});
4646

4747
it('should emit an event when an annotation is clicked', function (done) {
48+
4849
simulant.fire(svg, 'click', {
49-
clientX: rect.left + 15,
50-
clientY: rect.top + 15
50+
clientX: text.getBoundingClientRect().left + 1,
51+
clientY: text.getBoundingClientRect().top + 1
5152
});
5253

5354
setTimeout(function () {
5455
equal(annotationClickSpy.calledOnce, true);
56+
console.log(annotationClickSpy.getCall(0).args[0], text)
5557
equal(annotationClickSpy.getCall(0).args[0], text);
5658
done();
5759
}, 0);
5860
});
5961

6062
it('should emit an event when an annotation is blurred', function (done) {
6163
simulant.fire(svg, 'click', {
62-
clientX: rect.left + 15,
63-
clientY: rect.top + 15
64+
clientX: text.getBoundingClientRect().left + 1,
65+
clientY: text.getBoundingClientRect().top + 1
6466
});
6567

6668
setTimeout(function () {

test/UI/utils.spec.js

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
findSVGAtPoint,
1212
findAnnotationAtPoint,
1313
pointIntersectsRect,
14-
getAnnotationRect,
14+
getOffsetAnnotationRect,
1515
scaleUp,
1616
scaleDown,
1717
getScroll,
@@ -41,12 +41,14 @@ function createPath() {
4141
let div;
4242
let svg;
4343
let text;
44+
let textSvgGroup;
4445

4546
describe('UI::utils', function () {
4647
beforeEach(function () {
4748
div = document.createElement('div');
4849
svg = mockSVGContainer();
49-
text = mockTextAnnotation();
50+
textSvgGroup = mockTextAnnotation();
51+
text = textSvgGroup.firstChild;
5052
});
5153

5254
afterEach(function () {
@@ -66,17 +68,16 @@ describe('UI::utils', function () {
6668
});
6769

6870
it('should find svg container', function () {
69-
svg.appendChild(text);
71+
svg.appendChild(textSvgGroup);
7072

71-
equal(findSVGContainer(text), svg);
73+
equal(findSVGContainer(textSvgGroup), svg);
7274
});
7375

7476
it('should find svg at point', function () {
7577
svg.style.width = '10px';
7678
svg.style.height = '10px';
7779
document.body.appendChild(svg);
7880
let rect = svg.getBoundingClientRect();
79-
8081
equal(findSVGAtPoint(rect.left, rect.top), svg);
8182
equal(findSVGAtPoint(rect.left + rect.width, rect.top + rect.height), svg);
8283
equal(findSVGAtPoint(rect.left - 1, rect.top - 1), null);
@@ -85,20 +86,18 @@ describe('UI::utils', function () {
8586

8687
it('should find annotation at point', function () {
8788
text.setAttribute('data-pdf-annotate-type', 'text');
88-
svg.appendChild(text);
89+
svg.appendChild(textSvgGroup);
8990
document.body.appendChild(svg);
9091

9192
let rect = svg.getBoundingClientRect();
9293
let textRect = text.getBoundingClientRect();
9394
let textW = textRect.width;
9495
let textH = textRect.height;
9596
let textX = parseInt(text.getAttribute('x'), 10);
96-
let textY = parseInt(text.getAttribute('y'), 10) - textH; // NOTE this needs to be done to account for how text is rendered
97+
let textY = parseInt(text.getAttribute('y'), 10);
9798

98-
equal(findAnnotationAtPoint(rect.left + textX, rect.top + textY), text);
99-
equal(findAnnotationAtPoint(rect.left + textX + textW, rect.top + textY + textH), text);
100-
equal(findAnnotationAtPoint(rect.left + textX - 1, rect.top + textY - 1), null);
101-
equal(findAnnotationAtPoint(rect.left + textX + textW + 1, rect.top + textY + textH + 1), null);
99+
equal(findAnnotationAtPoint(textRect.left + 1, textRect.top + 1), text);
100+
equal(findAnnotationAtPoint(textRect.right + 1, textRect.bottom + 1), null);
102101
});
103102

104103
it('should detect if a rect collides with points', function () {
@@ -163,18 +162,19 @@ describe('UI::utils', function () {
163162
});
164163

165164
it('should get the size of text', function () {
166-
svg.appendChild(text);
165+
svg.appendChild(textSvgGroup);
167166
document.body.appendChild(svg);
168167

169-
let rect = text.getBoundingClientRect();
168+
let rect = textSvgGroup.getBoundingClientRect();
169+
let svgRect = svg.getBoundingClientRect()
170170

171-
deepEqual(getAnnotationRect(text), {
171+
deepEqual(getOffsetAnnotationRect(text), {
172172
width: rect.width,
173173
height: rect.height,
174-
left: parseInt(text.getAttribute('x'), 10),
175-
top: parseInt(text.getAttribute('y'), 10) - rect.height,
176-
right: parseInt(text.getAttribute('x'), 10) + rect.width,
177-
bottom: parseInt(text.getAttribute('y'), 10)
174+
left: rect.left - svgRect.left,
175+
top: rect.top - svgRect.top,
176+
right: rect.right - svgRect.left,
177+
bottom: rect.bottom - svgRect.top
178178
});
179179
});
180180

@@ -195,7 +195,7 @@ describe('UI::utils', function () {
195195

196196
svg.appendChild(rect);
197197

198-
deepEqual(getAnnotationRect(rect.children[0]), {
198+
deepEqual(getOffsetAnnotationRect(rect.children[0]), {
199199
width: parseInt(rect.children[0].getAttribute('width'), 10),
200200
height: parseInt(rect.children[0].getAttribute('height'), 10),
201201
left: parseInt(rect.children[0].getAttribute('x'), 10),
@@ -205,7 +205,7 @@ describe('UI::utils', function () {
205205
});
206206
});
207207
});
208-
208+
209209
it('should get the size of a rectangle', function () {
210210
document.body.appendChild(svg);
211211
let rect = renderRect({
@@ -236,7 +236,7 @@ describe('UI::utils', function () {
236236
rect.setAttribute('data-pdf-annotate-id', 'ann-foo');
237237
svg.appendChild(rect);
238238

239-
let size = getAnnotationRect(rect);
239+
let size = getOffsetAnnotationRect(rect);
240240

241241
equal(size.left, 53);
242242
equal(size.top, 103);
@@ -270,7 +270,7 @@ describe('UI::utils', function () {
270270
equal(rect.width, 300);
271271
equal(rect.height, 300);
272272
});
273-
273+
274274
it('should scale down', function () {
275275
svg.setAttribute('data-pdf-annotate-viewport', JSON.stringify(mockViewport(undefined, undefined, 1.5)));
276276
let rect = scaleDown(svg, {top: 150, left: 150, width: 300, height: 300});
@@ -309,7 +309,7 @@ describe('UI::utils', function () {
309309

310310
equal(document.head.querySelector('style[data-pdf-annotate-user-select]'), null);
311311
});
312-
312+
313313
it('should get metadata', function () {
314314
let {
315315
documentId,

0 commit comments

Comments
 (0)