-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathtest-amp-skimlinks.js
262 lines (241 loc) · 9.85 KB
/
test-amp-skimlinks.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
/**
* Copyright 2019 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {BrowserController, RequestBank} from '../../testing/test-helper';
import {PLATFORM_NAME} from '../../extensions/amp-skimlinks/0.1/constants';
import {parseQueryString} from '../../src/url';
// Create fake test urls to replace skimlinks API urls.
// RequestBank allow us to check if an API request has been made
// or not by calling RequestBank.withdraw later.
const pageTrackingUrl =
RequestBank.getUrl('pageTrackingUrl') + '/track.php?data=${data}';
const linksTrackingUrl =
RequestBank.getUrl('linksTrackingUrl') + '/link?data=${data}';
const nonAffiliateTrackingUrl =
RequestBank.getUrl('nonAffiliateTrackingUrl') + '?call=track&data=${data}';
const waypointUrl = `${RequestBank.getUrl('waypointUrl')}/`;
// Simulated click event created by browser.click() does not trigger
// the browser navigation when dispatched on a link.
// Using MouseEvent("click") instead of Event("click") does,
// which is what we need for some tests.
function clickLinkAndNavigate_(doc, selector) {
const element = doc.querySelector(selector);
if (element) {
const clickEvent = new MouseEvent('click', {bubbles: true});
element.dispatchEvent(clickEvent);
}
}
// TODO(@slocka) Disabled due to #22154, re-enable
describe.skip('amp-skimlinks', function() {
const setupBasic = {
extensions: ['amp-skimlinks'],
body: `
<amp-skimlinks
layout="nodisplay"
publisher-code="123X123"
tracking="true"
custom-redirect-domain="${waypointUrl}"
>
<script type="application/json">
{
"pageTrackingUrl": "${pageTrackingUrl}",
"linksTrackingUrl": "${linksTrackingUrl}",
"nonAffiliateTrackingUrl": "${nonAffiliateTrackingUrl}"
}
</script>
</amp-skimlinks>
<div>
<a id="merchant-link" href="https://nordstrom.com"> Test Merchant </a>
<a id="non-merchant-link" href="https://google.com"> Test non-Merchant </a>
</div>
`,
};
describes.integration('Basic features', setupBasic, env => {
let browser = null;
let clickLinkAndNavigate = null;
beforeEach(() => {
clickLinkAndNavigate = selector => {
return clickLinkAndNavigate_(env.win.document, selector);
};
browser = new BrowserController(env.win);
return browser.waitForElementBuild('amp-skimlinks');
});
it('Should send the page impression tracking request', () => {
return RequestBank.withdraw('pageTrackingUrl').then(req => {
const regex = /^\/track\.php\?data=([^&]*)&?.*$/;
const match = regex.exec(req.url);
expect(match).to.have.lengthOf(2);
const data = JSON.parse(decodeURIComponent(match[1]));
expect(data.jv).to.equal(PLATFORM_NAME);
expect(data.pub).to.equal('123X123');
// nonblocking.io is the default canonical url.
expect(data.pag).to.equal('http://nonblocking.io/');
expect(data.uuid).to.have.lengthOf(32);
});
});
it('Should send the links impression tracking request', () => {
return RequestBank.withdraw('linksTrackingUrl').then(req => {
const regex = /^\/link\?data=([^&]*)&?.*$/;
const match = regex.exec(req.url);
expect(match).to.have.lengthOf(2);
const data = JSON.parse(decodeURIComponent(match[1]));
expect(data.jv).to.equal(PLATFORM_NAME);
expect(data.pub).to.equal('123X123');
// nonblocking.io is the default canonical url.
expect(data.pag).to.equal('http://nonblocking.io/');
expect(data.uuid).to.have.lengthOf(32);
expect(data.hae).to.equal(1);
expect(data.dl).to.deep.equal({
'https://nordstrom.com/': {count: 1, ae: 1},
});
});
});
// TODO(alanorozco): Unskip on firefox
const itSkipFirefox = (desc, cb) =>
it
.configure()
.skipFirefox()
.run(desc, cb);
// TODO(alanorozco): Unskip on firefox
itSkipFirefox('should send NA-tracking on non-merchant link click ', () => {
// Give 500ms for amp-skimlinks to set up.
return browser.wait(500).then(() => {
clickLinkAndNavigate('#non-merchant-link');
return RequestBank.withdraw('nonAffiliateTrackingUrl').then(req => {
const regex = /^\/\?call=track&data=([^&]*)&?.*$/;
const match = regex.exec(req.url);
expect(match).to.have.lengthOf(2);
const data = JSON.parse(decodeURIComponent(match[1]));
expect(data.url).to.equal('https://google.com/');
expect(data.referrer).to.equal('http://nonblocking.io/');
expect(data.jv).to.equal(PLATFORM_NAME);
expect(data.uuid).to.have.lengthOf(32);
expect(data.pref).to.have.lengthOf.above(1);
});
});
});
it('Should send merchant links to waypoint on click', () => {
// Give 500ms for amp-skimlinks to set up.
return browser.wait(500).then(() => {
clickLinkAndNavigate('#merchant-link');
return RequestBank.withdraw('waypointUrl').then(req => {
// Remove "/?" in the url.
const queryString = req.url.slice(2);
const queryParams = parseQueryString(queryString);
expect(queryParams.id).to.equal('123X123');
expect(queryParams.jv).to.equal(PLATFORM_NAME);
expect(queryParams.xuuid).to.have.lengthOf(32);
expect(queryParams.url).to.equal('https://nordstrom.com/');
expect(queryParams.sref).to.equal('http://nonblocking.io/');
expect(queryParams.pref).to.have.lengthOf.above(1);
expect(queryParams.xs).to.equal('1');
});
});
});
});
const setupNoConfig = {
extensions: ['amp-skimlinks'],
body: `
<amp-skimlinks
layout="nodisplay"
publisher-code="123X123"
tracking="true"
>
</amp-skimlinks>
<div>
<a id="merchant-link" href="https://nordstrom.com"> Test Merchant </a>
<a id="non-merchant-link" href="https://google.com"> Test non-Merchant </a>
</div>
`,
};
// The purpose of these tests is to make sure that amp-skimlinks still
// works when the JSON config necessary to run our tests is not
// injected (similar to live environment).
// Since the JSON config is not set we can not use the proxy and
// therefore can only test a small subset of features.
describes.integration('Works without test config', setupNoConfig, env => {
let browser = null;
beforeEach(() => {
browser = new BrowserController(env.win);
return browser.waitForElementBuild('amp-skimlinks');
});
it('Should send merchant links to to waypoint on click', () => {
// Give 500ms for amp-skimlinks to set up.
return browser.wait(500).then(() => {
browser.click('#merchant-link');
const link = env.win.document.querySelector('#merchant-link');
const regex = /^https\:\/\/go\.skimresources\.com\/\?(.*)$/;
const match = regex.exec(link.href);
expect(match).to.be.lengthOf(2);
const queryParams = parseQueryString(match[1]);
expect(queryParams.id).to.equal('123X123');
expect(queryParams.jv).to.equal(PLATFORM_NAME);
expect(queryParams.xuuid).to.have.lengthOf(32);
expect(queryParams.url).to.equal('https://nordstrom.com/');
expect(queryParams.sref).to.equal('http://nonblocking.io/');
expect(queryParams.pref).to.have.lengthOf.above(1);
expect(queryParams.xs).to.equal('1');
});
});
});
const setupUnknownLinks = {
extensions: ['amp-skimlinks'],
body: `
<amp-skimlinks
layout="nodisplay"
publisher-code="123X123"
tracking="true"
>
<script type="application/json">
{
"beaconUrl": "http://deelay.me/2000/fakeBeacon"
}
</script>
</amp-skimlinks>
<div>
<a id="unknown-link" href="https://google.com"> Test unknown link </a>
</div>
`,
};
describes.integration('Affiliate unknown links', setupUnknownLinks, env => {
let browser = null;
beforeEach(() => {
browser = new BrowserController(env.win);
return browser.waitForElementBuild('amp-skimlinks');
});
it('Should send unknown links to waypoint', () => {
// Give 500ms for amp-skimlinks to set up.
return browser.wait(500).then(() => {
// beacon API url has been overwritten by a deelay.me URL that will keep
// the request pending during 2s. When the click happens, beacon API has
// not come back yet with affiliated domain information and the link is
// still considered as unknown.
browser.click('#unknown-link');
const link = env.win.document.querySelector('#unknown-link');
const regex = /^https\:\/\/go\.skimresources\.com\/\?(.*)$/;
const match = regex.exec(link.href);
expect(match).to.be.lengthOf(2);
const queryParams = parseQueryString(match[1]);
expect(queryParams.id).to.equal('123X123');
expect(queryParams.jv).to.equal(PLATFORM_NAME);
expect(queryParams.xuuid).to.have.lengthOf(32);
expect(queryParams.url).to.equal('https://google.com/');
expect(queryParams.sref).to.equal('http://nonblocking.io/');
expect(queryParams.pref).to.have.lengthOf.above(1);
expect(queryParams.xs).to.equal('1');
});
});
});
});