Skip to content

Commit

Permalink
only return relay if ember object
Browse files Browse the repository at this point in the history
  • Loading branch information
snewcomer committed Aug 23, 2018
1 parent 066c99f commit 51767e8
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 14 deletions.
8 changes: 4 additions & 4 deletions addon/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import isEmptyObject from 'ember-changeset/utils/computed/is-empty-object';
import inflate from 'ember-changeset/utils/computed/inflate';
import transform from 'ember-changeset/utils/computed/transform';
import isPromise from 'ember-changeset/utils/is-promise';
import isObject from 'ember-changeset/utils/is-object';
import isObject, { isAnyObject } from 'ember-changeset/utils/is-object';
import pureAssign from 'ember-changeset/utils/assign';
import includes from 'ember-changeset/utils/includes';
import take from 'ember-changeset/utils/take';
Expand Down Expand Up @@ -287,7 +287,7 @@ export function changeset(
let changes /*: { [string]: mixed } */ = get(this, '_bareChanges');
let preparedChanges = prepareChangesFn(changes);

assert('Callback to `changeset.prepare` must return an object', isObject(preparedChanges));
assert('Callback to `changeset.prepare` must return an object', isAnyObject(preparedChanges));
validateNestedObj('preparedChanges', preparedChanges);

let newChanges /*: Changes */ = keys(preparedChanges).reduce((newObj, key) => {
Expand Down Expand Up @@ -490,7 +490,7 @@ export function changeset(
) /*: T */ {
// Construct new `Err` instance.
let newError /*: Err */;
if (isObject(error)) {
if (isAnyObject(error)) {
let errorLike /*: ErrLike<*> */ = (error /*: any */);
assert('Error must have value.', errorLike.hasOwnProperty('value'));
assert('Error must have validation.', errorLike.hasOwnProperty('validation'));
Expand Down Expand Up @@ -709,7 +709,7 @@ export function changeset(
setNestedProperty(changes, key, new Change(value));

// ensure cache key is updated with new relay if value is object
if (isObject(value)) {
if (isAnyObject(value)) {
let cache /*: RelayCache */ = get(this, RELAY_CACHE);
cache[key] = Relay.create({ key, changeset: this, content: value });
}
Expand Down
4 changes: 2 additions & 2 deletions addon/utils/computed/inflate.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { computed, get } from '@ember/object';
import deepSet from 'ember-deep-set';
import { assert, runInDebug } from '@ember/debug';
import isObject from 'ember-changeset/utils/is-object';
import { isAnyObject } from 'ember-changeset/utils/is-object';
import { isBlank } from '@ember/utils';

const { keys } = Object;
Expand All @@ -25,7 +25,7 @@ export default function inflate /*:: <T> */ (
if (i < allParts.length - 1) {
let path = allParts.slice(0, i+1).join('.');
let msg = `Path ${path} leading up to ${key} must be an Object if specified.`;
assert(msg, isObject(obj[path]) || isBlank(obj[path]));
assert(msg, isAnyObject(obj[path]) || isBlank(obj[path]));
}
});
});
Expand Down
7 changes: 7 additions & 0 deletions addon/utils/is-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,12 @@
import { typeOf } from '@ember/utils';

export default function isObject(val /*: mixed */) {
if (val) {
return typeOf(val) === 'instance' || Object.getPrototypeOf(val) === Object.prototype;
}
return false;
}

export function isAnyObject(val /*: mixed */) {
return typeOf(val) === 'object' || typeOf(val) === 'instance';
}
4 changes: 2 additions & 2 deletions addon/utils/is-promise.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @flow

import { typeOf } from '@ember/utils';
import isObject from './is-object';
import { isAnyObject } from './is-object';

function isPromiseLike(obj /*: mixed */) /*: boolean */ {
return !!obj
Expand All @@ -14,5 +14,5 @@ function isPromiseLike(obj /*: mixed */) /*: boolean */ {
}

export default function isPromise(obj /*: mixed */) /*: boolean */ {
return isObject(obj) && isPromiseLike(obj);
return isAnyObject(obj) && isPromiseLike(obj);
}
10 changes: 5 additions & 5 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 an object', function(assert) {
class Moment {
constructor(date) {
this.date = date;
Expand All @@ -219,8 +219,8 @@ module('Unit | Utility | changeset', function(hooks) {

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');
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 Expand Up @@ -321,12 +321,12 @@ module('Unit | Utility | changeset', function(hooks) {
let c = new Changeset(model);
let actual = get(c, 'foo.bar.dog');
let expectedResult = get(model, 'foo.bar.dog');
assert.notEqual(actual, expectedResult, "using Ember.get won't work");
assert.equal(actual, expectedResult, "using Ember.get will work");
}

{
let c = new Changeset(model);
let actual = get(c, 'foo.bar.dog.content');
let actual = get(c, 'foo.bar.dog');
let expectedResult = get(model, 'foo.bar.dog');
assert.equal(actual, expectedResult, "you have to use .content");
}
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/utils/is-promise-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ testData.forEach(({ value, expected }) => {
test('it checks if an object is an instance of an RSVP.Promise', function(assert) {
let result = isPromise(value);

assert.equal(result, expected, `should be ${expected}`);
assert.equal(result, expected, `should be ${expected} for ${value}`);
});
});

0 comments on commit 51767e8

Please sign in to comment.