Skip to content

Commit

Permalink
Fix unit tests broken by ampproject#26687 (ampproject#27000)
Browse files Browse the repository at this point in the history
Cached meta tags feature requires stubbing AmpDoc.getMeta to ensure old
meta tags are reused in future tests.
  • Loading branch information
mdmower authored Feb 27, 2020
1 parent 7d53b76 commit 59d5517
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 31 deletions.
15 changes: 5 additions & 10 deletions test/unit/test-url-replacements.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,16 +193,7 @@ describes.sandboxed('UrlReplacements', {}, env => {
win.document.head = {
nodeType: /* element */ 1,
// Fake query selectors needed to bypass <meta> tag checks.
querySelector: selector => {
if (selector === 'meta[name="amp-link-variable-allowed-origin"]') {
return {
getAttribute: () => {
return 'https://whitelisted.com https://greylisted.com http://example.com';
},
};
}
return null;
},
querySelector: () => null,
querySelectorAll: () => [],
getRootNode() {
return win.document;
Expand All @@ -213,6 +204,10 @@ describes.sandboxed('UrlReplacements', {}, env => {
win.__AMP_SERVICES.documentInfo = null;
installDocumentInfoServiceForDoc(ampdoc);
win.ampdoc = ampdoc;
env.sandbox.stub(win.ampdoc, 'getMeta').returns({
'amp-link-variable-allowed-origin':
'https://whitelisted.com https://greylisted.com http://example.com',
});
installUrlReplacementsServiceForDoc(ampdoc);
return win;
}
Expand Down
20 changes: 6 additions & 14 deletions test/unit/test-variable-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import {
getTimingDataAsync,
} from '../../src/service/variable-source';

import {createElementWithAttributes} from '../../src/dom';

describes.fakeWin(
'VariableSource',
{
Expand Down Expand Up @@ -130,12 +128,9 @@ describes.fakeWin(
env => {
let variableSource;
beforeEach(() => {
env.win.document.head.appendChild(
createElementWithAttributes(env.win.document, 'meta', {
name: 'amp-allowed-url-macros',
content: 'ABC,ABCD,CANONICAL',
})
);
env.sandbox.stub(env.ampdoc, 'getMeta').returns({
'amp-allowed-url-macros': 'ABC,ABCD,CANONICAL',
});
variableSource = new VariableSource(env.ampdoc);
});

Expand Down Expand Up @@ -168,12 +163,9 @@ describes.fakeWin(
);

it('Should not work with empty variable whitelist', () => {
env.win.document.head.appendChild(
createElementWithAttributes(env.win.document, 'meta', {
name: 'amp-allowed-url-macros',
content: '',
})
);
env.sandbox.stub(env.ampdoc, 'getMeta').returns({
'amp-allowed-url-macros': '',
});
const variableSource = new VariableSource(env.ampdoc);

variableSource.setAsync('RANDOM', () => Promise.resolve('0.1234'));
Expand Down
10 changes: 3 additions & 7 deletions test/unit/url-expander/test-expander.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import {Expander} from '../../../src/service/url-expander/expander';
import {GlobalVariableSource} from '../../../src/service/url-replacements-impl';
import {createElementWithAttributes} from '../../../src/dom';
import {macroTask} from '../../../testing/yield';

describes.realWin(
Expand Down Expand Up @@ -100,12 +99,9 @@ describes.realWin(
};

function createExpanderWithWhitelist(whitelist, mockBindings) {
env.win.document.head.appendChild(
createElementWithAttributes(env.win.document, 'meta', {
name: 'amp-allowed-url-macros',
content: whitelist,
})
);
env.sandbox.stub(env.ampdoc, 'getMeta').returns({
'amp-allowed-url-macros': whitelist,
});

variableSource = new GlobalVariableSource(env.ampdoc);
return new Expander(variableSource, mockBindings);
Expand Down

0 comments on commit 59d5517

Please sign in to comment.