Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrap a single test in test-amp-form.js that expect a console error with allowConsoleError #14592

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
68 changes: 36 additions & 32 deletions extensions/amp-form/0.1/test/test-amp-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,16 @@ describes.repeated('', {
let sandbox;
const timer = Services.timerFor(window);

function getAmpForm(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);
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(getAmpFormSync(form, canonical));
}

function getForm(doc = document, button1 = true, button2 = false,
Expand Down Expand Up @@ -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 = getAmpFormSync(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(() => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has the same problem I mentioned in chat. The returned promise won't be chained (unless this has been since fixed).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noted. Not yet fixed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what you're referring to

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Place a breakpoint on line 577. I believe it won't get triggered.

This is because sinon chains returned promises, but allowConsoleError() currently swallows it. What we need is for allowConsoleError(f) to detect promises returned in by f and chain them. Then, we'll do:

return allowConsoleError(() => { 
  ...
});

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in #15228. With this, you can return allowConsoleError. Improperly written tests will fail presubmit checks.

return ampForm.xhrSubmitPromiseForTesting().then(() => {
expect(findTemplateStub).to.be.called;
// Template should have rendered an error
Expand Down