-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathhelpers.js
79 lines (61 loc) · 2.07 KB
/
helpers.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
import {Services} from '#service';
import {DEFAULT_SKIM_OPTIONS} from './constants';
import {CustomEventReporterBuilder} from '../../../../src/extension-analytics';
import {AmpSkimlinks} from '../amp-skimlinks';
import {Tracking} from '../tracking';
const helpersFactory = (env) => {
const {win} = env;
return {
createAnchor(href) {
const anchor = win.document.createElement('a');
anchor.href = href;
return anchor;
},
createStubXhr(data) {
const response = {
json: () => {
return Promise.resolve(data);
},
};
return {
fetchJson: env.sandbox.stub().returns(Promise.resolve(response)),
};
},
mockServiceGetter(getterName, returnValue) {
env.sandbox.stub(Services, getterName);
Services[getterName].returns(returnValue);
},
stubCustomEventReporterBuilder() {
env.sandbox.stub(CustomEventReporterBuilder.prototype, 'track');
env.sandbox.stub(CustomEventReporterBuilder.prototype, 'build').returns({
trigger: env.sandbox.stub(),
config_: {},
});
},
createTrackingWithStubAnalytics(skimOptions) {
skimOptions = {...DEFAULT_SKIM_OPTIONS, ...skimOptions};
this.stubCustomEventReporterBuilder();
return new Tracking(env, skimOptions, 'my-page-referrer');
},
getAnalyticsUrlVars(trackingService, eventName) {
const stub = trackingService.analytics_.trigger;
expect(stub.withArgs(eventName).calledOnce).to.be.true;
const analyticsData = stub.withArgs(eventName).args[0][1];
expect(analyticsData).to.be.an('object');
return analyticsData;
},
createAmpSkimlinks(extensionAttrs) {
const el = this.createAmpSkimlinksElement(extensionAttrs);
el.getAmpDoc = () => env.ampdoc;
return new AmpSkimlinks(el);
},
createAmpSkimlinksElement(extensionAttrs) {
const el = document.createElement('amp-skimlinks');
for (const k in extensionAttrs) {
el.setAttribute(k, extensionAttrs[k]);
}
return el;
},
};
};
export default helpersFactory;