diff --git a/addon/helpers/changeset.js b/addon/helpers/changeset.js index fcbed2b1..09cf34db 100644 --- a/addon/helpers/changeset.js +++ b/addon/helpers/changeset.js @@ -3,7 +3,9 @@ import Changeset from 'ember-changeset'; import isChangeset from 'ember-changeset/utils/is-changeset'; import isPromise from 'ember-changeset/utils/is-promise'; +import isRelay from 'ember-changeset/utils/is-relay'; import { helper } from '@ember/component/helper'; +import { get } from '@ember/object'; /*:: import type { ValidatorFunc } from 'ember-changeset/types/validator-func'; @@ -22,7 +24,11 @@ export function changeset( return obj.then((resolved) => new Changeset(resolved, validations, {}, options)); } - return new Changeset(obj, validations, {}, options); + let result = new Changeset(obj, validations, {}, options); + if (isRelay(result)) { + return get(result, 'content'); + } + return result; } export default helper(changeset); diff --git a/tests/integration/components/changeset-test.js b/tests/integration/components/changeset-test.js index 872dd187..388f4390 100644 --- a/tests/integration/components/changeset-test.js +++ b/tests/integration/components/changeset-test.js @@ -326,6 +326,36 @@ test('it does not rollback when validating', async function(assert) { assert.equal(find('code.odd').textContent.trim(), '10', 'should not rollback'); }); +test('it handles when changeset is already set', async function(assert) { + class Moment { + constructor(date) { + this.date = date; + } + } + let d = new Date('2015'); + let momentInstance = new Moment(d); + this.set('dummyModel', { startDate: momentInstance }); + this.render(hbs` + {{#with (changeset dummyModel) as |changeset|}} +

{{changeset.startDate.date}}

+ {{/with}} + `); + + assert.equal(find('h1').textContent.trim(), d, 'should update observable value'); +}); + +test('it handles when is plain object passed to helper', async function(assert) { + let d = new Date('2015'); + this.set('d', d); + this.render(hbs` + {{#with (changeset (hash date=d)) as |changeset|}} +

{{changeset.date}}

+ {{/with}} + `); + + assert.equal(find('h1').textContent.trim(), d, 'should update observable value'); +}); + test('it handles models that are promises', async function(assert) { this.set('dummyModel', resolve({ firstName: 'Jim', lastName: 'Bob' })); this.render(hbs` diff --git a/tests/unit/changeset-test.js b/tests/unit/changeset-test.js index 9f73485c..a2a1f9bf 100644 --- a/tests/unit/changeset-test.js +++ b/tests/unit/changeset-test.js @@ -199,7 +199,7 @@ module('Unit | Utility | changeset', function(hooks) { assert.equal(result, 'Jim Bob', 'should proxy to content'); }); - test('scott #get returns the content when the proxied content is an object', function(assert) { + test('#get returns the content when the proxied content is a class', function(assert) { class Moment { constructor(date) { this.date = date; @@ -216,11 +216,6 @@ module('Unit | Utility | changeset', function(hooks) { assert.deepEqual(newValue, momentInstance, 'correct getter'); assert.ok(newValue instanceof Moment, 'correct instance'); assert.equal(newValue.date, d, 'correct date on moment object'); - - newValue = get(c, 'startDate'); - assert.deepEqual(newValue, momentInstance, 'correct getter'); - // assert.ok(newValue instanceof Moment, 'correct instance'); - // assert.equal(newValue.date, d, 'correct date on moment object'); }); test('#get returns change if present', function(assert) {