Skip to content

Commit

Permalink
ensure helper returns relay content
Browse files Browse the repository at this point in the history
  • Loading branch information
snewcomer committed Aug 25, 2018
1 parent 10e657c commit 146f016
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
8 changes: 7 additions & 1 deletion addon/helpers/changeset.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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);
30 changes: 30 additions & 0 deletions tests/integration/components/changeset-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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|}}
<h1>{{changeset.startDate.date}}</h1>
{{/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|}}
<h1>{{changeset.date}}</h1>
{{/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`
Expand Down
7 changes: 1 addition & 6 deletions tests/unit/changeset-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down

0 comments on commit 146f016

Please sign in to comment.