From d82b0b42b041ade8c2828b01caeb8fd102af6e0f Mon Sep 17 00:00:00 2001 From: Daniel Rozenberg Date: Thu, 12 Apr 2018 16:24:54 -0400 Subject: [PATCH 1/2] Wrap a single test in test-amp-form.js that expect a console error with allowConsoleError --- extensions/amp-form/0.1/test/test-amp-form.js | 68 ++++++++++--------- 1 file changed, 36 insertions(+), 32 deletions(-) diff --git a/extensions/amp-form/0.1/test/test-amp-form.js b/extensions/amp-form/0.1/test/test-amp-form.js index 2f165b91d8ac..f14fad321f32 100644 --- a/extensions/amp-form/0.1/test/test-amp-form.js +++ b/extensions/amp-form/0.1/test/test-amp-form.js @@ -55,13 +55,16 @@ describes.repeated('', { let sandbox; const timer = Services.timerFor(window); - function getAmpForm(form, canonical = 'https://example.com/amps.html') { + function getUnresolvedAmpForm(form, canonical = 'https://example.com/amps.html') { new AmpFormService(env.ampdoc); Services.documentInfoForDoc(env.ampdoc).canonicalUrl = canonical; cidServiceForDocForTesting(env.ampdoc); env.ampdoc.getBody().appendChild(form); - const ampForm = new AmpForm(form, 'amp-form-test-id'); - return Promise.resolve(ampForm); + return new AmpForm(form, 'amp-form-test-id'); + } + + function getAmpForm(form, canonical = 'https://example.com/amps.html') { + return Promise.resolve(getUnresolvedAmpForm(form, canonical)); } function getForm(doc = document, button1 = true, button2 = false, @@ -539,36 +542,37 @@ describes.repeated('', { }); it('should allow rendering responses through inlined templates', () => { - return getAmpForm(getForm(env.win.document, true)).then(ampForm => { - const form = ampForm.form_; - // Add a div[submit-error] with a template child. - const errorContainer = document.createElement('div'); - errorContainer.setAttribute('submit-error', ''); - form.appendChild(errorContainer); - const errorTemplate = document.createElement('template'); - errorTemplate.setAttribute('type', 'amp-mustache'); - errorTemplate.content.appendChild( - document.createTextNode('Error: {{message}}')); - errorContainer.appendChild(errorTemplate); - let renderedTemplate = document.createElement('div'); - renderedTemplate.innerText = 'Error: hello there'; - sandbox.stub(ampForm.xhr_, 'fetch').returns(Promise.reject({ - response: { - status: 400, - json() { - return Promise.resolve({message: 'hello there'}); - }, + const ampForm = getUnresolvedAmpForm(getForm(env.win.document, true)); + const form = ampForm.form_; + // Add a div[submit-error] with a template child. + const errorContainer = document.createElement('div'); + errorContainer.setAttribute('submit-error', ''); + form.appendChild(errorContainer); + const errorTemplate = document.createElement('template'); + errorTemplate.setAttribute('type', 'amp-mustache'); + errorTemplate.content.appendChild( + document.createTextNode('Error: {{message}}')); + errorContainer.appendChild(errorTemplate); + let renderedTemplate = document.createElement('div'); + renderedTemplate.innerText = 'Error: hello there'; + sandbox.stub(ampForm.xhr_, 'fetch').returns(Promise.reject({ + response: { + status: 400, + json() { + return Promise.resolve({message: 'hello there'}); }, - })); - sandbox.stub(ampForm.templates_, 'findAndRenderTemplate') - .returns(Promise.resolve(renderedTemplate)); - const event = { - stopImmediatePropagation: sandbox.spy(), - target: form, - preventDefault: sandbox.spy(), - }; - ampForm.handleSubmitEvent_(event); - const findTemplateStub = ampForm.templates_.findAndRenderTemplate; + }, + })); + sandbox.stub(ampForm.templates_, 'findAndRenderTemplate') + .returns(Promise.resolve(renderedTemplate)); + const event = { + stopImmediatePropagation: sandbox.spy(), + target: form, + preventDefault: sandbox.spy(), + }; + ampForm.handleSubmitEvent_(event); + const findTemplateStub = ampForm.templates_.findAndRenderTemplate; + allowConsoleError(() => { return ampForm.xhrSubmitPromiseForTesting().then(() => { expect(findTemplateStub).to.be.called; // Template should have rendered an error From ac80e57f30eebbadc4f585bb6052096ba7c6817e Mon Sep 17 00:00:00 2001 From: Daniel Rozenberg Date: Mon, 7 May 2018 16:10:18 -0400 Subject: [PATCH 2/2] :%s/getUnresolvedAmpForm/getAmpFormSync/g --- extensions/amp-form/0.1/test/test-amp-form.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/extensions/amp-form/0.1/test/test-amp-form.js b/extensions/amp-form/0.1/test/test-amp-form.js index f14fad321f32..697336247d7f 100644 --- a/extensions/amp-form/0.1/test/test-amp-form.js +++ b/extensions/amp-form/0.1/test/test-amp-form.js @@ -55,7 +55,7 @@ describes.repeated('', { let sandbox; const timer = Services.timerFor(window); - function getUnresolvedAmpForm(form, canonical = 'https://example.com/amps.html') { + function getAmpFormSync(form, canonical = 'https://example.com/amps.html') { new AmpFormService(env.ampdoc); Services.documentInfoForDoc(env.ampdoc).canonicalUrl = canonical; cidServiceForDocForTesting(env.ampdoc); @@ -64,7 +64,7 @@ describes.repeated('', { } function getAmpForm(form, canonical = 'https://example.com/amps.html') { - return Promise.resolve(getUnresolvedAmpForm(form, canonical)); + return Promise.resolve(getAmpFormSync(form, canonical)); } function getForm(doc = document, button1 = true, button2 = false, @@ -542,7 +542,7 @@ describes.repeated('', { }); it('should allow rendering responses through inlined templates', () => { - const ampForm = getUnresolvedAmpForm(getForm(env.win.document, true)); + const ampForm = getAmpFormSync(getForm(env.win.document, true)); const form = ampForm.form_; // Add a div[submit-error] with a template child. const errorContainer = document.createElement('div');