From bba3f07e69ec26bfc3fc9c93c3dcf9ee08ed9a2b Mon Sep 17 00:00:00 2001 From: Chris Thoburn Date: Fri, 12 Jul 2019 10:27:08 -0700 Subject: [PATCH] some cleanup --- .../integration/adapter/find-all-test.js | 3 +- .../tests/integration/debug-adapter-test.js | 14 +- .../tests/integration/inverse-test.js | 187 ++++++++++++++---- .../integration/record-array-manager-test.js | 47 +++-- .../adapter-populated-record-array-test.js | 4 +- .../tests/integration/snapshot-test.js | 2 +- 6 files changed, 193 insertions(+), 64 deletions(-) diff --git a/packages/-ember-data/tests/integration/adapter/find-all-test.js b/packages/-ember-data/tests/integration/adapter/find-all-test.js index 2ca7067d3b7..8bb05732754 100644 --- a/packages/-ember-data/tests/integration/adapter/find-all-test.js +++ b/packages/-ember-data/tests/integration/adapter/find-all-test.js @@ -141,7 +141,7 @@ module('integration/adapter/find-all - Finding All Records of a Type', function( ); }); - test('When all records for a type are requested, records that are created on the client should be added to the record array.', assert => { + test('When all records for a type are requested, records that are created on the client should be added to the record array.', async assert => { assert.expect(3); let allRecords = store.peekAll('person'); @@ -153,6 +153,7 @@ module('integration/adapter/find-all - Finding All Records of a Type', function( ); store.createRecord('person', { name: 'Carsten Nielsen' }); + await settled(); assert.equal(get(allRecords, 'length'), 1, "the record array's length is 1"); assert.equal( diff --git a/packages/-ember-data/tests/integration/debug-adapter-test.js b/packages/-ember-data/tests/integration/debug-adapter-test.js index 9007b190d77..dd97386f89e 100644 --- a/packages/-ember-data/tests/integration/debug-adapter-test.js +++ b/packages/-ember-data/tests/integration/debug-adapter-test.js @@ -3,6 +3,7 @@ import { A } from '@ember/array'; import { get } from '@ember/object'; import Model from '@ember-data/model'; import Adapter from '@ember-data/adapter'; +import { DebugAdapter } from 'ember-data/-private'; import { module, test } from 'qunit'; import { settled } from '@ember/test-helpers'; import { attr } from '@ember-data/model'; @@ -15,24 +16,25 @@ class Post extends Model { module('integration/debug-adapter - DS.DebugAdapter', function(hooks) { setupTest(hooks); - let store, debugAdapter; + let store; hooks.beforeEach(function() { let { owner } = this; owner.register('model:post', Post); store = owner.lookup('service:store'); - debugAdapter = owner.lookup('data-adapter:main'); - - debugAdapter.reopen({ + let _adapter = DebugAdapter.extend({ getModelTypes() { return A([{ klass: Post, name: 'post' }]); }, }); + owner.register('data-adapter:main', _adapter); }); test('Watching Model Types', async function(assert) { assert.expect(5); + let { owner } = this; + let debugAdapter = owner.lookup('data-adapter:main'); function added(types) { assert.equal(types.length, 1, 'added one type'); @@ -59,6 +61,8 @@ module('integration/debug-adapter - DS.DebugAdapter', function(hooks) { }); test('Watching Records', async function(assert) { + let { owner } = this; + let debugAdapter = owner.lookup('data-adapter:main'); let addedRecords, updatedRecords, removedIndex, removedCount; this.owner.register( @@ -164,6 +168,8 @@ module('integration/debug-adapter - DS.DebugAdapter', function(hooks) { }); test('Column names', function(assert) { + let { owner } = this; + let debugAdapter = owner.lookup('data-adapter:main'); class Person extends Model { @attr() title; diff --git a/packages/-ember-data/tests/integration/inverse-test.js b/packages/-ember-data/tests/integration/inverse-test.js index 36d29a0d6b2..08e65cba4b4 100644 --- a/packages/-ember-data/tests/integration/inverse-test.js +++ b/packages/-ember-data/tests/integration/inverse-test.js @@ -15,11 +15,13 @@ function stringify(string) { module('integration/inverse_test - inverseFor', function(hooks) { setupTest(hooks); let store; - let user; - let job; hooks.beforeEach(function() { let { owner } = this; + store = owner.lookup('service:store'); + }); + + test('Finds the inverse when there is only one possible available', function(assert) { class User extends Model { @attr() name; @@ -47,25 +49,12 @@ module('integration/inverse_test - inverseFor', function(hooks) { } } - class ReflexiveModel extends Model { - @belongsTo('reflexive-model', { async: false }) - reflexiveProp; - - toString() { - return stringify('reflexiveModel'); - } - } + let { owner } = this; owner.register('model:user', User); owner.register('model:job', Job); - owner.register('model:reflexive-model', ReflexiveModel); - store = owner.lookup('service:store'); - - job = store.modelFor('job'); - user = store.modelFor('user'); - }); - - test('Finds the inverse when there is only one possible available', function(assert) { + let job = store.modelFor('job'); + let user = store.modelFor('user'); let inverseDefinition = job.inverseFor('user', store); assert.deepEqual( @@ -83,13 +72,45 @@ module('integration/inverse_test - inverseFor', function(hooks) { }); test('Finds the inverse when only one side has defined it manually', function(assert) { - job.reopen({ - owner: belongsTo('user', { inverse: 'previousJob', async: false }), - }); + class User extends Model { + @attr() + name; + + @belongsTo('user', { async: true, inverse: null }) + bestFriend; - user.reopen({ - previousJob: belongsTo('job', { async: false }), - }); + @belongsTo('job', { async: false }) + job; + + @belongsTo('job', { async: false }) + previousJob; + + toString() { + return stringify('user'); + } + } + + class Job extends Model { + @attr() + isGood; + + @belongsTo('user', { async: false }) + user; + + @belongsTo('user', { inverse: 'previousJob', async: false }) + owner; + + toString() { + return stringify('job'); + } + } + + let { owner } = this; + owner.register('model:user', User); + owner.register('model:job', Job); + + let job = store.modelFor('job'); + let user = store.modelFor('user'); assert.deepEqual( job.inverseFor('owner', store), @@ -120,27 +141,76 @@ module('integration/inverse_test - inverseFor', function(hooks) { }); test('Returns null if inverse relationship it is manually set with a different relationship key', function(assert) { - job.reopen({ - user: belongsTo('user', { inverse: 'previousJob', async: false }), - }); + class User extends Model { + @attr() + name; + + @belongsTo('user', { async: true, inverse: null }) + bestFriend; + + @belongsTo('job', { async: false }) + job; + + toString() { + return stringify('user'); + } + } + + class Job extends Model { + @attr() + isGood; + + @belongsTo('user', { inverse: 'previousJob', async: false }) + user; + + toString() { + return stringify('job'); + } + } - user.reopen({ - job: belongsTo('job', { async: false }), - }); + let { owner } = this; + owner.register('model:user', User); + owner.register('model:job', Job); + let user = store.modelFor('user'); assert.equal(user.inverseFor('job', store), null, 'There is no inverse'); }); testInDebug('Errors out if you define 2 inverses to the same model', function(assert) { - job.reopen({ - user: belongsTo('user', { inverse: 'job', async: false }), - owner: belongsTo('user', { inverse: 'job', async: false }), - }); + class User extends Model { + @attr() + name; + + @belongsTo('user', { async: true, inverse: null }) + bestFriend; + + @belongsTo('job', { async: false }) + job; + + toString() { + return stringify('user'); + } + } - user.reopen({ - job: belongsTo('job', { async: false }), - }); + class Job extends Model { + @attr() + isGood; + + @belongsTo('user', { inverse: 'job', async: false }) + user; + @belongsTo('user', { inverse: 'job', async: false }) + owner; + + toString() { + return stringify('job'); + } + } + let { owner } = this; + owner.register('model:user', User); + owner.register('model:job', Job); + + let user = store.modelFor('user'); assert.expectAssertion(() => { user.inverseFor('job', store); }, /Assertion Failed: You defined the 'job' relationship on model:user, but you defined the inverse relationships of type model:job multiple times/i); @@ -148,6 +218,37 @@ module('integration/inverse_test - inverseFor', function(hooks) { test('Caches findInverseFor return value', function(assert) { assert.expect(1); + class User extends Model { + @attr() + name; + + @belongsTo('user', { async: true, inverse: null }) + bestFriend; + + @belongsTo('job', { async: false }) + job; + + toString() { + return stringify('user'); + } + } + + class Job extends Model { + @attr() + isGood; + + @belongsTo('user', { async: false }) + user; + + toString() { + return stringify('job'); + } + } + let { owner } = this; + owner.register('model:user', User); + owner.register('model:job', Job); + + let job = store.modelFor('job'); let inverseForUser = job.inverseFor('user', store); job.findInverseFor = function() { @@ -158,6 +259,18 @@ module('integration/inverse_test - inverseFor', function(hooks) { }); testInDebug('Errors out if you do not define an inverse for a reflexive relationship', function(assert) { + class ReflexiveModel extends Model { + @belongsTo('reflexive-model', { async: false }) + reflexiveProp; + + toString() { + return stringify('reflexiveModel'); + } + } + + let { owner } = this; + owner.register('model:reflexive-model', ReflexiveModel); + //Maybe store is evaluated lazily, so we need this :( assert.expectWarning(() => { var reflexiveModel; diff --git a/packages/-ember-data/tests/integration/record-array-manager-test.js b/packages/-ember-data/tests/integration/record-array-manager-test.js index d170cf5aaee..e2cb2b3de1f 100644 --- a/packages/-ember-data/tests/integration/record-array-manager-test.js +++ b/packages/-ember-data/tests/integration/record-array-manager-test.js @@ -1,7 +1,7 @@ import { A } from '@ember/array'; import { setupTest } from 'ember-qunit'; import OrderedSet from '@ember/ordered-set'; - +import { settled } from '@ember/test-helpers'; import { module, test } from 'qunit'; import Model from '@ember-data/model'; @@ -59,9 +59,6 @@ module('integration/record_array_manager', function(hooks) { } test('destroying the store correctly cleans everything up', async function(assert) { - let query = {}; - let person; - store.push({ data: { type: 'car', @@ -78,7 +75,7 @@ module('integration/record_array_manager', function(hooks) { }, }); - store.push({ + let person = store.push({ data: { type: 'person', id: '1', @@ -92,30 +89,36 @@ module('integration/record_array_manager', function(hooks) { }, }, }); - person = store.peekRecord('person', 1); let all = store.peekAll('person'); + let query = {}; let adapterPopulated = manager.createAdapterPopulatedRecordArray('person', query); let allSummary = tap(all, 'willDestroy'); let adapterPopulatedSummary = tap(adapterPopulated, 'willDestroy'); let internalPersonModel = person._internalModel; - assert.equal(allSummary.called.length, 0); - assert.equal(adapterPopulatedSummary.called.length, 0); - assert.equal(internalPersonModel._recordArrays.size, 1, 'expected the person to be a member of 1 recordArrays'); - assert.equal('person' in manager._liveRecordArrays, true); + assert.equal(allSummary.called.length, 0, 'initial: no calls to all.willDestroy'); + assert.equal(adapterPopulatedSummary.called.length, 0, 'initial: no calls to adapterPopulated.willDestroy'); + assert.equal( + internalPersonModel._recordArrays.size, + 1, + 'initial: expected the person to be a member of 1 recordArrays' + ); + assert.equal('person' in manager._liveRecordArrays, true, 'initial: we have a live array for person'); - await all.destroy(); + all.destroy(); + await settled(); assert.equal(internalPersonModel._recordArrays.size, 0, 'expected the person to be a member of 1 recordArrays'); - assert.equal(allSummary.called.length, 1); - assert.equal('person' in manager._liveRecordArrays, false); + assert.equal(allSummary.called.length, 1, 'all.willDestroy called once'); + assert.equal('person' in manager._liveRecordArrays, false, 'no longer have a live array for person'); - await manager.destroy(); + manager.destroy(); + await settled(); assert.equal(internalPersonModel._recordArrays.size, 0, 'expected the person to be a member of no recordArrays'); - assert.equal(allSummary.called.length, 1); - assert.equal(adapterPopulatedSummary.called.length, 1); + assert.equal(allSummary.called.length, 1, 'all.willDestroy still only called once'); + assert.equal(adapterPopulatedSummary.called.length, 1, 'adapterPopulated.willDestroy called once'); }); test('batch liveRecordArray changes', async function(assert) { @@ -132,7 +135,7 @@ module('integration/record_array_manager', function(hooks) { assert.deepEqual(cars.toArray(), []); assert.equal(arrayContentWillChangeCount, 0, 'expected NO arrayChangeEvents yet'); - await store.push({ + store.push({ data: [ { type: 'car', @@ -152,6 +155,7 @@ module('integration/record_array_manager', function(hooks) { }, ], }); + await settled(); assert.equal(arrayContentWillChangeCount, 1, 'expected ONE array change event'); @@ -170,7 +174,7 @@ module('integration/record_array_manager', function(hooks) { arrayContentWillChangeCount = 0; - await store.push({ + store.push({ data: [ { type: 'car', @@ -182,10 +186,11 @@ module('integration/record_array_manager', function(hooks) { }, ], }); + await settled(); assert.equal(arrayContentWillChangeCount, 0, 'expected NO array change events'); - await store.push({ + store.push({ data: [ { type: 'car', @@ -197,6 +202,7 @@ module('integration/record_array_manager', function(hooks) { }, ], }); + await settled(); assert.equal(arrayContentWillChangeCount, 1, 'expected ONE array change event'); // reset function so it doesn't execute after test finishes and store is torn down @@ -219,7 +225,8 @@ module('integration/record_array_manager', function(hooks) { let adapterPopulated = manager.createAdapterPopulatedRecordArray('car', query); - await adapterPopulated.destroy(); + adapterPopulated.destroy(); + await settled(); assert.equal(manager._adapterPopulatedRecordArrays.length, 0); }); diff --git a/packages/-ember-data/tests/integration/record-arrays/adapter-populated-record-array-test.js b/packages/-ember-data/tests/integration/record-arrays/adapter-populated-record-array-test.js index e712215893b..d0f5ee1b394 100644 --- a/packages/-ember-data/tests/integration/record-arrays/adapter-populated-record-array-test.js +++ b/packages/-ember-data/tests/integration/record-arrays/adapter-populated-record-array-test.js @@ -1,6 +1,7 @@ import { run } from '@ember/runloop'; import { Promise } from 'rsvp'; import { setupStore, createStore } from 'dummy/tests/helpers/store'; +import { settled } from '@ember/test-helpers'; import { module, test } from 'qunit'; @@ -151,8 +152,9 @@ module('integration/record-arrays/adapter_populated_record_array - DS.AdapterPop ); }); - test('recordArray.replace() throws error', function(assert) { + test('recordArray.replace() throws error', async function(assert) { let recordArray = store.recordArrayManager.createAdapterPopulatedRecordArray('person', null); + await settled(); assert.throws( () => { diff --git a/packages/-ember-data/tests/integration/snapshot-test.js b/packages/-ember-data/tests/integration/snapshot-test.js index d0a59e5b1b3..77c4f7d2b9a 100644 --- a/packages/-ember-data/tests/integration/snapshot-test.js +++ b/packages/-ember-data/tests/integration/snapshot-test.js @@ -458,7 +458,7 @@ module('integration/snapshot - Snapshot', function(hooks) { }); test('snapshot.belongsTo() returns a snapshot if relationship link has been fetched', async function(assert) { - assert.expect(2); + assert.expect(4); store.adapterFor('application').findBelongsTo = function(store, snapshot, link, relationship) { return resolve({ data: { id: 1, type: 'post', attributes: { title: 'Hello World' } } });