Add tests for findAll store culling

This commit is contained in:
Michael Lange 2018-02-20 12:04:36 -08:00
parent 80c8db559b
commit 762a82702a
3 changed files with 86 additions and 0 deletions

View File

@ -17,6 +17,9 @@ export default function(modelName, description, options = { needs: [] }) {
// Reassign the subject to provide the serializer
this.subject = () => model.store.serializerFor(modelName);
// Expose the store as well, since it is a parameter for many serializer methods
this.store = model.store;
if (options.beforeEach) {
options.beforeEach.apply(this, arguments);
}

View File

@ -0,0 +1,71 @@
import { run } from '@ember/runloop';
import { test } from 'ember-qunit';
import wait from 'ember-test-helpers/wait';
import NodeModel from 'nomad-ui/models/node';
import moduleForSerializer from '../../helpers/module-for-serializer';
import pushPayloadToStore from '../../utils/push-payload-to-store';
moduleForSerializer('node', 'Unit | Serializer | Node', {
needs: ['serializer:node', 'service:config', 'transform:fragment', 'model:allocation'],
});
test('local store is culled to reflect the state of findAll requests', function(assert) {
const findAllResponse = [
makeNode('1', 'One', '127.0.0.1:4646'),
makeNode('2', 'Two', '127.0.0.2:4646'),
makeNode('3', 'Three', '127.0.0.3:4646'),
];
const payload = this.subject().normalizeFindAllResponse(this.store, NodeModel, findAllResponse);
pushPayloadToStore(this.store, payload, NodeModel.modelName);
assert.equal(
payload.data.length,
findAllResponse.length,
'Each original record is returned in the response'
);
assert.equal(
this.store
.peekAll('node')
.filterBy('id')
.get('length'),
findAllResponse.length,
'Each original record is now in the store'
);
const newFindAllResponse = [
makeNode('2', 'Two', '127.0.0.2:4646'),
makeNode('3', 'Three', '127.0.0.3:4646'),
makeNode('4', 'Four', '127.0.0.4:4646'),
];
let newPayload;
run(() => {
newPayload = this.subject().normalizeFindAllResponse(this.store, NodeModel, newFindAllResponse);
});
pushPayloadToStore(this.store, newPayload, NodeModel.modelName);
return wait().then(() => {
assert.equal(
newPayload.data.length,
newFindAllResponse.length,
'Each new record is returned in the response'
);
assert.equal(
this.store
.peekAll('node')
.filterBy('id')
.get('length'),
newFindAllResponse.length,
'The node length in the store reflects the new response'
);
assert.notOk(this.store.peekAll('node').findBy('id', '1'), 'Record One is no longer found');
});
});
function makeNode(id, name, ip) {
return { ID: id, Name: name, HTTPAddr: ip };
}

View File

@ -0,0 +1,12 @@
import { run } from '@ember/runloop';
// These are private store methods called by store "finder" methods.
// Useful in unit tests when there is store interaction, since calling
// adapter and serializer methods directly will never insert data into
// the store.
export default function pushPayloadToStore(store, payload, modelName) {
run(() => {
store._push(payload);
store._didUpdateAll(modelName);
});
}