@guardian/ophan-tracker-js 2.2.1 → 2.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -43,6 +43,10 @@ if (typeof document.addEventListener === 'function') {
|
|
|
43
43
|
document.addEventListener('click', function (e) {
|
|
44
44
|
let anchorTarget, info;
|
|
45
45
|
anchorTarget = validAncestorAnchorElement(e.target);
|
|
46
|
+
if (!anchorTarget) {
|
|
47
|
+
// If no anchor element was clicked, exit early.
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
46
50
|
info = {
|
|
47
51
|
from: [location.protocol, '//', location.host, location.pathname].join(''),
|
|
48
52
|
to: anchorTarget ? anchorTarget.href : void 0,
|
|
@@ -1,12 +1,17 @@
|
|
|
1
1
|
import clickPathCapture, { validAncestorAnchorElement, getContainingComponent } from '../click-path-capture.js';
|
|
2
|
+
import transmit from '../transmit.js';
|
|
3
|
+
import ophan from '../core.js';
|
|
2
4
|
const { getDataLinkNames } = clickPathCapture;
|
|
3
5
|
describe('click-path-capture.js', () => {
|
|
4
6
|
beforeAll(() => {
|
|
5
7
|
document.body.innerHTML = `
|
|
6
8
|
<div data-component="component1">
|
|
7
|
-
<
|
|
8
|
-
<
|
|
9
|
-
|
|
9
|
+
<div id="testElement4">
|
|
10
|
+
<a href="#" data-link-name="link1" id="link1">
|
|
11
|
+
<span id="testElement1"></span>
|
|
12
|
+
</a>
|
|
13
|
+
<span id="nonLinkElement">Non-link element</span>
|
|
14
|
+
</div>
|
|
10
15
|
</div>
|
|
11
16
|
<div id="testElement2" data-component="component2"></div>
|
|
12
17
|
`;
|
|
@@ -41,4 +46,26 @@ describe('click-path-capture.js', () => {
|
|
|
41
46
|
expect(getDataLinkNames(element)).toEqual([]);
|
|
42
47
|
});
|
|
43
48
|
});
|
|
49
|
+
describe('click event handling', () => {
|
|
50
|
+
let transmitSendMoreSpy, ophanStoreDataToSendOnNextEventSpy;
|
|
51
|
+
beforeEach(() => {
|
|
52
|
+
transmitSendMoreSpy = jest.spyOn(transmit, 'sendMore').mockImplementation(() => { });
|
|
53
|
+
ophanStoreDataToSendOnNextEventSpy = jest.spyOn(ophan, 'storeDataToSendOnNextEvent').mockImplementation(() => { });
|
|
54
|
+
});
|
|
55
|
+
afterEach(() => {
|
|
56
|
+
jest.clearAllMocks();
|
|
57
|
+
});
|
|
58
|
+
test('should send request and store data when a link is clicked', () => {
|
|
59
|
+
const element = document.getElementById('testElement1');
|
|
60
|
+
element.click();
|
|
61
|
+
expect(transmitSendMoreSpy).toHaveBeenCalled();
|
|
62
|
+
expect(ophanStoreDataToSendOnNextEventSpy).toHaveBeenCalled();
|
|
63
|
+
});
|
|
64
|
+
test('should not send request when a non-link element within a container is clicked', () => {
|
|
65
|
+
const element = document.getElementById('testElement4');
|
|
66
|
+
element.click();
|
|
67
|
+
expect(transmitSendMoreSpy).not.toHaveBeenCalled();
|
|
68
|
+
expect(ophanStoreDataToSendOnNextEventSpy).not.toHaveBeenCalled();
|
|
69
|
+
});
|
|
70
|
+
});
|
|
44
71
|
});
|