-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cleanup record array manager #4597
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
890d051
[PERF pt.1] avoid extra scanning of recordArrays when adding records
stefanpenner f5c2ba5
tidy up tests related to adapter-populated-record-arrays
stefanpenner b5785fe
tidy up proxy related change events for AdapterPopulatedRecordArray d…
stefanpenner e5f106e
tidy-up AdapterPopulatedRecordArrays:
stefanpenner b699683
fixup based on PR review
stefanpenner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
249 changes: 249 additions & 0 deletions
249
tests/integration/record-arrays/adapter-populated-record-array-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,249 @@ | ||
import {setupStore, createStore} from 'dummy/tests/helpers/store'; | ||
import Ember from 'ember'; | ||
|
||
import {module, test} from 'qunit'; | ||
|
||
import DS from 'ember-data'; | ||
|
||
let store; | ||
const { run, RSVP: { Promise } } = Ember; | ||
|
||
const Person = DS.Model.extend({ | ||
name: DS.attr('string'), | ||
toString() { | ||
return `<Person#${this.get('id')}>`; | ||
} | ||
}); | ||
|
||
|
||
const adapter = DS.Adapter.extend({ | ||
deleteRecord() { | ||
return Promise.resolve(); | ||
} | ||
}); | ||
|
||
module('integration/record-arrays/adapter_populated_record_array - DS.AdapterPopulatedRecordArray', { | ||
beforeEach() { | ||
store = createStore({ | ||
adapter: adapter, | ||
person: Person | ||
}); | ||
} | ||
}); | ||
|
||
test('when a record is deleted in an adapter populated record array, it should be removed', function(assert) { | ||
let recordArray = store.recordArrayManager | ||
.createAdapterPopulatedRecordArray(store.modelFor('person'), null); | ||
|
||
let payload = { | ||
data: [ | ||
{ | ||
type: 'person', | ||
id: '1', | ||
attributes: { | ||
name: 'Scumbag Dale' | ||
} | ||
}, | ||
{ | ||
type: 'person', | ||
id: '2', | ||
attributes: { | ||
name: 'Scumbag Katz' | ||
} | ||
}, | ||
{ | ||
type: 'person', | ||
id: '3', | ||
attributes: { | ||
name: 'Scumbag Bryn' | ||
} | ||
} | ||
] | ||
}; | ||
|
||
run(() => { | ||
recordArray._setInternalModels(store._push(payload), payload); | ||
}); | ||
|
||
assert.equal(recordArray.get('length'), 3, "expected recordArray to contain exactly 3 records"); | ||
|
||
run(() => recordArray.get('firstObject').destroyRecord()); | ||
|
||
assert.equal(recordArray.get('length'), 2, "expected recordArray to contain exactly 2 records"); | ||
}); | ||
|
||
test('stores the metadata off the payload', function(assert) { | ||
let recordArray = store.recordArrayManager | ||
.createAdapterPopulatedRecordArray(Person, null); | ||
|
||
let payload = { | ||
data: [ | ||
{ | ||
type: 'person', | ||
id: '1', | ||
attributes: { | ||
name: 'Scumbag Dale' | ||
} | ||
}, | ||
{ | ||
type: 'person', | ||
id: '2', | ||
attributes: { | ||
name: 'Scumbag Katz' | ||
} | ||
}, | ||
{ | ||
type: 'person', | ||
id: '3', | ||
attributes: { | ||
name: 'Scumbag Bryn' | ||
} | ||
} | ||
], | ||
meta: { | ||
foo: 'bar' | ||
} | ||
}; | ||
|
||
run(() => { | ||
recordArray._setInternalModels(store._push(payload), payload); | ||
}); | ||
|
||
assert.equal(recordArray.get('meta.foo'), 'bar', 'expected meta.foo to be bar from payload'); | ||
}); | ||
|
||
test('stores the links off the payload', function(assert) { | ||
let recordArray = store.recordArrayManager | ||
.createAdapterPopulatedRecordArray(store.modelFor('person'), null); | ||
|
||
let payload = { | ||
data: [ | ||
{ | ||
type: 'person', | ||
id: '1', | ||
attributes: { | ||
name: 'Scumbag Dale' | ||
} | ||
}, | ||
{ | ||
type: 'person', | ||
id: '2', | ||
attributes: { | ||
name: 'Scumbag Katz' | ||
} | ||
}, | ||
{ | ||
type: 'person', | ||
id: '3', | ||
attributes: { | ||
name: 'Scumbag Bryn' | ||
} | ||
} | ||
], | ||
links: { | ||
first: '/foo?page=1' | ||
} | ||
}; | ||
|
||
run(() => { | ||
recordArray._setInternalModels(store._push(payload), payload); | ||
}); | ||
|
||
assert.equal(recordArray.get('links.first'), '/foo?page=1', 'expected links.first to be "/foo?page=1" from payload'); | ||
}); | ||
|
||
test('recordArray.replace() throws error', function(assert) { | ||
let recordArray = store.recordArrayManager | ||
.createAdapterPopulatedRecordArray(Person, null); | ||
|
||
assert.throws(() => { | ||
recordArray.replace(); | ||
}, Error('The result of a server query (on (subclass of DS.Model)) is immutable.'), 'throws error'); | ||
}); | ||
|
||
test('loadRecord re-syncs internalModels recordArrays', function(assert) { | ||
let env = setupStore({ person: Person }); | ||
let store = env.store; | ||
|
||
let payload = [ | ||
{ id: '1', name: 'Scumbag Dale' }, | ||
{ id: '2', name: 'Scumbag Katz' } | ||
]; | ||
|
||
env.adapter.query = function(store, type, query, recordArray) { | ||
return payload; | ||
}; | ||
|
||
return store.query('person', { }).then(recordArray => { | ||
return recordArray.update().then(recordArray => { | ||
assert.deepEqual(recordArray.getEach('name'), ['Scumbag Dale', 'Scumbag Katz'], 'expected query to contain specific records'); | ||
|
||
payload = [ | ||
{ id: '1', name: 'Scumbag Dale' }, | ||
{ id: '3', name: 'Scumbag Penner' } | ||
]; | ||
|
||
return recordArray.update(); | ||
}).then(recordArray => { | ||
assert.deepEqual(recordArray.getEach('name'), ['Scumbag Dale', 'Scumbag Penner']); | ||
}); | ||
}); | ||
}); | ||
|
||
test('when an adapter populated record gets updated the array contents are also updated', function(assert) { | ||
assert.expect(8); | ||
|
||
let filteredPromise, filteredArr, findPromise, findArray; | ||
let env = setupStore({ person: Person }); | ||
let store = env.store; | ||
let array = [{ id: '1', name: 'Scumbag Dale' }]; | ||
|
||
// resemble server side filtering | ||
env.adapter.query = function(store, type, query, recordArray) { | ||
return array.slice(query.slice); | ||
}; | ||
|
||
// implement findAll to further test that query updates won't muddle | ||
// with the non-query record arrays | ||
env.adapter.findAll = function(store, type, sinceToken) { | ||
return array.slice(0); | ||
}; | ||
|
||
run(() => { | ||
filteredPromise = store.query('person', { slice: 1 }); | ||
findPromise = store.findAll('person'); | ||
|
||
// initialize adapter populated record array and assert initial state | ||
filteredPromise.then((_filteredArr) => { | ||
filteredArr = _filteredArr; | ||
assert.equal(filteredArr.get('length'), 0, 'No records for this query'); | ||
assert.equal(filteredArr.get('isUpdating'), false, 'Record array isUpdating state updated'); | ||
}); | ||
|
||
// initialize a record collection array and assert initial state | ||
findPromise.then((_findArr) => { | ||
findArray = _findArr; | ||
assert.equal(findArray.get('length'), 1, 'All records are included in collection array'); | ||
}); | ||
}); | ||
|
||
// a new element gets pushed in record array | ||
run(() => { | ||
array.push({ id: '2', name: 'Scumbag Katz' }); | ||
filteredArr.update().then(() => { | ||
assert.equal(filteredArr.get('length'), 1, 'The new record is returned and added in adapter populated array'); | ||
assert.equal(filteredArr.get('isUpdating'), false, 'Record array isUpdating state updated'); | ||
assert.equal(findArray.get('length'), 2); | ||
}); | ||
}); | ||
|
||
// element gets removed | ||
run(() => { | ||
array.pop(0); | ||
filteredArr.update().then(() => { | ||
assert.equal(filteredArr.get('length'), 0, 'Record removed from array'); | ||
// record not removed from the model collection | ||
assert.equal(findArray.get('length'), 2, 'Record still remains in collection array'); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're overwriting
toString()
in thePerson
above. Not sure why it's used here though.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would expect the assertion to be
'The result of a server query (on Person) is immutable.'
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
opened issue: #4615