Skip to content
Permalink
Browse files

fix(form): fix wrong answer when clicking checkboxes too fast

  • Loading branch information...
anehx committed Jun 20, 2019
1 parent 094f60c commit 8b8bc305b736c25466ad076472d15153199956d2
@@ -33,15 +33,13 @@ export default Component.extend({
"config:environment"
);

this.set("field.answer.value", value);

/* istanbul ignore next */
if (environment !== "test") {
yield timeout(500);
}

const answer = this.get("field.answer");

answer.set("value", value);

yield this.field.validate.perform();

try {
@@ -32,7 +32,7 @@ export default Component.extend({
* @param {Boolean} checked Whether the options checkbox is checked or not
*/
toggle(slug, checked) {
const value = get(this, "field.answer.listValue") || [];
const value = get(this, "field.answer.value") || [];

this.onSave([
...new Set([...value, slug].filter(v => v !== slug || checked))
@@ -7,7 +7,7 @@
type="checkbox"
name={{field.id}}
value={{option.slug}}
checked={{contains option.slug field.answer.listValue}}
checked={{contains option.slug field.answer.value}}
disabled={{disabled}}
onchange={{action (action "toggle" option.slug) value="target.checked"}}
>
@@ -129,7 +129,7 @@ module("Integration | Component | cf-field/input", function(hooks) {
__typename="MultipleChoiceQuestion"
)
answer=(hash
listValue=(array "option-1")
value=(array "option-1")
)
)
}}
@@ -14,7 +14,7 @@ module("Integration | Component | cf-field/input/checkbox", function(hooks) {
field=(hash
id="test"
answer=(hash
listValue=(array "option-1" "option-2")
value=(array "option-1" "option-2")
)
question=(hash
multipleChoiceOptions=(hash
@@ -82,15 +82,15 @@ module("Integration | Component | cf-field/input/checkbox", function(hooks) {
test("it triggers save on click", async function(assert) {
assert.expect(3);

this.set("listValue", []);
this.set("save", value => this.set("listValue", value));
this.set("value", []);
this.set("save", value => this.set("value", value));

await render(hbs`
{{cf-field/input/checkbox
onSave=save
field=(hash
answer=(hash
listValue=listValue
value=value
)
question=(hash
multipleChoiceOptions=(hash
@@ -106,12 +106,12 @@ module("Integration | Component | cf-field/input/checkbox", function(hooks) {
`);

await click("label:nth-of-type(1) input");
assert.deepEqual(this.listValue, ["option-1"]);
assert.deepEqual(this.value, ["option-1"]);

await click("label:nth-of-type(2) input");
assert.deepEqual(this.listValue, ["option-1", "option-2"]);
assert.deepEqual(this.value, ["option-1", "option-2"]);

await click("label:nth-of-type(1) input");
assert.deepEqual(this.listValue, ["option-2"]);
assert.deepEqual(this.value, ["option-2"]);
});
});

0 comments on commit 8b8bc30

Please sign in to comment.
You can’t perform that action at this time.