Skip to content

Commit

Permalink
support custom isEqual function
Browse files Browse the repository at this point in the history
  • Loading branch information
GavinJoyce committed Oct 31, 2017
1 parent fa56b35 commit 71d75a9
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
4 changes: 3 additions & 1 deletion addon/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ export default Ember.Mixin.create({
this._super(...arguments);

let config = this.get('didChangeAttrsConfig');
let equalityFn = config.isEqual || isEqual;

let trackedAttrs = config.attrs;
let oldValues = this.get('_didChangeAttrsWeakMap').get(this);
let changes = {};
Expand All @@ -43,7 +45,7 @@ export default Ember.Mixin.create({
let current = this.get(key);
let previous = oldValues[key];

if (!isEqual(key, previous, current)) { //TODO: configurable equality fn
if (!equalityFn(key, previous, current)) {
changes[key] = { previous, current };
oldValues[key] = current;
}
Expand Down
61 changes: 61 additions & 0 deletions tests/integration/did-change-attrs-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ test('Basic usage', function(assert) {
this.set('isAdmin', false);

this.render(hbs`{{x-changer email=email isAdmin=isAdmin name=name}}`);

assert.equal(didChangeAttrsCallCount, 0, '`didChangeAttrs` is not called on initial render');

this.set('email', '[email protected]');
Expand All @@ -47,3 +48,63 @@ test('Basic usage', function(assert) {
this.set('name', 'TheTomster');
assert.equal(didChangeAttrsCallCount, 1, '`didChangeAttrs` is not called when an untracked attribute is changed');
});

test('Custom isEqual', function(assert) {
let changedAttrs, didChangeAttrsCallCount = 0;

registerComponent(this, {
didChangeAttrsConfig: {
attrs: ['user', 'isAdmin'],
isEqual(key, a, b) {
if (key === 'user') {
return (a && b) ? a.id === b.id : a === b;
}
return a === b;
}
},

didChangeAttrs(changes) {
this._super(...arguments);

didChangeAttrsCallCount++;
changedAttrs = changes;
}
});

this.set('user', { name: 'Tomster', id: '123' });
this.set('isAdmin', false);

this.render(hbs`{{x-changer user=user isAdmin=isAdmin}}`);

assert.equal(didChangeAttrsCallCount, 0, '`didChangeAttrs` is not called on initial render');

this.set('user', { name: 'TheTomster', id: '123' });

assert.equal(didChangeAttrsCallCount, 0, '`didChangeAttrs` is not called because user entities are equal');

this.set('user', { name: 'Zoey', id: '456' });

assert.equal(didChangeAttrsCallCount, 1, '`didChangeAttrs` is called because user entities are not equal');
assert.deepEqual(changedAttrs, {
user: {
previous: {
id: '123',
name: 'Tomster'
},
current: {
id: '456',
name: 'Zoey'
}
}
}, '`user` included in `changedAttrs` because `user.id` is different');

this.set('isAdmin', true);

assert.equal(didChangeAttrsCallCount, 2, '`didChangeAttrs` is called because isAdmin changed');
assert.deepEqual(changedAttrs, {
isAdmin: {
previous: false,
current: true
}
}, '`isAdmin` included in `changedAttrs` because it changed');
});

0 comments on commit 71d75a9

Please sign in to comment.