Skip to content

Commit

Permalink
amp-geo: Add API tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mdmower committed Feb 1, 2020
1 parent f34e54d commit 7ebb3bf
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
56 changes: 56 additions & 0 deletions extensions/amp-geo/0.1/test/test-amp-geo.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import {AmpGeo} from '../amp-geo';
import {GEO_IN_GROUP} from '../amp-geo-in-group';
import {Services} from '../../../../src/services';
import {urls} from '../../../../src/config';
import {user} from '../../../../src/log';
import {vsyncForTesting} from '../../../../src/service/vsync-impl';

Expand Down Expand Up @@ -63,6 +64,7 @@ describes.realWin(
let geo;
let el;
let userErrorStub;
let xhr;

beforeEach(() => {
userErrorStub = env.sandbox.stub(user(), 'error');
Expand All @@ -77,6 +79,10 @@ describes.realWin(
vsync.schedule_ = () => {
vsync.runScheduledTasks_();
};
xhr = {
fetchJson: env.sandbox.stub(),
};
env.sandbox.stub(Services, 'xhrFor').returns(xhr);

geo = new AmpGeo(el);
});
Expand All @@ -98,6 +104,16 @@ describes.realWin(
}
}

function stubFetchJson(json, success) {
if (success) {
xhr.fetchJson.resolves({
json: () => Promise.resolve(JSON.parse(json)),
});
} else {
xhr.fetchJson.rejects({status: 404});
}
}

it('should not throw or error on empty config', () => {
expect(() => {
geo.buildCallback();
Expand Down Expand Up @@ -386,6 +402,46 @@ describes.realWin(
});
});

it('should recognize country if API has valid schema', () => {
env.sandbox.stub(win.__AMP_MODE, 'localDev').value(false);
env.sandbox.stub(urls, 'geoApi').value('/geoapi');
stubFetchJson('{"country": "ca", "other": "ok"}', true);
addConfigElement('script');

geo.buildCallback();
return Services.geoForDocOrNull(el).then(geo => {
expect(userErrorStub).to.not.be.called;
expect(geo.ISOCountry).to.equal('ca');
expect(geo.isInCountryGroup('nafta')).to.equal(GEO_IN_GROUP.IN);
});
});

it('should not recognize country if API has invalid schema', () => {
env.sandbox.stub(win.__AMP_MODE, 'localDev').value(false);
env.sandbox.stub(urls, 'geoApi').value('/geoapi');
stubFetchJson('{"country": "abc"}', true);
addConfigElement('script');

geo.buildCallback();
return Services.geoForDocOrNull(el).then(geo => {
expect(userErrorStub).to.be.called;
expect(geo.ISOCountry).to.equal('unknown');
});
});

it('should not recognize country if API unreachable', () => {
env.sandbox.stub(win.__AMP_MODE, 'localDev').value(false);
env.sandbox.stub(urls, 'geoApi').value('/geoapi');
stubFetchJson(null, false);
addConfigElement('script');

geo.buildCallback();
return Services.geoForDocOrNull(el).then(geo => {
expect(userErrorStub).to.be.called;
expect(geo.ISOCountry).to.equal('unknown');
});
});

it('should throw if it has multiple script child elements', () => {
expect(() => {
addConfigElement('script');
Expand Down
2 changes: 2 additions & 0 deletions test/integration/test-configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ describe.configure().run('Configuration', function() {
config.thirdPartyUrl = 'http://bar.baz.com';
config.thirdPartyFrameRegex = /a-website\.com/;
config.errorReportingUrl = 'http://error.foo.com';
config.geoApiUrl = 'http://geo.bar.com';

return fixture.awaitEvent(AmpEvents.LOAD_START, 1).then(() => {
expect(fixture.win.AMP.config.urls.cdn).to.equal(config.cdnUrl);
Expand All @@ -47,6 +48,7 @@ describe.configure().run('Configuration', function() {
expect(fixture.win.AMP.config.urls.errorReporting).to.equal(
config.errorReportingUrl
);
expect(fixture.win.AMP.config.urls.geoApi).to.equal(config.geoApiUrl);
});
});
});

0 comments on commit 7ebb3bf

Please sign in to comment.