Skip to content

Commit

Permalink
fix(form): fix isNew property for empty answers
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonas Metzener authored and anehx committed Dec 27, 2019
1 parent aedf903 commit 2491dbf
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
5 changes: 5 additions & 0 deletions addon/lib/answer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { inject as service } from "@ember/service";
import { decodeId } from "ember-caluma/helpers/decode-id";
import { getOwner } from "@ember/application";
import { parseDocument } from "ember-caluma/lib/parsers";
import { isEmpty } from "@ember/utils";

/**
* Object which represents an answer in context of a field
Expand Down Expand Up @@ -40,6 +41,10 @@ export default Base.extend({
return this.raw.id ? decodeId(this.raw.id) : null;
}),

isNew: computed("uuid", "value", function() {
return !this.uuid || isEmpty(this.value);
}),

/**
* The name of the property in which the value is stored. This depends on the
* type of the answer.
Expand Down
8 changes: 5 additions & 3 deletions addon/lib/field.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Base from "ember-caluma/lib/base";
import { computed, getWithDefault, defineProperty } from "@ember/object";
import { equal, not, reads, empty } from "@ember/object/computed";
import { equal, not, reads } from "@ember/object/computed";
import { inject as service } from "@ember/service";
import { assert } from "@ember/debug";
import { getOwner } from "@ember/application";
Expand Down Expand Up @@ -178,12 +178,14 @@ export default Base.extend({
isInvalid: not("isValid"),

/**
* Whether the field is new (never saved to the backend service)
* Whether the field is new (never saved to the backend service or empty)
*
* @property {Boolean} isNew
* @accessor
*/
isNew: empty("answer.pk"),
isNew: computed("answer.isNew", function() {
return !this.answer || this.answer.isNew;
}),

/**
* The type of the question
Expand Down
16 changes: 13 additions & 3 deletions tests/unit/lib/field-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,20 @@ module("Unit | Library | field", function(hooks) {
});

test("computes isNew correctly", async function(assert) {
assert.expect(2);
assert.expect(3);

const answeredField = this.document.findField("test-question");
const unansweredField = this.document.findField("test-question-3");

assert.equal(answeredField.isNew, false);
assert.equal(unansweredField.isNew, true);

const oldValue = answeredField.value;
answeredField.set("answer.value", null); // empty value

assert.equal(answeredField.isNew, true);

assert.equal(this.document.findField("test-question").isNew, false);
assert.equal(this.document.findField("test-question-3").isNew, true);
answeredField.set("answer.value", oldValue); // reset value
});

test("can compute the question", async function(assert) {
Expand Down

0 comments on commit 2491dbf

Please sign in to comment.