Skip to content

Commit

Permalink
fix: exclude unchecked checkboxes from login form submissions (#8068) (
Browse files Browse the repository at this point in the history
  • Loading branch information
vursen authored Nov 1, 2024
1 parent 073c99a commit 65901a4
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 8 deletions.
1 change: 1 addition & 0 deletions packages/login/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
},
"devDependencies": {
"@esm-bundle/chai": "^4.3.4",
"@vaadin/checkbox": "~24.4.12",
"@vaadin/testing-helpers": "^0.6.0",
"sinon": "^13.0.2"
},
Expand Down
12 changes: 12 additions & 0 deletions packages/login/src/vaadin-login-form-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
*/
import { LoginMixin } from './vaadin-login-mixin.js';

function isCheckbox(field) {
return (field.inputElement || field).type === 'checkbox';
}

/**
* @polymerMixin
* @mixes LoginMixin
Expand Down Expand Up @@ -63,6 +67,10 @@ export const LoginFormMixin = (superClass) =>
detail.custom = {};

fields.forEach((field) => {
if (isCheckbox(field) && !field.checked) {
return;
}

detail.custom[field.name] = field.value;
});
}
Expand Down Expand Up @@ -91,6 +99,10 @@ export const LoginFormMixin = (superClass) =>

if (this._customFields.length) {
this._customFields.forEach((field) => {
if (isCheckbox(field) && !field.checked) {
return;
}

formData.append(field.name, field.value);
});
}
Expand Down
1 change: 1 addition & 0 deletions packages/login/test/login-submit-lit.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import '@vaadin/checkbox/vaadin-lit-checkbox.js';
import '../src/vaadin-lit-login-form.js';
import '../src/vaadin-lit-login-overlay.js';
import './login-submit.common.js';
1 change: 1 addition & 0 deletions packages/login/test/login-submit-polymer.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import '@vaadin/checkbox';
import '../src/vaadin-login-form.js';
import '../src/vaadin-login-overlay.js';
import './login-submit.common.js';
59 changes: 51 additions & 8 deletions packages/login/test/login-submit.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,18 @@ describe('login form submit', () => {
beforeEach(async () => {
overlay = fixtureSync(`
<vaadin-login-overlay opened>
<input name="foo" value="bar" slot="custom-form-area">
<vaadin-text-field name="code" value="1234" slot="custom-form-area"></vaadin-text-field>
<input name="nativeCheckbox" type="checkbox" slot="custom-form-area">
<vaadin-checkbox name="vaadinCheckbox" slot="custom-form-area"></vaadin-checkbox>
<input name="nativeTextField" value="bar" slot="custom-form-area">
<vaadin-text-field name="vaadinTextField" value="1234" slot="custom-form-area"></vaadin-text-field>
</vaadin-login-overlay>
`);
await nextRender();
login = overlay.$.vaadinLoginForm;
});

it('should add values of fields in the custom form area to the login event detail', () => {
it('should include values of text fields in login event detail', () => {
const loginSpy = sinon.spy();

overlay.addEventListener('login', loginSpy);

const { vaadinLoginUsername } = fillUsernameAndPassword(login);
Expand All @@ -117,8 +118,38 @@ describe('login form submit', () => {
expect(loginSpy.called).to.be.true;

const { detail } = loginSpy.firstCall.args[0];
expect(detail.custom.foo).to.be.equal('bar');
expect(detail.custom.code).to.be.equal('1234');
expect(detail.custom.nativeTextField).to.be.equal('bar');
expect(detail.custom.vaadinTextField).to.be.equal('1234');
});

it('should not include values of unchecked checkboxes in login event detail', () => {
const loginSpy = sinon.spy();
overlay.addEventListener('login', loginSpy);

const { vaadinLoginUsername } = fillUsernameAndPassword(login);
enter(vaadinLoginUsername);
expect(loginSpy.called).to.be.true;

const event = loginSpy.firstCall.args[0];
expect(event.detail.custom).to.not.have.property('nativeCheckbox');
expect(event.detail.custom).to.not.have.property('vaadinCheckbox');
});

it('should include values of checked checkboxes in login event detail', () => {
const loginSpy = sinon.spy();
overlay.addEventListener('login', loginSpy);

const { vaadinLoginUsername } = fillUsernameAndPassword(login);

login.querySelector('[name=nativeCheckbox]').checked = true;
login.querySelector('[name=vaadinCheckbox]').checked = true;

enter(vaadinLoginUsername);
expect(loginSpy.called).to.be.true;

const event = loginSpy.firstCall.args[0];
expect(event.detail.custom.nativeCheckbox).to.equal('on');
expect(event.detail.custom.vaadinCheckbox).to.equal('on');
});

describe('form submit', () => {
Expand All @@ -127,8 +158,20 @@ describe('login form submit', () => {
await nextRender();
});

it('should submit values of fields in the custom form area to the native form', (done) => {
testFormSubmitValues(false, true, done, { foo: 'bar', code: '1234' });
it('should submit values of text fields in custom form area to the native form', (done) => {
testFormSubmitValues(false, true, done, { nativeTextField: 'bar', vaadinTextField: '1234' });
});

it('should submit values of checkboxes in custom form area to the native form when checked', (done) => {
login.querySelector('[name=nativeCheckbox]').checked = true;
login.querySelector('[name=vaadinCheckbox]').checked = true;

testFormSubmitValues(false, true, done, {
nativeCheckbox: 'on',
vaadinCheckbox: 'on',
nativeTextField: 'bar',
vaadinTextField: '1234',
});
});
});
});
Expand Down

0 comments on commit 65901a4

Please sign in to comment.