Improve test coverage for validation on participation form (#308)

* add tests that participation form is validated
* assert that first invalid form element is focused on invalid submission attempt
This commit is contained in:
Jeldrik Hanschke 2019-11-20 14:52:23 +01:00 committed by GitHub
parent d2fcba466a
commit d9b1bf7b5a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 89 additions and 24 deletions

View file

@ -14,9 +14,10 @@
placeholder=(t "poll.input.newUserName.placeholder") placeholder=(t "poll.input.newUserName.placeholder")
property="name" property="name"
classNames="name" classNames="name"
data-test-form-element="name"
}} }}
<div class="selections"> <div class="selections">
{{#each selections as |selection index|}} {{#each selections as |selection|}}
{{#if isFreeText}} {{#if isFreeText}}
{{form.element {{form.element
controlType="text" controlType="text"
@ -47,6 +48,7 @@
property="value" property="value"
showValidationOn="change" showValidationOn="change"
useIcons=false useIcons=false
data-test-form-element=(concat "option-" selection.labelValue)
as |el| as |el|
}} }}
{{#each possibleAnswers as |possibleAnswer|}} {{#each possibleAnswers as |possibleAnswer|}}
@ -61,7 +63,7 @@
checked={{eq possibleAnswer.type el.value}} checked={{eq possibleAnswer.type el.value}}
onchange={{action (mut el.value) possibleAnswer.type}} onchange={{action (mut el.value) possibleAnswer.type}}
id={{concat el.id "_" possibleAnswer.type}} id={{concat el.id "_" possibleAnswer.type}}
name={{concat possibleAnswer.type index}} name={{selection.labelValue}}
> >
<label class="custom-control-label" for={{concat el.id "_" possibleAnswer.type}}> <label class="custom-control-label" for={{concat el.id "_" possibleAnswer.type}}>
{{possibleAnswer.label}} {{possibleAnswer.label}}

View file

@ -203,6 +203,68 @@ module('Acceptance | participate in a poll', function(hooks) {
resolveSubmission(resolveSubmissionWith); resolveSubmission(resolveSubmissionWith);
}); });
module('validation', function() {
test('shows validation errors for participation form on submit', async function(assert) {
let encryptionKey = 'abcdefghijklmnopqrstuvwxyz0123456789';
let poll = this.server.create('poll', {
encryptionKey,
});
await visit(`/poll/${poll.id}/participation?encryptionKey=${encryptionKey}`);
await click('button[type="submit"]');
assert.dom('[data-test-form-element="name"] input').hasClass('is-invalid');
assert.dom('[data-test-form-element="option-2017-12-24"] input[id$="yes"]').hasClass('is-invalid');
assert.dom('[data-test-form-element="option-2017-12-24"] input[id$="no"]').hasClass('is-invalid');
assert.dom('[data-test-form-element="option-2018-01-01"] input[id$="yes"]').hasClass('is-invalid');
assert.dom('[data-test-form-element="option-2018-01-01"] input[id$="no"]').hasClass('is-invalid');
assert.dom('[data-test-form-element="name"] input').isFocused();
assert.equal(currentRouteName(), 'poll.participation', 'invalid form prevents a transition');
});
test('does not show validation error for name if poll allows anonymous participation', async function(assert) {
let encryptionKey = 'abcdefghijklmnopqrstuvwxyz0123456789';
let poll = this.server.create('poll', {
anonymousUser: true,
encryptionKey,
});
await visit(`/poll/${poll.id}/participation?encryptionKey=${encryptionKey}`);
await click('button[type="submit"]');
assert.dom('[data-test-form-element="name"] input').hasClass('is-valid');
assert.dom('[data-test-form-element="option-2017-12-24"] input[id$="yes"]').hasClass('is-invalid');
assert.dom('[data-test-form-element="option-2017-12-24"] input[id$="no"]').hasClass('is-invalid');
assert.dom('[data-test-form-element="option-2018-01-01"] input[id$="yes"]').hasClass('is-invalid');
assert.dom('[data-test-form-element="option-2018-01-01"] input[id$="no"]').hasClass('is-invalid');
assert.dom('[data-test-form-element="option-2017-12-24"] input[id$="yes"]').isFocused();
assert.equal(currentRouteName(), 'poll.participation', 'invalid form prevents a transition');
});
test('does not show validation error for option inputs if poll does not force an answer to each option', async function(assert) {
let encryptionKey = 'abcdefghijklmnopqrstuvwxyz0123456789';
let poll = this.server.create('poll', {
encryptionKey,
forceAnswer: false
});
await visit(`/poll/${poll.id}/participation?encryptionKey=${encryptionKey}`);
await click('button[type="submit"]');
assert.dom('[data-test-form-element="name"] input').hasClass('is-invalid');
assert.dom('[data-test-form-element="option-2017-12-24"] input[id$="yes"]').hasClass('is-valid');
assert.dom('[data-test-form-element="option-2017-12-24"] input[id$="no"]').hasClass('is-valid');
assert.dom('[data-test-form-element="option-2018-01-01"] input[id$="yes"]').hasClass('is-valid');
assert.dom('[data-test-form-element="option-2018-01-01"] input[id$="no"]').hasClass('is-valid');
assert.dom('[data-test-form-element="name"] input').isFocused();
assert.equal(currentRouteName(), 'poll.participation', 'invalid form prevents a transition');
});
test('does not show validation errors while saving participation', async function(assert) { test('does not show validation errors while saving participation', async function(assert) {
let encryptionKey = 'abcdefghijklmnopqrstuvwxyz0123456789'; let encryptionKey = 'abcdefghijklmnopqrstuvwxyz0123456789';
let poll = this.server.create('poll', { let poll = this.server.create('poll', {
@ -232,4 +294,5 @@ module('Acceptance | participate in a poll', function(hooks) {
// need to resolve with a valid response cause otherwise Ember Data would throw // need to resolve with a valid response cause otherwise Ember Data would throw
resolveSubmission(resolveSubmissionWith); resolveSubmission(resolveSubmissionWith);
}); });
});
}); });