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

refactor!: ensure initial validation with constraints and value #4406

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions packages/email-field/src/vaadin-email-field.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export class EmailField extends TextField {
constructor() {
super();
this._setType('email');
this.pattern = '^([a-zA-Z0-9_\\.\\-+])+@[a-zA-Z0-9-.]+\\.[a-zA-Z0-9-]{2,}$';
}

/** @protected */
Expand All @@ -62,14 +63,6 @@ export class EmailField extends TextField {
this.inputElement.autocapitalize = 'off';
}
}

/** @protected */
_createConstraintsObserver() {
// NOTE: pattern needs to be set before constraints observer is initialized
this.pattern = this.pattern || '^([a-zA-Z0-9_\\.\\-+])+@[a-zA-Z0-9-.]+\\.[a-zA-Z0-9-]{2,}$';

super._createConstraintsObserver();
}
}

customElements.define('vaadin-email-field', EmailField);
45 changes: 32 additions & 13 deletions packages/email-field/test/email-field.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,25 +84,44 @@ describe('email-field', () => {
field.remove();
});

it('should not validate by default', async () => {
it('should not validate without value', async () => {
document.body.appendChild(field);
await nextRender();
expect(validateSpy.called).to.be.false;
});

it('should not validate when the field has an initial value', async () => {
field.value = 'foo@example.com';
document.body.appendChild(field);
await nextRender();
expect(validateSpy.called).to.be.false;
});
describe('with value', () => {
beforeEach(() => {
field.value = 'foo@example.com';
});

it('should not validate when the field has an initial value and invalid', async () => {
field.value = 'foo@example.com';
field.invalid = true;
document.body.appendChild(field);
await nextRender();
expect(validateSpy.called).to.be.false;
it('should validate by default', async () => {
document.body.appendChild(field);
await nextRender();
expect(validateSpy.calledOnce).to.be.true;
});

it('should validate when using a custom pattern', async () => {
field.pattern = '.+@example.com';
document.body.appendChild(field);
await nextRender();
expect(validateSpy.calledOnce).to.be.true;
});

it('should not validate when pattern is unset', async () => {
field.pattern = '';
document.body.appendChild(field);
await nextRender();
expect(validateSpy.called).to.be.false;
});

it('should not validate when pattern is unset and the field has invalid', async () => {
field.pattern = '';
field.invalid = true;
document.body.appendChild(field);
await nextRender();
expect(validateSpy.called).to.be.false;
});
});
});

Expand Down