Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jridgewell committed Jun 5, 2017
1 parent a9ab27b commit caeada0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 49 deletions.
18 changes: 12 additions & 6 deletions extensions/amp-form/0.1/test/integration/test-integration-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,10 @@ describes.realWin('AmpForm Integration', {
() => ampForm.renderTemplatePromiseForTesting());

form.dispatchEvent(new Event('submit'));
return fetch.catch(fetchError => fetchError).then(fetchError => {
expect(fetchError.error.message).to.match(/HTTP error 500/);
return fetch.then(() => {
throw new Error('UNREACHABLE');
}, fetchError => {
expect(fetchError.message).to.match(/HTTP error 500/);
return render.then(() => {
const rendered = form.querySelector('[i-amphtml-rendered]');
expect(rendered.textContent).to.equal(
Expand Down Expand Up @@ -257,8 +259,10 @@ describes.realWin('AmpForm Integration', {
() => ampForm.renderTemplatePromiseForTesting());

form.dispatchEvent(new Event('submit'));
return fetch.catch(fetchError => fetchError).then(fetchError => {
expect(fetchError.error.message).to.match(/HTTP error 500/);
return fetch.then(() => {
throw new Error('UNREACHABLE');
}, fetchError => {
expect(fetchError.message).to.match(/HTTP error 500/);
return render.then(() => {
const rendered = form.querySelector('[i-amphtml-rendered]');
expect(rendered.textContent).to.equal(
Expand Down Expand Up @@ -294,8 +298,10 @@ describes.realWin('AmpForm Integration', {
() => form.querySelector('amp-img img'));

form.dispatchEvent(new Event('submit'));
return fetch.catch(fetchError => fetchError).then(fetchError => {
expect(fetchError.error.message).to.match(/HTTP error 500/);
return fetch.then(() => {
throw new Error('UNREACHABLE');
}, fetchError => {
expect(fetchError.message).to.match(/HTTP error 500/);

// It shouldn't have the i-amphtml-rendered attribute since no
// template was rendered.
Expand Down
62 changes: 19 additions & 43 deletions test/functional/test-impression.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,58 +93,36 @@ describe('impression', () => {
toggleExperiment(window, 'alp', true);
viewer.getParam.withArgs('click').returns('https://www.example.com');
xhr.fetchJson.returns(new Promise(resolve => {
setTimeout(() => {
resolve({
json() {
return Promise.resolve({
'location': 'test_location?gclid=654321',
});
},
});
}, 5000);
// never resolves
}));
const clock = sandbox.useFakeTimers();
const promise = new Promise(resolve => {
setTimeout(() => {
resolve();
}, 2000);
});
clock.tick(2001);
return promise.then(() => {
expect(window.location.href).to.not.contain('gclid=654321');
const href = window.location.href;
return getTrackImpressionPromise().then(() => {
throw new Error('UNREACHABLE');
}, () => {
expect(window.location.href).to.equal(href);
});
});

it('should resolve trackImpressionPromise after timeout', () => {
toggleExperiment(window, 'alp', true);
viewer.getParam.withArgs('click').returns('https://www.example.com');
xhr.fetchJson.returns(new Promise(resolve => {
setTimeout(() => {
resolve({
json() {
return Promise.resolve(null);
},
});
}, 10000);
xhr.fetchJson.returns(Promise.resolve({
json() {
return Promise.resolve(null);
},
}));
const clock = sandbox.useFakeTimers();
maybeTrackImpression(window);
return Promise.resolve().then(() => {
clock.tick(8001);
return getTrackImpressionPromise().should.be.fulfilled;
});
return getTrackImpressionPromise();
});

it('should do nothing if get empty response', () => {
toggleExperiment(window, 'alp', true);
viewer.getParam.withArgs('click').returns('https://www.example.com');
const prevHref = window.location.href;
maybeTrackImpression(window);
return Promise.resolve().then(() => {
return Promise.resolve().then(() => {
expect(window.location.href).to.equal(prevHref);
return getTrackImpressionPromise().should.be.fulfilled;
});
return getTrackImpressionPromise().then(() => {
expect(window.location.href).to.equal(prevHref);
return getTrackImpressionPromise().should.be.fulfilled;
});
});

Expand All @@ -162,13 +140,11 @@ describe('impression', () => {
const prevHref = window.location.href;
window.history.replaceState(null, '', prevHref + '?bar=foo&test=4321');
maybeTrackImpression(window);
return Promise.resolve().then(() => {
return Promise.resolve().then(() => {
expect(window.location.href).to.equal('http://localhost:9876/context.html'
+ '?bar=foo&test=4321&gclid=123456&foo=bar&example=123');
window.history.replaceState(null, '', prevHref);
return getTrackImpressionPromise().should.be.fulfilled;
});
return getTrackImpressionPromise().then(() => {
expect(window.location.href).to.equal('http://localhost:9876/context.html'
+ '?bar=foo&test=4321&gclid=123456&foo=bar&example=123');
window.history.replaceState(null, '', prevHref);
return getTrackImpressionPromise().should.be.fulfilled;
});
});
});

0 comments on commit caeada0

Please sign in to comment.