Skip to content

Commit

Permalink
Fixup record-array/filtered-record-array
Browse files Browse the repository at this point in the history
* ensure filter function updates just before destroy are ignored
* add unit test coverage for what makes filtered-record-arrays different then RecordArray
  • Loading branch information
stefanpenner committed Oct 19, 2016
1 parent c7fab16 commit 6bf9d46
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 8 deletions.
3 changes: 3 additions & 0 deletions addon/-private/system/record-arrays/filtered-record-array.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ export default RecordArray.extend({
@private
*/
_updateFilter() {
if (get(this, 'isDestroying') || get(this, 'isDestroyed')) {
return;
}
get(this, 'manager').updateFilter(this, get(this, 'type'), get(this, 'filterFunction'));
},

Expand Down
88 changes: 80 additions & 8 deletions tests/unit/record-arrays/filtered-record-array-test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,89 @@
import DS from 'ember-data';
import Ember from 'ember';

import {module, test} from 'qunit';

var filteredArray;
const { get } = Ember;
const { FilteredRecordArray } = DS;

module("unit/record-arrays/filtered-record-array - DS.FilteredRecordArray", {
beforeEach() {
filteredArray = DS.FilteredRecordArray.create({ type: 'recordType' });
}
module('unit/record-arrays/filtered-record-array - DS.FilteredRecordArray');

test('default initial state', function(assert) {
let recordArray = FilteredRecordArray.create({ type: 'recordType' });

assert.equal(get(recordArray, 'isLoaded'), true);
assert.equal(get(recordArray, 'type'), 'recordType');
assert.equal(get(recordArray, 'content'), null);
assert.equal(get(recordArray, 'filterFunction'), null);
assert.equal(get(recordArray, 'store'), null);
});

test('custom initial state', function(assert) {
let content = [];
let store = {};
let filterFunction = () => true;
let recordArray = FilteredRecordArray.create({
type: 'apple',
isLoaded: false, // ignored
isUpdating: true,
content,
store,
filterFunction
})
assert.equal(get(recordArray, 'isLoaded'), true);
assert.equal(get(recordArray, 'isUpdating'), false); // cannot set as default value:
assert.equal(get(recordArray, 'type'), 'apple');
assert.equal(get(recordArray, 'content'), content);
assert.equal(get(recordArray, 'store'), store);
assert.equal(get(recordArray, 'filterFunction'), filterFunction);
});

test('recordArray.replace() throws error', function(assert) {
test('#replace() throws error', function(assert) {
let recordArray = FilteredRecordArray.create({ type: 'recordType' });

assert.throws(function() {
filteredArray.replace();
}, Error("The result of a client-side filter (on recordType) is immutable."), 'throws error');
recordArray.replace();
}, Error('The result of a client-side filter (on recordType) is immutable.'), 'throws error');
});

test('updateFilter', function(assert) {
let didUpdateFilter = 0;
const updatedFilterFunction = () => true;

const manager = {
updateFilter(array, type, filterFunction) {
didUpdateFilter++;
assert.equal(recordArray, array);
assert.equal(type, 'recordType');
assert.equal(filterFunction, updatedFilterFunction);
},
unregisterRecordArray() {}
};

let recordArray = FilteredRecordArray.create({
type: 'recordType',
manager,
content: Ember.A()
});

assert.equal(didUpdateFilter, 0, 'no filterFunction should have been changed yet');

Ember.run(() => {
recordArray.set('filterFunction', updatedFilterFunction);
assert.equal(didUpdateFilter, 0, 'record array manager should not yet be informed of the filterFunction change');
recordArray.set('filterFunction', updatedFilterFunction);
assert.equal(didUpdateFilter, 0, 'record array manager should not yet be informed of the filterFunction change')
});

assert.equal(didUpdateFilter, 1, 'record array manager should have been informed once that the array filterFunction has changed');

didUpdateFilter = 0;
Ember.run(() => {
recordArray.set('filterFunction', updatedFilterFunction);
assert.equal(didUpdateFilter, 0, 'record array manager should not be informed of this change');
recordArray.destroy();
assert.equal(didUpdateFilter, 0, 'record array manager should not be informed of this change');
});

assert.equal(didUpdateFilter, 0, 'record array manager should not be informed of this change');
})

0 comments on commit 6bf9d46

Please sign in to comment.