UNPKG

5.08 kBPlain TextView Raw
1/*
2 * Copyright (c) 2022 Snowplow Analytics Ltd, 2010 Anthon Pang
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31import F from 'lodash/fp';
32import { DockerWrapper, start, stop } from '../micro';
33
34declare var trackPageView: () => void;
35declare var findMaxX: () => number;
36declare var findMaxY: () => number;
37declare var getCurrentPageViewId: () => void;
38declare var findFirstEventForPageViewId: (id: string) => Record<string, unknown>;
39declare var findLastEventForPageViewId: (id: string) => Record<string, unknown>;
40
41describe('Activity tracking with callbacks', () => {
42 let docker: DockerWrapper;
43
44 beforeAll(async () => {
45 await browser.call(async () => {
46 return await start().then((container) => {
47 docker = container;
48 });
49 });
50 await browser.url('/index.html');
51 await browser.setCookies({ name: 'container', value: docker.url });
52 });
53
54 afterAll(async () => {
55 await browser.call(async () => {
56 return await stop(docker.container);
57 });
58 });
59
60 it('reports events on scroll', async () => {
61 await browser.url('/activity-callback.html?test1');
62 await browser.waitUntil(async () => (await $('#init').getText()) === 'true', {
63 timeout: 5000,
64 timeoutMsg: 'expected init after 5s',
65 interval: 250,
66 });
67
68 await $('#bottomRight').scrollIntoView();
69
70 await browser.waitUntil(async () => +(await $('#numEvents').getText()) >= 1, {
71 timeout: 10000,
72 timeoutMsg: 'expected > 1 event after 10s',
73 });
74 const [maxX, maxY] = await browser.execute(() => {
75 return [findMaxX(), findMaxY()];
76 });
77
78 expect(maxX).toBeGreaterThan(100);
79 expect(maxY).toBeGreaterThan(100);
80 });
81
82 it('carries pageviewid change through and resets scroll', async () => {
83 await browser.url('/activity-callback.html?test2');
84 await browser.waitUntil(async () => (await $('#init').getText()) === 'true', {
85 timeout: 5000,
86 timeoutMsg: 'expected init after 5s',
87 interval: 250,
88 });
89
90 await browser.execute(() => window.scrollTo(0, 0));
91
92 await browser.execute(() => {
93 getCurrentPageViewId();
94 });
95 const firstPageViewId = await $('#currentPageViewId').getText();
96
97 await $('#bottomRight').scrollIntoView();
98 await $('#middle').scrollIntoView();
99 await browser.waitUntil(async () => +(await $('#numEvents').getText()) >= 1, {
100 timeout: 10000,
101 timeoutMsg: 'expected > 1 event after 10s',
102 });
103
104 await browser.execute(() => {
105 trackPageView();
106 });
107 await $('#bottomRight').scrollIntoView();
108
109 await browser.waitUntil(async () => +(await $('#numEvents').getText()) > 1, {
110 timeout: 10000,
111 timeoutMsg: 'expected > 1 event after 10s',
112 });
113
114 await browser.execute(() => {
115 getCurrentPageViewId();
116 });
117 const secondPageViewId = await $('#currentPageViewId').getText();
118
119 // sanity check
120 expect(firstPageViewId).not.toEqual(secondPageViewId);
121
122 const first = await browser.execute((id) => {
123 return findFirstEventForPageViewId(id);
124 }, firstPageViewId);
125 const second = await browser.execute((id) => {
126 return findLastEventForPageViewId(id);
127 }, secondPageViewId);
128
129 const getMinXY = F.at(['minXOffset', 'minYOffset']);
130
131 // the first page view starts at 0,0
132 expect(getMinXY(first)).toEqual([0, 0]);
133
134 // but the second starts at #bottomRight and only moves as far as #middle
135 // so there is no way it can get to 0,0
136 const [secondX, secondY] = getMinXY(second);
137 expect(secondX).toBeGreaterThan(0);
138 expect(secondY).toBeGreaterThan(0);
139 });
140});