Skip to content

Commit

Permalink
Experiment-protect form custom-validations. (ampproject#5424)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkhatib authored and mityaha committed Nov 30, 2016
1 parent 3325426 commit 4af3bb1
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 23 deletions.
23 changes: 13 additions & 10 deletions extensions/amp-form/0.1/form-validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import {isExperimentOn} from '../../../src/experiments';
import {ValidationBubble} from './validation-bubble';


Expand Down Expand Up @@ -336,16 +337,18 @@ export class AsYouGoValidator extends AbstractCustomValidator {
* @return {!FormValidator}
*/
export function getFormValidator(form) {
const customValidation = form.getAttribute(
'custom-validation-reporting');

switch (customValidation) {
case CustomValidationTypes.AsYouGo:
return new AsYouGoValidator(form);
case CustomValidationTypes.ShowAllOnSubmit:
return new ShowAllOnSubmitValidator(form);
case CustomValidationTypes.ShowFirstOnSubmit:
return new ShowFirstOnSubmitValidator(form);
const win = form.ownerDocument.defaultView;
if (isExperimentOn(win, 'amp-form-custom-validations')) {
const customValidation = form.getAttribute(
'custom-validation-reporting');
switch (customValidation) {
case CustomValidationTypes.AsYouGo:
return new AsYouGoValidator(form);
case CustomValidationTypes.ShowAllOnSubmit:
return new ShowAllOnSubmitValidator(form);
case CustomValidationTypes.ShowFirstOnSubmit:
return new ShowFirstOnSubmitValidator(form);
}
}

if (isReportValiditySupported(form.ownerDocument)) {
Expand Down
34 changes: 21 additions & 13 deletions extensions/amp-form/0.1/test/test-form-validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
ShowFirstOnSubmitValidator,
} from '../form-validators';
import {ValidationBubble} from '../validation-bubble';
import {toggleExperiment} from '../../../../src/experiments';


describe('form-validators', () => {
Expand All @@ -45,6 +46,13 @@ describe('form-validators', () => {
});
}

function getTestingIframe() {
return createIframePromise().then(iframe => {
toggleExperiment(iframe.win, 'amp-form-custom-validations', true);
return iframe;
});
}

function getForm(doc = document, isCustomValidations = false) {
const form = doc.createElement('form');
form.setAttribute('method', 'POST');
Expand Down Expand Up @@ -100,7 +108,7 @@ describe('form-validators', () => {

describe('getFormValidator', () => {
it('should return default or polyfill instance', () => {
return createIframePromise().then(iframe => {
return getTestingIframe().then(iframe => {
const doc = iframe.doc;
const form = getForm(doc);
setReportValiditySupported(true);
Expand All @@ -112,7 +120,7 @@ describe('form-validators', () => {
});

it('should return custom validator instances', () => {
return createIframePromise().then(iframe => {
return getTestingIframe().then(iframe => {
const doc = iframe.doc;
const form = getForm(doc);
form.setAttribute('custom-validation-reporting', 'as-you-go');
Expand All @@ -131,7 +139,7 @@ describe('form-validators', () => {

describe('DefaultValidator', () => {
it('should reports form validity', () => {
return createIframePromise().then(iframe => {
return getTestingIframe().then(iframe => {
const doc = iframe.doc;
const form = getForm(doc);
const validator = new DefaultValidator(form);
Expand All @@ -144,7 +152,7 @@ describe('form-validators', () => {

describe('PolyfillDefaultValidator', () => {
it('should reports form validity', () => {
return createIframePromise().then(iframe => {
return getTestingIframe().then(iframe => {
const doc = iframe.doc;
const form = getForm(doc);
const validator = new PolyfillDefaultValidator(form);
Expand All @@ -159,7 +167,7 @@ describe('form-validators', () => {
});

it('should hide validation bubble onblur', () => {
return createIframePromise().then(iframe => {
return getTestingIframe().then(iframe => {
const doc = iframe.doc;
const form = getForm(doc);
const validator = new PolyfillDefaultValidator(form);
Expand All @@ -170,7 +178,7 @@ describe('form-validators', () => {
});

it('should re-validate on input if is is actively reported', () => {
return createIframePromise().then(iframe => {
return getTestingIframe().then(iframe => {
const doc = iframe.doc;
const form = getForm(doc);
const validator = new PolyfillDefaultValidator(form);
Expand Down Expand Up @@ -200,7 +208,7 @@ describe('form-validators', () => {

describe('ShowFirstOnSubmitValidator', () => {
it('should show first validation message', () => {
return createIframePromise().then(iframe => {
return getTestingIframe().then(iframe => {
const doc = iframe.doc;
const form = getForm(doc, true);
const validations = doc.querySelectorAll('[visible-when-invalid]');
Expand All @@ -214,7 +222,7 @@ describe('form-validators', () => {
});

it('should not report on interaction for non-active inputs', () => {
return createIframePromise().then(iframe => {
return getTestingIframe().then(iframe => {
const doc = iframe.doc;
const form = getForm(doc, true);
const validations = doc.querySelectorAll('[visible-when-invalid]');
Expand All @@ -233,7 +241,7 @@ describe('form-validators', () => {
});

it('should not report on interaction for non-active inputs', () => {
return createIframePromise().then(iframe => {
return getTestingIframe().then(iframe => {
const doc = iframe.doc;
const form = getForm(doc, true);
const validations = doc.querySelectorAll('[visible-when-invalid]');
Expand Down Expand Up @@ -275,7 +283,7 @@ describe('form-validators', () => {

describe('ShowAllOnSubmitValidator', () => {
it('should show validation messages for all inputs', () => {
return createIframePromise().then(iframe => {
return getTestingIframe().then(iframe => {
const doc = iframe.doc;
const form = getForm(doc, true);
const validations = doc.querySelectorAll('[visible-when-invalid]');
Expand All @@ -289,7 +297,7 @@ describe('form-validators', () => {
});

it('should not report on interaction for non-active inputs', () => {
return createIframePromise().then(iframe => {
return getTestingIframe().then(iframe => {
const doc = iframe.doc;
const form = getForm(doc, true);
const validations = doc.querySelectorAll('[visible-when-invalid]');
Expand All @@ -308,7 +316,7 @@ describe('form-validators', () => {
});

it('should re-validate and report on interaction for active inputs', () => {
return createIframePromise().then(iframe => {
return getTestingIframe().then(iframe => {
const doc = iframe.doc;
const form = getForm(doc, true);
const validations = doc.querySelectorAll('[visible-when-invalid]');
Expand Down Expand Up @@ -357,7 +365,7 @@ describe('form-validators', () => {

describe('AsYouGoValidator', () => {
it('should report validation for input on interaction', () => {
return createIframePromise().then(iframe => {
return getTestingIframe().then(iframe => {
const doc = iframe.doc;
const form = getForm(doc, true);
const validations = doc.querySelectorAll('[visible-when-invalid]');
Expand Down
6 changes: 6 additions & 0 deletions tools/experiments/experiments.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ const EXPERIMENTS = [
spec: 'https://github.com/ampproject/amphtml/issues/3343',
cleanupIssue: 'https://github.com/ampproject/amphtml/issues/3998',
},
{
id: 'amp-form-custom-validations',
name: 'AMP Form Custom Validations',
spec: 'https://github.com/ampproject/amphtml/issues/3343',
cleanupIssue: 'https://github.com/ampproject/amphtml/issues/5423',
},
{
id: 'amp-google-vrview-image',
name: 'AMP VR Viewer for images via Google VRView',
Expand Down

0 comments on commit 4af3bb1

Please sign in to comment.