Skip to content

Commit

Permalink
Fix test-linker-manager.js.
Browse files Browse the repository at this point in the history
  • Loading branch information
William Chou committed Dec 3, 2018
1 parent 965515f commit 3744b93
Showing 1 changed file with 45 additions and 32 deletions.
77 changes: 45 additions & 32 deletions extensions/amp-analytics/0.1/test/test-linker-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ describes.realWin('Linker Manager', {amp: true}, env => {
let doc;
let windowInterface;
let handlers;
let element;
let beforeSubmitStub;

beforeEach(() => {
Expand All @@ -53,6 +54,9 @@ describes.realWin('Linker Manager', {amp: true}, env => {
canonicalUrl: 'https://www.canonical.com/some/path?q=123',
});

// Note: this only tests the single ampdoc case.
element = doc.documentElement;

handlers = [];
sandbox.stub(Services, 'navigationForDoc').returns({
registerAnchorMutator: (callback, priority) => {
Expand All @@ -77,18 +81,18 @@ describes.realWin('Linker Manager', {amp: true}, env => {
},
},
},
}, null).init();
}, /* type */ null, element).init();

expect(handlers.length).to.equal(1);
});

it('does not register anchor mutator if no linkers config', () => {
new LinkerManager(ampdoc, {}, null).init();
new LinkerManager(ampdoc, {}, /* type */ null, element).init();
expect(handlers.length).to.equal(0);
});

it('does not register anchor mutator if empty linkers config', () => {
new LinkerManager(ampdoc, {linkers: {}}, null).init();
new LinkerManager(ampdoc, {linkers: {}}, /* type */ null, element).init();
expect(handlers.length).to.equal(0);
});

Expand All @@ -106,7 +110,7 @@ describes.realWin('Linker Manager', {amp: true}, env => {
},
},
},
}, null).init();
}, /* type */ null, element).init();
expect(handlers.length).to.equal(0);
});

Expand All @@ -123,7 +127,7 @@ describes.realWin('Linker Manager', {amp: true}, env => {
},
},
},
}, null).init();
}, /* type */ null, element).init();
expect(handlers.length).to.equal(0);
});

Expand All @@ -141,7 +145,7 @@ describes.realWin('Linker Manager', {amp: true}, env => {
},
},
},
}, null).init();
}, /* type */ null, element).init();
expect(handlers.length).to.equal(1);
});

Expand Down Expand Up @@ -173,7 +177,8 @@ describes.realWin('Linker Manager', {amp: true}, env => {
},
};

return new LinkerManager(ampdoc, config, null).init().then(() => {
const lm = new LinkerManager(ampdoc, config, /* type */ null, element);
return lm.init().then(() => {
expect(handlers.length).to.equal(1);
expect(clickAnchor('https://www.source.com/dest?a=1')).to.equal(
'https://www.source.com/dest' +
Expand All @@ -196,7 +201,8 @@ describes.realWin('Linker Manager', {amp: true}, env => {
},
};

return new LinkerManager(ampdoc, config, null).init().then(() => {
const lm = new LinkerManager(ampdoc, config, /* type */ null, element);
return lm.init().then(() => {
expect(handlers.length).to.equal(1);
expect(clickAnchor('https://www.source.com/dest?a=1')).to.equal(
'https://www.source.com/dest?a=1'
Expand All @@ -218,7 +224,8 @@ describes.realWin('Linker Manager', {amp: true}, env => {
},
};

return new LinkerManager(ampdoc, config, null).init().then(() => {
const lm = new LinkerManager(ampdoc, config, /* type */ null, element);
return lm.init().then(() => {
clock.tick(1000 * 60 * 5); // 5 minutes.
const linkerUrl = clickAnchor('https://www.source.com/dest?a=1');

Expand Down Expand Up @@ -249,7 +256,8 @@ describes.realWin('Linker Manager', {amp: true}, env => {
},
};

return new LinkerManager(ampdoc, config, null).init().then(() => {
const lm = new LinkerManager(ampdoc, config, /* type */ null, element);
return lm.init().then(() => {
// testLinker1 should apply to both canonical and source
// testLinker2 should not
const canonicalDomainUrl = clickAnchor('https://www.canonical.com/path');
Expand Down Expand Up @@ -291,7 +299,8 @@ describes.realWin('Linker Manager', {amp: true}, env => {
},
};

return new LinkerManager(ampdoc, config, null).init().then(() => {
const lm = new LinkerManager(ampdoc, config, /* type */ null, element);
return lm.init().then(() => {
const fooDomainUrl = clickAnchor('https://foo.com/path');
const barDomainUrl = clickAnchor('https://bar.com/path');

Expand All @@ -314,7 +323,8 @@ describes.realWin('Linker Manager', {amp: true}, env => {
},
};

return new LinkerManager(ampdoc, config, null).init().then(() => {
const lm = new LinkerManager(ampdoc, config, /* type */ null, element);
return lm.init().then(() => {
const url1 = clickAnchor('https://www.source.com/path');
const url2 = clickAnchor('https://amp.www.source.com/path');
const url3 = clickAnchor('https://canonical.com/path');
Expand Down Expand Up @@ -351,7 +361,8 @@ describes.realWin('Linker Manager', {amp: true}, env => {
},
};

return new LinkerManager(ampdoc, config, null).init().then(() => {
const lm = new LinkerManager(ampdoc, config, /* type */ null, element);
return lm.init().then(() => {
const a = clickAnchor('https://www.source.com/path');
expect(a).to.contain('testLinker1=');
expect(a).to.not.contain('testLinker2=');
Expand Down Expand Up @@ -379,7 +390,8 @@ describes.realWin('Linker Manager', {amp: true}, env => {
},
};

return new LinkerManager(ampdoc, config).init().then(() => {
const lm = new LinkerManager(ampdoc, config, /* type */ null, element);
return lm.init().then(() => {
const a = clickAnchor('https://www.source.com');
expect(a).to.not.contain('testLinker1=');
expect(a).to.contain('testLinker2=');
Expand All @@ -404,12 +416,11 @@ describes.realWin('Linker Manager', {amp: true}, env => {
},
},
};

return new LinkerManager(ampdoc, config, 'googleanalytics')
.init().then(() => {
const a = clickAnchor('https://www.source.com/path');
expect(a).to.contain('testLinker1=');
});
const lm = new LinkerManager(ampdoc, config, 'googleanalytics', element);
return lm.init().then(() => {
const a = clickAnchor('https://www.source.com/path');
expect(a).to.contain('testLinker1=');
});
});

it('should only add one linker for auto opt-in', () => {
Expand All @@ -424,8 +435,10 @@ describes.realWin('Linker Manager', {amp: true}, env => {
},
},
};
const p1 = new LinkerManager(ampdoc, config, 'googleanalytics').init();
const p2 = new LinkerManager(ampdoc, config, 'googleanalytics').init();
const p1 =
new LinkerManager(ampdoc, config, 'googleanalytics', element).init();
const p2 =
new LinkerManager(ampdoc, config, 'googleanalytics', element).init();
return Promise.all([p1, p2]).then(() => {
const a = clickAnchor('https://www.source.com/path');
expect(a).to.not.match(/(testLinker1=.*){2}/);
Expand All @@ -445,7 +458,7 @@ describes.realWin('Linker Manager', {amp: true}, env => {
},
};

new LinkerManager(ampdoc, config, 'somevendor');
new LinkerManager(ampdoc, config, 'somevendor', element);
expect(handlers.length).to.equal(0);
});

Expand All @@ -461,7 +474,7 @@ describes.realWin('Linker Manager', {amp: true}, env => {
},
};

new LinkerManager(ampdoc, config, 'googleanalytics');
new LinkerManager(ampdoc, config, 'googleanalytics', element);
expect(handlers.length).to.equal(0);
});

Expand Down Expand Up @@ -493,7 +506,7 @@ describes.realWin('Linker Manager', {amp: true}, env => {
},
};

new LinkerManager(ampdoc, config, 'googleanalytics');
new LinkerManager(ampdoc, config, 'googleanalytics', element);
expect(handlers.length).to.equal(0);
});
});
Expand Down Expand Up @@ -534,7 +547,7 @@ describes.realWin('Linker Manager', {amp: true}, env => {
},
},
},
}, null);
}, /* type */ null, element);

return linkerManager.init().then(() => {
expect(beforeSubmitStub.calledOnce).to.be.true;
Expand All @@ -554,7 +567,7 @@ describes.realWin('Linker Manager', {amp: true}, env => {
},
destinationDomains: ['www.ampproject.com'],
},
}, null);
}, /* type */ null, element);

return linkerManager.init().then(() => {
const form = createForm();
Expand Down Expand Up @@ -584,7 +597,7 @@ describes.realWin('Linker Manager', {amp: true}, env => {
},
destinationDomains: ['www.ampproject.com'],
},
}, null);
}, /* type */ null, element);

return linkerManager.init().then(() => {
const form = createForm();
Expand Down Expand Up @@ -613,7 +626,7 @@ describes.realWin('Linker Manager', {amp: true}, env => {
},
destinationDomains: ['www.ampproject.com'],
},
}, null);
}, /* type */ null, element);

return linkerManager.init().then(() => {
const form = createForm();
Expand Down Expand Up @@ -643,7 +656,7 @@ describes.realWin('Linker Manager', {amp: true}, env => {
},
destinationDomains: ['www.ampproject.com'],
},
}, null);
}, /* type */ null, element);

return linkerManager.init().then(() => {
const form = createForm();
Expand All @@ -670,7 +683,7 @@ describes.realWin('Linker Manager', {amp: true}, env => {
},
},
},
}, null);
}, /* type */ null, element);

const manager2 = new LinkerManager(ampdoc, {
linkers: {
Expand All @@ -682,7 +695,7 @@ describes.realWin('Linker Manager', {amp: true}, env => {
},
},
},
}, null);
}, /* type */ null, element);

const p1 = manager1.init();
const p2 = manager2.init();
Expand Down

0 comments on commit 3744b93

Please sign in to comment.