Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mock variableService getMacros #26300

Merged
merged 10 commits into from
Feb 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 34 additions & 3 deletions extensions/amp-analytics/0.1/test/test-vendors-new.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import {AmpAnalytics} from '../amp-analytics';
import {AnalyticsConfig} from '../config';
import {ExpansionOptions} from '../variables';
import {ExpansionOptions, variableServiceForDoc} from '../variables';
import {IFRAME_TRANSPORTS} from '../iframe-transport-vendors';
import {
ImagePixelVerifier,
Expand All @@ -40,13 +40,18 @@ describes.realWin(
function(env) {
let win, doc;
let requestVerifier;
let elementMacros;

beforeEach(() => {
win = env.win;
doc = win.document;
const wi = mockWindowInterface(env.sandbox);
wi.getLocation.returns(win.location);
requestVerifier = new ImagePixelVerifier(wi);
elementMacros = {
'COOKIE': null,
'CONSENT_STATE': null,
};
});

describe('Should not contain iframe transport if not whitelisted', () => {
Expand Down Expand Up @@ -104,6 +109,32 @@ describes.realWin(
config = analytics.config_;
done();
});

// Have to get service after analytics element is created
const variableService = variableServiceForDoc(doc);

window.sandbox
.stub(variableService, 'getMacros')
.callsFake(function() {
// Add all the macros in amp-analytics
const merged = {...this.macros_, ...elementMacros};

// Change the resolving function
const keys = Object.keys(merged);
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
merged[key] = (...args) => {
let params = args
.filter(val => val !== undefined)
.join(',');
if (params) {
params = '(' + params + ')';
}
return `_${key.replace('$', '').toLowerCase()}${params}_`;
};
}
return /** @type {!JsonObject} */ (merged);
});
});

it('test requests', function*() {
Expand Down Expand Up @@ -161,8 +192,8 @@ describes.realWin(
// Write this out for easy copy pasting.
if (url !== val) {
throw new Error(
`Vendor ${vendor}, request ${name} doesn't match` +
`Expected value ${val}, get value ${url}`
`Vendor ${vendor}, request ${name} doesn't match. ` +
`Expected value ${val}, get value ${url}.`
);
}
expect(url).to.equal(val);
Expand Down
27 changes: 26 additions & 1 deletion extensions/amp-analytics/0.1/test/test-vendors.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import {ANALYTICS_CONFIG} from '../vendors';
import {AmpAnalytics} from '../amp-analytics';
import {ExpansionOptions} from '../variables';
import {ExpansionOptions, variableServiceForDoc} from '../variables';
//import {IFRAME_TRANSPORTS} from '../iframe-transport-vendors';
import {
ImagePixelVerifier,
Expand Down Expand Up @@ -61,13 +61,18 @@ describes.realWin.skip(
function(env) {
let win, doc;
let requestVerifier;
let elementMacros;

beforeEach(() => {
win = env.win;
doc = win.document;
const wi = mockWindowInterface(env.sandbox);
wi.getLocation.returns(win.location);
requestVerifier = new ImagePixelVerifier(wi);
elementMacros = {
'COOKIE': null,
'CONSENT_STATE': null,
};
});

function getAnalyticsTag(config, attrs) {
Expand Down Expand Up @@ -169,6 +174,26 @@ describes.realWin.skip(
analytics.buildCallback();
yield analytics.layoutCallback();

// Have to get service after analytics element is created
const variableService = variableServiceForDoc(doc);

window.sandbox
.stub(variableService, 'getMacros')
.callsFake(function() {
// Add all the macros in amp-analytics
const merged = {...this.macros_, ...elementMacros};

// Change the resolving function
const keys = Object.keys(merged);
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
merged[key] = (opt_param, opt_param2, opt_param3) => {
return `_${key.replace('$', '')}_`;
};
}
return /** @type {!JsonObject} */ (merged);
});

// Wait for event queue to clear.
yield macroTask();

Expand Down
14 changes: 7 additions & 7 deletions extensions/amp-analytics/0.1/test/vendor-requests.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"event": "//webcare.byside.com/BWA!webcareId/amp/signal.php?event_id=!eventId&event_label=!eventLabel&fields=!fields&webcare_id=!webcareId&bwch=!channel&lang=pt&fid=!fid&bwit=A&tuid=_client_id_&suid=&puid=_page_view_id_p_timestamp_&referrer=_document_referrer_&page=_source_url_&amppage=_ampdoc_url_&bwpt=_title_&bres=_viewport_width_x_viewport_height_&res=_screen_width_x_screen_height_&v=v20171116a&ampv=_amp_version_&viewer=_viewer_&ua=_user_agent_&r=_random_"
},
"captainmetrics": {
"hit": "https://photon.captainmetrics.com/amp/?cId=_client_id_&sId=W2CX8IhWFDNI_pvEhheYkaODWeJu0zIDlM5YaCtUeKAvYaEfTW5Rhxy_13DvjsZy&ts=_timestamp_&rand=_random_&pId=!projectId&ampV=_amp_version_&v=1&lang=_browser_language_&ua=_user_agent_&res=_screen_height_x_screen_width_&aRes=_available_screen_height_x_available_screen_width_&off=_timezone_&tz=_timezone_code_&lp=_source_url_&ref=_external_referrer_&ampRef=_ampdoc_url_&pageId=_page_view_id_"
"hit": "https://photon.captainmetrics.com/amp/?cId=_client_id_&sId=_hash(_page_view_id__client_id_)_&ts=_timestamp_&rand=_random_&pId=!projectId&ampV=_amp_version_&v=1&lang=_browser_language_&ua=_user_agent_&res=_screen_height_x_screen_width_&aRes=_available_screen_height_x_available_screen_width_&off=_timezone_&tz=_timezone_code_&lp=_source_url_&ref=_external_referrer_&ampRef=_ampdoc_url_&pageId=_page_view_id_"
},
"chartbeat": {
"host": "https://ping.chartbeat.net",
Expand All @@ -73,7 +73,7 @@
"comscore": {
"host": "https://sb.scorecardresearch.com",
"base": "https://sb.scorecardresearch.com/b?",
"pageview": "https://sb.scorecardresearch.com/b?c1=2&c2=1000001&cs_ucfr=&cs_amp_consent=&cs_pv=_page_view_id_&c12=_client_id_&rn=_random_&c8=_title_&c7=_canonical_url_&c9=_document_referrer_&cs_c7amp=_ampdoc_url_"
"pageview": "https://sb.scorecardresearch.com/b?c1=2&c2=1000001&cs_ucfr=_if(_equals(_consent_state_%2Csufficient)_%2C1)__if(_equals(_consent_state_%2Cinsufficient)_%2C0)__if(_equals(_consent_state_%2C)_%2C)_&cs_amp_consent=_consent_state_&cs_pv=_page_view_id_&c12=_client_id_&rn=_random_&c8=_title_&c7=_canonical_url_&c9=_document_referrer_&cs_c7amp=_ampdoc_url_"
},
"cxense": {
"host": "https://scomcluster.cxense.com",
Expand Down Expand Up @@ -412,11 +412,11 @@
"structEvent": "https://!collectorHost/i?url=_canonical_url_&page=_title_&res=_screen_width_x_screen_height_&stm=_timestamp_&tz=_timezone_code_&aid=!appId&p=web&tv=amp-0.3&cd=_screen_color_depth_&cs=_document_charset_&duid=_client_id_&lang=_browser_language_&refr=_document_referrer_&vp=_viewport_width_x_viewport_height_&ds=_scroll_width_x_scroll_height_&e=se&se_ca=!structEventCategory&se_ac=!structEventAction&se_la=!structEventLabel&se_pr=!structEventProperty&se_va=!structEventValue"
},
"snowplow_v2": {
"basePrefix": "https://!collectorHost/i?p=web&tv=amp-1.0.0&url=_ampdoc_url_&page=_title_&res=_screen_width_x_screen_height_&dtm=_timestamp_&tz=_timezone_code_&aid=!appId&cd=_screen_color_depth_&cs=_document_charset_&lang=_browser_language_&refr=_document_referrer_&vp=_viewport_width_x_viewport_height_&ua=_user_agent_&ds=_scroll_width_x_scroll_height_&uid=!userId&co=%7B%22schema%22%3A%22iglu%3Acom.snowplowanalytics.snowplow%2Fcontexts%2Fjsonschema%2F1-0-0%22%2C%22data%22%3A%5B%7B%22schema%22%3A%22iglu%3Adev.amp.snowplow%2Famp_id%2Fjsonschema%2F1-0-0%22%2C%22data%22%3A%7B%22ampClientId%22%3A%22_client_id_%22%2C%20%22domainUserid%22%3A%20%22_query_param_%22%2C%20%22userId%22%3A%20%22!userId%22%7D%7D%2C!customContexts%2C%7B%22schema%22%3A%22iglu%3Adev.amp.snowplow%2Famp_web_page%2Fjsonschema%2F1-0-0%22%2C%22data%22%3A%7B%22ampPageViewId%22%3A%22_page_view_id_64_%22%7D%7D%5D%7D",
"pageView": "https://!collectorHost/i?p=web&tv=amp-1.0.0&e=pv&url=_ampdoc_url_&page=_title_&res=_screen_width_x_screen_height_&dtm=_timestamp_&tz=_timezone_code_&aid=!appId&cd=_screen_color_depth_&cs=_document_charset_&lang=_browser_language_&refr=_document_referrer_&vp=_viewport_width_x_viewport_height_&ua=_user_agent_&ds=_scroll_width_x_scroll_height_&uid=!userId&co=%7B%22schema%22%3A%22iglu%3Acom.snowplowanalytics.snowplow%2Fcontexts%2Fjsonschema%2F1-0-0%22%2C%22data%22%3A%5B%7B%22schema%22%3A%22iglu%3Adev.amp.snowplow%2Famp_id%2Fjsonschema%2F1-0-0%22%2C%22data%22%3A%7B%22ampClientId%22%3A%22_client_id_%22%2C%20%22domainUserid%22%3A%20%22_query_param_%22%2C%20%22userId%22%3A%20%22!userId%22%7D%7D%2C!customContexts%2C%7B%22schema%22%3A%22iglu%3Adev.amp.snowplow%2Famp_web_page%2Fjsonschema%2F1-0-0%22%2C%22data%22%3A%7B%22ampPageViewId%22%3A%22_page_view_id_64_%22%7D%7D%5D%7D",
"structEvent": "https://!collectorHost/i?p=web&tv=amp-1.0.0&e=se&se_ca=!structEventCategory&se_ac=!structEventAction&se_la=!structEventLabel&se_pr=!structEventProperty&se_va=!structEventValue&url=_ampdoc_url_&page=_title_&res=_screen_width_x_screen_height_&dtm=_timestamp_&tz=_timezone_code_&aid=!appId&cd=_screen_color_depth_&cs=_document_charset_&lang=_browser_language_&refr=_document_referrer_&vp=_viewport_width_x_viewport_height_&ua=_user_agent_&ds=_scroll_width_x_scroll_height_&uid=!userId&co=%7B%22schema%22%3A%22iglu%3Acom.snowplowanalytics.snowplow%2Fcontexts%2Fjsonschema%2F1-0-0%22%2C%22data%22%3A%5B%7B%22schema%22%3A%22iglu%3Adev.amp.snowplow%2Famp_id%2Fjsonschema%2F1-0-0%22%2C%22data%22%3A%7B%22ampClientId%22%3A%22_client_id_%22%2C%20%22domainUserid%22%3A%20%22_query_param_%22%2C%20%22userId%22%3A%20%22!userId%22%7D%7D%2C!customContexts%2C%7B%22schema%22%3A%22iglu%3Adev.amp.snowplow%2Famp_web_page%2Fjsonschema%2F1-0-0%22%2C%22data%22%3A%7B%22ampPageViewId%22%3A%22_page_view_id_64_%22%7D%7D%5D%7D",
"pagePing": "https://!collectorHost/i?p=web&tv=amp-1.0.0&e=pp&pp_mix=_scroll_left_&pp_miy=_scroll_top_&url=_ampdoc_url_&page=_title_&res=_screen_width_x_screen_height_&dtm=_timestamp_&tz=_timezone_code_&aid=!appId&cd=_screen_color_depth_&cs=_document_charset_&lang=_browser_language_&refr=_document_referrer_&vp=_viewport_width_x_viewport_height_&ua=_user_agent_&ds=_scroll_width_x_scroll_height_&uid=!userId&co=%7B%22schema%22%3A%22iglu%3Acom.snowplowanalytics.snowplow%2Fcontexts%2Fjsonschema%2F1-0-0%22%2C%22data%22%3A%5B%7B%22schema%22%3A%22iglu%3Adev.amp.snowplow%2Famp_id%2Fjsonschema%2F1-0-0%22%2C%22data%22%3A%7B%22ampClientId%22%3A%22_client_id_%22%2C%20%22domainUserid%22%3A%20%22_query_param_%22%2C%20%22userId%22%3A%20%22!userId%22%7D%7D%2C!customContexts%2C%7B%22schema%22%3A%22iglu%3Adev.amp.snowplow%2Famp_web_page%2Fjsonschema%2F1-0-0%22%2C%22data%22%3A%7B%22ampPageViewId%22%3A%22_page_view_id_64_%22%7D%7D%5D%7D",
"selfDescribingEvent": "https://!collectorHost/i?p=web&tv=amp-1.0.0&e=ue&ue_pr=%7B%22schema%22%3A%22iglu%3Acom.snowplowanalytics.snowplow%2Funstruct_event%2Fjsonschema%2F1-0-0%22%2C%22data%22%3A%7B%22schema%22%3A%22iglu%3A!customEventSchemaVendor%2F!customEventSchemaName%2Fjsonschema%2F!customEventSchemaVersion%22%2C%22data%22%3A!customEventSchemaData%7D%7D&url=_ampdoc_url_&page=_title_&res=_screen_width_x_screen_height_&dtm=_timestamp_&tz=_timezone_code_&aid=!appId&cd=_screen_color_depth_&cs=_document_charset_&lang=_browser_language_&refr=_document_referrer_&vp=_viewport_width_x_viewport_height_&ua=_user_agent_&ds=_scroll_width_x_scroll_height_&uid=!userId&co=%7B%22schema%22%3A%22iglu%3Acom.snowplowanalytics.snowplow%2Fcontexts%2Fjsonschema%2F1-0-0%22%2C%22data%22%3A%5B%7B%22schema%22%3A%22iglu%3Adev.amp.snowplow%2Famp_id%2Fjsonschema%2F1-0-0%22%2C%22data%22%3A%7B%22ampClientId%22%3A%22_client_id_%22%2C%20%22domainUserid%22%3A%20%22_query_param_%22%2C%20%22userId%22%3A%20%22!userId%22%7D%7D%2C!customContexts%2C%7B%22schema%22%3A%22iglu%3Adev.amp.snowplow%2Famp_web_page%2Fjsonschema%2F1-0-0%22%2C%22data%22%3A%7B%22ampPageViewId%22%3A%22_page_view_id_64_%22%7D%7D%5D%7D"
"basePrefix": "https://!collectorHost/i?p=web&tv=amp-1.0.0&url=_ampdoc_url_&page=_title_&res=_screen_width_x_screen_height_&dtm=_timestamp_&tz=_timezone_code_&aid=!appId&cd=_screen_color_depth_&cs=_document_charset_&lang=_browser_language_&refr=_document_referrer_&vp=_viewport_width_x_viewport_height_&ua=_user_agent_&ds=_scroll_width_x_scroll_height_&uid=!userId&co=%7B%22schema%22%3A%22iglu%3Acom.snowplowanalytics.snowplow%2Fcontexts%2Fjsonschema%2F1-0-0%22%2C%22data%22%3A%5B%7B%22schema%22%3A%22iglu%3Adev.amp.snowplow%2Famp_id%2Fjsonschema%2F1-0-0%22%2C%22data%22%3A%7B%22ampClientId%22%3A%22_client_id_%22%2C%20%22domainUserid%22%3A%20%22_cookie(_sp_duid)_%22%2C%20%22userId%22%3A%20%22!userId%22%7D%7D%2C_replace(!customContexts%2C%5E%2C*(.%2B%3F)%2C*%24%2C%241%2C)_%7B%22schema%22%3A%22iglu%3Adev.amp.snowplow%2Famp_web_page%2Fjsonschema%2F1-0-0%22%2C%22data%22%3A%7B%22ampPageViewId%22%3A%22_page_view_id_64_%22%7D%7D%5D%7D",
"pageView": "https://!collectorHost/i?p=web&tv=amp-1.0.0&e=pv&url=_ampdoc_url_&page=_title_&res=_screen_width_x_screen_height_&dtm=_timestamp_&tz=_timezone_code_&aid=!appId&cd=_screen_color_depth_&cs=_document_charset_&lang=_browser_language_&refr=_document_referrer_&vp=_viewport_width_x_viewport_height_&ua=_user_agent_&ds=_scroll_width_x_scroll_height_&uid=!userId&co=%7B%22schema%22%3A%22iglu%3Acom.snowplowanalytics.snowplow%2Fcontexts%2Fjsonschema%2F1-0-0%22%2C%22data%22%3A%5B%7B%22schema%22%3A%22iglu%3Adev.amp.snowplow%2Famp_id%2Fjsonschema%2F1-0-0%22%2C%22data%22%3A%7B%22ampClientId%22%3A%22_client_id_%22%2C%20%22domainUserid%22%3A%20%22_cookie(_sp_duid)_%22%2C%20%22userId%22%3A%20%22!userId%22%7D%7D%2C_replace(!customContexts%2C%5E%2C*(.%2B%3F)%2C*%24%2C%241%2C)_%7B%22schema%22%3A%22iglu%3Adev.amp.snowplow%2Famp_web_page%2Fjsonschema%2F1-0-0%22%2C%22data%22%3A%7B%22ampPageViewId%22%3A%22_page_view_id_64_%22%7D%7D%5D%7D",
"structEvent": "https://!collectorHost/i?p=web&tv=amp-1.0.0&e=se&se_ca=!structEventCategory&se_ac=!structEventAction&se_la=!structEventLabel&se_pr=!structEventProperty&se_va=_if(!structEventValue%2C!structEventValue%2Cnull)_&url=_ampdoc_url_&page=_title_&res=_screen_width_x_screen_height_&dtm=_timestamp_&tz=_timezone_code_&aid=!appId&cd=_screen_color_depth_&cs=_document_charset_&lang=_browser_language_&refr=_document_referrer_&vp=_viewport_width_x_viewport_height_&ua=_user_agent_&ds=_scroll_width_x_scroll_height_&uid=!userId&co=%7B%22schema%22%3A%22iglu%3Acom.snowplowanalytics.snowplow%2Fcontexts%2Fjsonschema%2F1-0-0%22%2C%22data%22%3A%5B%7B%22schema%22%3A%22iglu%3Adev.amp.snowplow%2Famp_id%2Fjsonschema%2F1-0-0%22%2C%22data%22%3A%7B%22ampClientId%22%3A%22_client_id_%22%2C%20%22domainUserid%22%3A%20%22_cookie(_sp_duid)_%22%2C%20%22userId%22%3A%20%22!userId%22%7D%7D%2C_replace(!customContexts%2C%5E%2C*(.%2B%3F)%2C*%24%2C%241%2C)_%7B%22schema%22%3A%22iglu%3Adev.amp.snowplow%2Famp_web_page%2Fjsonschema%2F1-0-0%22%2C%22data%22%3A%7B%22ampPageViewId%22%3A%22_page_view_id_64_%22%7D%7D%5D%7D",
"pagePing": "https://!collectorHost/i?p=web&tv=amp-1.0.0&e=pp&pp_mix=_scroll_left_&pp_miy=_scroll_top_&url=_ampdoc_url_&page=_title_&res=_screen_width_x_screen_height_&dtm=_timestamp_&tz=_timezone_code_&aid=!appId&cd=_screen_color_depth_&cs=_document_charset_&lang=_browser_language_&refr=_document_referrer_&vp=_viewport_width_x_viewport_height_&ua=_user_agent_&ds=_scroll_width_x_scroll_height_&uid=!userId&co=%7B%22schema%22%3A%22iglu%3Acom.snowplowanalytics.snowplow%2Fcontexts%2Fjsonschema%2F1-0-0%22%2C%22data%22%3A%5B%7B%22schema%22%3A%22iglu%3Adev.amp.snowplow%2Famp_id%2Fjsonschema%2F1-0-0%22%2C%22data%22%3A%7B%22ampClientId%22%3A%22_client_id_%22%2C%20%22domainUserid%22%3A%20%22_cookie(_sp_duid)_%22%2C%20%22userId%22%3A%20%22!userId%22%7D%7D%2C_replace(!customContexts%2C%5E%2C*(.%2B%3F)%2C*%24%2C%241%2C)_%7B%22schema%22%3A%22iglu%3Adev.amp.snowplow%2Famp_web_page%2Fjsonschema%2F1-0-0%22%2C%22data%22%3A%7B%22ampPageViewId%22%3A%22_page_view_id_64_%22%7D%7D%5D%7D",
"selfDescribingEvent": "https://!collectorHost/i?p=web&tv=amp-1.0.0&e=ue&ue_pr=%7B%22schema%22%3A%22iglu%3Acom.snowplowanalytics.snowplow%2Funstruct_event%2Fjsonschema%2F1-0-0%22%2C%22data%22%3A%7B%22schema%22%3A%22iglu%3A!customEventSchemaVendor%2F!customEventSchemaName%2Fjsonschema%2F!customEventSchemaVersion%22%2C%22data%22%3A!customEventSchemaData%7D%7D&url=_ampdoc_url_&page=_title_&res=_screen_width_x_screen_height_&dtm=_timestamp_&tz=_timezone_code_&aid=!appId&cd=_screen_color_depth_&cs=_document_charset_&lang=_browser_language_&refr=_document_referrer_&vp=_viewport_width_x_viewport_height_&ua=_user_agent_&ds=_scroll_width_x_scroll_height_&uid=!userId&co=%7B%22schema%22%3A%22iglu%3Acom.snowplowanalytics.snowplow%2Fcontexts%2Fjsonschema%2F1-0-0%22%2C%22data%22%3A%5B%7B%22schema%22%3A%22iglu%3Adev.amp.snowplow%2Famp_id%2Fjsonschema%2F1-0-0%22%2C%22data%22%3A%7B%22ampClientId%22%3A%22_client_id_%22%2C%20%22domainUserid%22%3A%20%22_cookie(_sp_duid)_%22%2C%20%22userId%22%3A%20%22!userId%22%7D%7D%2C_replace(!customContexts%2C%5E%2C*(.%2B%3F)%2C*%24%2C%241%2C)_%7B%22schema%22%3A%22iglu%3Adev.amp.snowplow%2Famp_web_page%2Fjsonschema%2F1-0-0%22%2C%22data%22%3A%7B%22ampPageViewId%22%3A%22_page_view_id_64_%22%7D%7D%5D%7D"
},
"teaanalytics": {
"domain": "https://!channel/v1/amp",
Expand Down