Skip to content
Permalink
Browse files

fix(form): fix new state of fields and fieldset initialization

  • Loading branch information...
anehx authored and czosel committed Jul 3, 2019
1 parent 24b66e9 commit 03a1b7cf65f999103d00a31b9ff238f1d4f4ab10
Showing with 26 additions and 16 deletions.
  1. +0 −9 addon/lib/answer.js
  2. +9 −3 addon/lib/field.js
  3. +2 −2 addon/lib/fieldset.js
  4. +1 −2 tests/unit/lib/answer-test.js
  5. +14 −0 tests/unit/lib/field-test.js
@@ -8,7 +8,6 @@ import { decodeId } from "ember-caluma/helpers/decode-id";
import { getOwner } from "@ember/application";
import Document from "ember-caluma/lib/document";
import { parseDocument } from "ember-caluma/lib/parsers";
import { equal } from "@ember/object/computed";
import Evented from "@ember/object/evented";

/**
@@ -53,14 +52,6 @@ export default Base.extend(Evented, {
return this.raw.id ? decodeId(this.raw.id) : null;
}),

/**
* Whether the answer is new.
*
* @property {Boolean} isNew
* @accessor
*/
isNew: equal("id", null),

/**
* The name of the property in which the value is stored. This depends on the
* type of the answer.
@@ -163,7 +163,13 @@ export default Base.extend(Evented, {
* @property {Boolean} isNew
* @accessor
*/
isNew: reads("answer.isNew"),
isNew: computed("answer.pk", function() {
if (!this.answer) {
return true;
}

return !this.answer.pk;
}),

/**
* The type of the question
@@ -313,7 +319,7 @@ export default Base.extend(Evented, {
`removeAnswer.answer`
);

this.answer.set("id", undefined);
this.answer.set("raw.id", undefined);
} else {
response = yield this.apollo.mutate(
{
@@ -329,7 +335,7 @@ export default Base.extend(Evented, {
`saveDocument${type}.answer`
);

this.answer.setProperties(response);
this.answer.setProperties({ raw: response });
}

return response;
@@ -4,7 +4,7 @@ import { getOwner } from "@ember/application";
import { inject as service } from "@ember/service";
import Field from "ember-caluma/lib/field";
import Form from "ember-caluma/lib/form";
import { computed } from "@ember/object";
import { computed, get } from "@ember/object";

/**
* Object that represents a combination of a document and a form
@@ -91,7 +91,7 @@ export default Base.extend({
raw: {
question,
answer: this.raw.answers.find(
answer => answer.question.slug === question.slug
answer => get(answer, "question.slug") === question.slug
)
},
fieldset: this
@@ -20,7 +20,7 @@ module("Unit | Library | answer", function(hooks) {
});

test("it computes a pk", async function(assert) {
assert.expect(4);
assert.expect(3);

const answer = Answer.create(this.owner.ownerInjection(), {
raw: {
@@ -29,7 +29,6 @@ module("Unit | Library | answer", function(hooks) {
}
});

assert.equal(answer.isNew, true);
assert.equal(answer.id, null);

answer.set("raw.id", btoa("Answer:xxxx-xxxx"));
@@ -41,6 +41,20 @@ module("Unit | Library | field", function(hooks) {
assert.equal(field.pk, "Document:xxx-xxx:Question:test-question");
});

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

const field = Field.create(this.owner.ownerInjection(), {
raw: { question: this.question, answer: this.answer }
});
const newField = Field.create(this.owner.ownerInjection(), {
raw: { question: this.question }
});

assert.equal(field.isNew, false);
assert.equal(newField.isNew, true);
});

test("can compute the question", async function(assert) {
assert.expect(2);

0 comments on commit 03a1b7c

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