From 1dd3aaa9524c89f6f38199d5233fe074a010e256 Mon Sep 17 00:00:00 2001 From: Stefan Penner Date: Wed, 19 Oct 2016 18:47:44 +0300 Subject: [PATCH] Fixup record-array/adapter-populate-record-array * add unit tests * fixup shape --- .../adapter-populated-record-array.js | 1 + .../adapter-populated-record-array-test.js | 147 ++++++++++++++++++ 2 files changed, 148 insertions(+) create mode 100644 tests/unit/record-arrays/adapter-populated-record-array-test.js diff --git a/addon/-private/system/record-arrays/adapter-populated-record-array.js b/addon/-private/system/record-arrays/adapter-populated-record-array.js index 5c69bde359b..da10adc7d7f 100644 --- a/addon/-private/system/record-arrays/adapter-populated-record-array.js +++ b/addon/-private/system/record-arrays/adapter-populated-record-array.js @@ -23,6 +23,7 @@ export default RecordArray.extend({ init() { this._super(...arguments); this.query = this.query || null; + this.links = null; }, replace() { diff --git a/tests/unit/record-arrays/adapter-populated-record-array-test.js b/tests/unit/record-arrays/adapter-populated-record-array-test.js new file mode 100644 index 00000000000..4ebeee1c5fa --- /dev/null +++ b/tests/unit/record-arrays/adapter-populated-record-array-test.js @@ -0,0 +1,147 @@ +import DS from 'ember-data'; + +import {module, test} from 'qunit'; +import Ember from 'ember'; + +const { RSVP, run } = Ember; +const { AdapterPopulatedRecordArray } = DS; + +module('unit/record-arrays/adapter-populated-record-array - DS.AdapterPopulatedRecordArray'); + +test('default initial state', function(assert) { + let recordArray = AdapterPopulatedRecordArray.create({ type: 'recordType' }); + + assert.equal(recordArray.get('isLoaded'), false, 'expected isLoaded to be false'); + assert.equal(recordArray.get('type'), 'recordType'); + assert.equal(recordArray.get('content'), null); + assert.equal(recordArray.get('query'), null); + assert.equal(recordArray.get('store'), null); + assert.equal(recordArray.get('links'), null); +}); + +test('custom initial state', function(assert) { + let content = []; + let store = {}; + let recordArray = AdapterPopulatedRecordArray.create({ + type: 'apple', + isLoaded: true, + isUpdating: true, + content, + store, + query: 'some-query', + links: 'foo' + }) + assert.equal(recordArray.get('isLoaded'), true); + assert.equal(recordArray.get('isUpdating'), false); + assert.equal(recordArray.get('type'), 'apple'); + assert.equal(recordArray.get('content'), content); + assert.equal(recordArray.get('store'), store); + assert.equal(recordArray.get('query'), 'some-query'); + assert.equal(recordArray.get('links'), null); +}); + +test('#replace() throws error', function(assert) { + let recordArray = AdapterPopulatedRecordArray.create({ type: 'recordType' }); + + assert.throws(() => { + recordArray.replace(); + }, Error('The result of a server query (on recordType) is immutable.'), 'throws error'); +}); + +test('#update uses _update enabling query specific behavior', function(assert) { + let queryCalled = 0; + let deferred = RSVP.defer(); + + const store = { + _query(modelName, query, array) { + queryCalled++; + assert.equal(modelName, 'recordType'); + assert.equal(query, 'some-query'); + assert.equal(array, recordArray); + + return deferred.promise; + } + }; + + let recordArray = AdapterPopulatedRecordArray.create({ + type: { modelName: 'recordType' }, + store, + query: 'some-query' + }); + + assert.equal(recordArray.get('isUpdating'), false, 'should not yet be updating'); + + assert.equal(queryCalled, 0); + + let updateResult = recordArray.update(); + + assert.equal(queryCalled, 1); + + deferred.resolve('return value'); + + assert.equal(recordArray.get('isUpdating'), true, 'should be updating'); + + return updateResult.then(result => { + assert.equal(result, 'return value'); + assert.equal(recordArray.get('isUpdating'), false, 'should no longer be updating'); + }); +}); + +// TODO: is this method required, i suspect store._query should be refactor so this is not needed +test('#loadRecords', function(assert) { + let didAddRecord = 0; + const manager = { + recordArraysForRecord(record) { + return { + add(array) { + didAddRecord++; + assert.equal(array, recordArray); + } + } + } + }; + + let recordArray = AdapterPopulatedRecordArray.create({ + query: 'some-query', + manager + }); + + let model1 = { + id: 2, + _internalModel: { getRecord() { return model1; }} + }; + + let model2 = { + id: 2, + _internalModel: { getRecord() { return model2; }} + }; + + assert.equal(didAddRecord, 0, 'no records should have been added yet'); + + let didLoad = 0; + recordArray.on('didLoad', function() { + didLoad++; + }); + + let links = { foo:1 }; + let meta = { bar:2 }; + + run(() => { + assert.equal(recordArray.loadRecords([model1, model2], { + links, + meta + }), undefined, 'loadRecords should have no return value'); + + assert.equal(didAddRecord, 2, 'two records should have been adde'); + + assert.deepEqual(recordArray.toArray(), [ + model1, + model2 + ], 'should now contain the loaded records'); + + assert.equal(didLoad, 0, 'didLoad event should not have fired'); + assert.equal(recordArray.get('links').foo, 1); + assert.equal(recordArray.get('meta').bar, 2); + }); + assert.equal(didLoad, 1, 'didLoad event should have fired once'); +});