diff --git a/packages/-ember-data/tests/integration/application-test.js b/packages/-ember-data/tests/integration/application-test.js index 7de434365cf..0bd7cb9ba98 100644 --- a/packages/-ember-data/tests/integration/application-test.js +++ b/packages/-ember-data/tests/integration/application-test.js @@ -2,7 +2,6 @@ import Namespace from '@ember/application/namespace'; import Service, { inject as service } from '@ember/service'; import Controller from '@ember/controller'; import Application from '@ember/application'; -import { run } from '@ember/runloop'; import Store from 'ember-data/store'; import { module, test } from 'qunit'; import { setupTest } from 'ember-qunit'; @@ -110,12 +109,6 @@ module('integration/application - Attaching initializer', function(hooks) { this.owner = null; }); - hooks.afterEach(function() { - if (this.application !== null) { - run(this.application, 'destroy'); - } - }); - test('ember-data initializer is run', async function(assert) { let ran = false; @@ -129,7 +122,7 @@ module('integration/application - Attaching initializer', function(hooks) { this.application = this.TestApplication.create({ autoboot: false }); - await run(() => this.application.boot()); + await this.application.boot(); assert.ok(ran, 'ember-data initializer was found'); }); @@ -149,7 +142,8 @@ module('integration/application - Attaching initializer', function(hooks) { this.application = this.TestApplication.create({ autoboot: false }); - await run(() => this.application.boot().then(() => (this.owner = this.application.buildInstance()))); + await this.application.boot(); + this.owner = this.application.buildInstance(); let store = this.owner.lookup('service:store'); assert.ok( diff --git a/packages/-ember-data/tests/integration/debug-adapter-test.js b/packages/-ember-data/tests/integration/debug-adapter-test.js index ced7a0f0c15..9007b190d77 100644 --- a/packages/-ember-data/tests/integration/debug-adapter-test.js +++ b/packages/-ember-data/tests/integration/debug-adapter-test.js @@ -1,7 +1,6 @@ import { setupTest } from 'ember-qunit'; import { A } from '@ember/array'; import { get } from '@ember/object'; -import { run } from '@ember/runloop'; import Model from '@ember-data/model'; import Adapter from '@ember-data/adapter'; import { module, test } from 'qunit'; @@ -156,7 +155,7 @@ module('integration/debug-adapter - DS.DebugAdapter', function(hooks) { addedRecords = updatedRecords = []; removedCount = removedIndex = null; - run(() => post.unloadRecord()); + post.unloadRecord(); await settled(); diff --git a/packages/-ember-data/tests/integration/inverse-test.js b/packages/-ember-data/tests/integration/inverse-test.js index 1c114c457b7..36d29a0d6b2 100644 --- a/packages/-ember-data/tests/integration/inverse-test.js +++ b/packages/-ember-data/tests/integration/inverse-test.js @@ -1,14 +1,9 @@ -import { run } from '@ember/runloop'; -import setupStore from 'dummy/tests/helpers/store'; - +import { setupTest } from 'ember-qunit'; import testInDebug from 'dummy/tests/helpers/test-in-debug'; import { module, test } from 'qunit'; -import DS from 'ember-data'; - -let env, store, User, Job, ReflexiveModel; - -const { attr, belongsTo } = DS; +import Model from '@ember-data/model'; +import { attr, belongsTo } from '@ember-data/model'; function stringify(string) { return function() { @@ -16,53 +11,67 @@ function stringify(string) { }; } +// @pete_the_pete refer to https://github.com/emberjs/data/commit/e34cb5a37391ce2e5d25401ca189a9bd19a29340 module('integration/inverse_test - inverseFor', function(hooks) { - hooks.beforeEach(function() { - User = DS.Model.extend({ - name: attr('string'), - bestFriend: belongsTo('user', { async: true, inverse: null }), - job: belongsTo('job', { async: false }), - }); - - User.toString = stringify('user'); - - Job = DS.Model.extend({ - isGood: attr(), - user: belongsTo('user', { async: false }), - }); - - Job.toString = stringify('job'); + setupTest(hooks); + let store; + let user; + let job; - ReflexiveModel = DS.Model.extend({ - reflexiveProp: belongsTo('reflexive-model', { async: false }), - }); - - ReflexiveModel.toString = stringify('reflexiveModel'); - - env = setupStore({ - user: User, - job: Job, - reflexiveModel: ReflexiveModel, - }); - - store = env.store; - - Job = store.modelFor('job'); - User = store.modelFor('user'); - ReflexiveModel = store.modelFor('reflexive-model'); - }); - - hooks.afterEach(function() { - run(env.container, 'destroy'); + hooks.beforeEach(function() { + let { owner } = this; + 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'); + } + } + + class ReflexiveModel extends Model { + @belongsTo('reflexive-model', { async: false }) + reflexiveProp; + + toString() { + return stringify('reflexiveModel'); + } + } + 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 inverseDefinition = Job.inverseFor('user', store); + let inverseDefinition = job.inverseFor('user', store); assert.deepEqual( inverseDefinition, { - type: User, + type: user, name: 'job', kind: 'belongsTo', options: { @@ -74,18 +83,18 @@ module('integration/inverse_test - inverseFor', function(hooks) { }); test('Finds the inverse when only one side has defined it manually', function(assert) { - Job.reopen({ + job.reopen({ owner: belongsTo('user', { inverse: 'previousJob', async: false }), }); - User.reopen({ + user.reopen({ previousJob: belongsTo('job', { async: false }), }); assert.deepEqual( - Job.inverseFor('owner', store), + job.inverseFor('owner', store), { - type: User, //the model's type + type: user, //the model's type name: 'previousJob', //the models relationship key kind: 'belongsTo', options: { @@ -96,9 +105,9 @@ module('integration/inverse_test - inverseFor', function(hooks) { ); assert.deepEqual( - User.inverseFor('previousJob', store), + user.inverseFor('previousJob', store), { - type: Job, //the model's type + type: job, //the model's type name: 'owner', //the models relationship key kind: 'belongsTo', options: { @@ -111,57 +120,55 @@ 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({ + job.reopen({ user: belongsTo('user', { inverse: 'previousJob', async: false }), }); - User.reopen({ + user.reopen({ job: belongsTo('job', { async: false }), }); - assert.equal(User.inverseFor('job', store), null, 'There is no inverse'); + 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({ + job.reopen({ user: belongsTo('user', { inverse: 'job', async: false }), owner: belongsTo('user', { inverse: 'job', async: false }), }); - User.reopen({ + user.reopen({ job: belongsTo('job', { async: false }), }); assert.expectAssertion(() => { - User.inverseFor('job', store); - }, /You defined the 'job' relationship on user, but you defined the inverse relationships of type job multiple times/i); + 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); }); test('Caches findInverseFor return value', function(assert) { assert.expect(1); - var inverseForUser = Job.inverseFor('user', store); - Job.findInverseFor = function() { + let inverseForUser = job.inverseFor('user', store); + job.findInverseFor = function() { assert.ok(false, 'Find is not called anymore'); }; - assert.equal(inverseForUser, Job.inverseFor('user', store), 'Inverse cached succesfully'); + assert.equal(inverseForUser, job.inverseFor('user', store), 'Inverse cached succesfully'); }); testInDebug('Errors out if you do not define an inverse for a reflexive relationship', function(assert) { //Maybe store is evaluated lazily, so we need this :( assert.expectWarning(() => { var reflexiveModel; - run(() => { - store.push({ - data: { - type: 'reflexive-model', - id: '1', - }, - }); - reflexiveModel = store.peekRecord('reflexive-model', 1); - reflexiveModel.get('reflexiveProp'); + store.push({ + data: { + type: 'reflexive-model', + id: '1', + }, }); + reflexiveModel = store.peekRecord('reflexive-model', 1); + reflexiveModel.get('reflexiveProp'); }, /Detected a reflexive relationship by the name of 'reflexiveProp'/); }); }); diff --git a/packages/-ember-data/tests/integration/lifecycle-hooks-test.js b/packages/-ember-data/tests/integration/lifecycle-hooks-test.js index e4f3dae82e2..2d8be462f62 100644 --- a/packages/-ember-data/tests/integration/lifecycle-hooks-test.js +++ b/packages/-ember-data/tests/integration/lifecycle-hooks-test.js @@ -1,28 +1,25 @@ import { resolve } from 'rsvp'; -import { run } from '@ember/runloop'; +import { setupTest } from 'ember-qunit'; import { deprecatedTest } from 'dummy/tests/helpers/deprecated-test'; -import setupStore from 'dummy/tests/helpers/store'; +import Model from '@ember-data/model'; +import { attr } from '@ember-data/model'; import { module } from 'qunit'; -import DS from 'ember-data'; - -let Person, env; -const { attr } = DS; - module('integration/lifecycle_hooks - Lifecycle Hooks', function(hooks) { + setupTest(hooks); + let store; + let adapter; hooks.beforeEach(function() { - Person = DS.Model.extend({ - name: attr('string'), - }); - - env = setupStore({ - person: Person, - }); - }); + let { owner } = this; + class Person extends Model { + @attr() + name; + } - hooks.afterEach(function() { - run(env.container, 'destroy'); + owner.register('model:person', Person); + store = owner.lookup('service:store'); + adapter = store.adapterFor('application'); }); deprecatedTest( @@ -31,15 +28,15 @@ module('integration/lifecycle_hooks - Lifecycle Hooks', function(hooks) { id: 'ember-data:evented-api-usage', until: '4.0', }, - function(assert) { + async function(assert) { let done = assert.async(); assert.expect(3); - env.adapter.createRecord = function(store, type, snapshot) { + adapter.createRecord = function(store, type, snapshot) { return resolve({ data: { id: 99, type: 'person', attributes: { name: 'Yehuda Katz' } } }); }; - let person = env.store.createRecord('person', { name: 'Yehuda Katz' }); + let person = store.createRecord('person', { name: 'Yehuda Katz' }); person.on('didCreate', function() { assert.equal(this, person, 'this is bound to the record'); @@ -48,7 +45,7 @@ module('integration/lifecycle_hooks - Lifecycle Hooks', function(hooks) { done(); }); - run(person, 'save'); + await person.save(); } ); @@ -58,14 +55,14 @@ module('integration/lifecycle_hooks - Lifecycle Hooks', function(hooks) { id: 'ember-data:evented-api-usage', until: '4.0', }, - function(assert) { + async function(assert) { assert.expect(3); - env.adapter.createRecord = function(store, type, snapshot) { + adapter.createRecord = function(store, type, snapshot) { return resolve(); }; - let person = env.store.createRecord('person', { id: 99, name: 'Yehuda Katz' }); + let person = store.createRecord('person', { id: 99, name: 'Yehuda Katz' }); person.on('didCreate', function() { assert.equal(this, person, 'this is bound to the record'); @@ -73,7 +70,7 @@ module('integration/lifecycle_hooks - Lifecycle Hooks', function(hooks) { assert.equal(this.get('name'), 'Yehuda Katz', 'the attribute has been assigned'); }); - run(person, 'save'); + await person.save(); } ); }); diff --git a/packages/-ember-data/tests/integration/polymorphic-belongs-to-test.js b/packages/-ember-data/tests/integration/polymorphic-belongs-to-test.js index 1af2ecaf5da..dab6a445a07 100644 --- a/packages/-ember-data/tests/integration/polymorphic-belongs-to-test.js +++ b/packages/-ember-data/tests/integration/polymorphic-belongs-to-test.js @@ -1,40 +1,39 @@ -import { run } from '@ember/runloop'; -import setupStore from 'dummy/tests/helpers/store'; - +import { setupTest } from 'ember-qunit'; import { module, test } from 'qunit'; - -import DS from 'ember-data'; - -const { attr, belongsTo } = DS; - -let store; - -const Book = DS.Model.extend({ - title: attr(), - author: belongsTo('person', { polymorphic: true, async: false }), -}); - -const Author = DS.Model.extend({ - name: attr(), -}); - -const AsyncBook = DS.Model.extend({ - author: belongsTo('person', { polymorphic: true }), -}); +import Model from '@ember-data/model'; +import { attr, belongsTo } from '@ember-data/model'; module('integration/polymorphic-belongs-to - Polymorphic BelongsTo', function(hooks) { - hooks.beforeEach(function() { - let env = setupStore({ - book: Book, - author: Author, - 'async-book': AsyncBook, - person: DS.Model.extend(), - }); - store = env.store; - }); + setupTest(hooks); + let store; - hooks.afterEach(function() { - run(store, 'destroy'); + hooks.beforeEach(function() { + let { owner } = this; + class Book extends Model { + @attr() + title; + + @belongsTo('person', { polymorphic: true, async: false }) + author; + } + + class Author extends Model { + @attr() + name; + } + + class Person extends Model {} + + class AsyncBook extends Model { + @belongsTo('person', { polymorphic: true }) + author; + } + owner.register('model:book', Book); + owner.register('model:author', Author); + owner.register('model:person', Person); + owner.register('model:async-book', AsyncBook); + + store = owner.lookup('service:store'); }); test('using store.push with a null value for a payload in relationships sets the Models relationship to null - sync relationship', assert => { @@ -61,11 +60,8 @@ module('integration/polymorphic-belongs-to - Polymorphic BelongsTo', function(ho ], }; - let book = run(() => { - store.push(payload); - return store.peekRecord('book', 1); - }); - + store.push(payload); + let book = store.peekRecord('book', 1); assert.equal(book.get('author.id'), 1); let payloadThatResetsBelongToRelationship = { @@ -81,7 +77,7 @@ module('integration/polymorphic-belongs-to - Polymorphic BelongsTo', function(ho }, }; - run(() => store.push(payloadThatResetsBelongToRelationship)); + store.push(payloadThatResetsBelongToRelationship); assert.strictEqual(book.get('author'), null); }); @@ -109,10 +105,8 @@ module('integration/polymorphic-belongs-to - Polymorphic BelongsTo', function(ho ], }; - let book = run(() => { - store.push(payload); - return store.peekRecord('async-book', 1); - }); + store.push(payload); + let book = store.peekRecord('async-book', 1); let payloadThatResetsBelongToRelationship = { data: { @@ -131,7 +125,7 @@ module('integration/polymorphic-belongs-to - Polymorphic BelongsTo', function(ho .get('author') .then(author => { assert.equal(author.get('id'), 1); - run(() => store.push(payloadThatResetsBelongToRelationship)); + store.push(payloadThatResetsBelongToRelationship); return book.get('author'); }) .then(author => { 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 62bd361780a..d170cf5aaee 100644 --- a/packages/-ember-data/tests/integration/record-array-manager-test.js +++ b/packages/-ember-data/tests/integration/record-array-manager-test.js @@ -1,36 +1,44 @@ import { A } from '@ember/array'; -import { run } from '@ember/runloop'; -import setupStore from 'dummy/tests/helpers/store'; +import { setupTest } from 'ember-qunit'; import OrderedSet from '@ember/ordered-set'; import { module, test } from 'qunit'; -import DS from 'ember-data'; +import Model from '@ember-data/model'; +import RESTAdapter from '@ember-data/adapter/rest'; +import { attr, belongsTo, hasMany } from '@ember-data/model'; -let store, env, manager; +let store, manager; -const Person = DS.Model.extend({ - name: DS.attr('string'), - cars: DS.hasMany('car', { async: false }), -}); +class Person extends Model { + @attr() + name; -const Car = DS.Model.extend({ - make: DS.attr('string'), - model: DS.attr('string'), - person: DS.belongsTo('person', { async: false }), -}); + @hasMany('car', { async: false }) + cars; +} + +class Car extends Model { + @attr() + make; + + @attr() + model; + + @belongsTo('person', { async: false }) + person; +} module('integration/record_array_manager', function(hooks) { + setupTest(hooks); hooks.beforeEach(function() { - env = setupStore({ - adapter: DS.RESTAdapter.extend(), - }); - store = env.store; + let { owner } = this; + owner.register('adapter:application', RESTAdapter); + owner.register('model:car', Car); + owner.register('model:person', Person); + store = owner.lookup('service:store'); manager = store.recordArrayManager; - - env.owner.register('model:car', Car); - env.owner.register('model:person', Person); }); function tap(obj, methodName, callback) { @@ -50,45 +58,41 @@ module('integration/record_array_manager', function(hooks) { return summary; } - test('destroying the store correctly cleans everything up', function(assert) { + test('destroying the store correctly cleans everything up', async function(assert) { let query = {}; let person; - run(() => { - store.push({ - data: { - type: 'car', - id: '1', - attributes: { - make: 'BMC', - model: 'Mini Cooper', - }, - relationships: { - person: { - data: { type: 'person', id: '1' }, - }, + store.push({ + data: { + type: 'car', + id: '1', + attributes: { + make: 'BMC', + model: 'Mini Cooper', + }, + relationships: { + person: { + data: { type: 'person', id: '1' }, }, }, - }); + }, }); - run(() => { - store.push({ - data: { - type: 'person', - id: '1', - attributes: { - name: 'Tom Dale', - }, - relationships: { - cars: { - data: [{ type: 'car', id: '1' }], - }, + store.push({ + data: { + type: 'person', + id: '1', + attributes: { + name: 'Tom Dale', + }, + relationships: { + cars: { + data: [{ type: 'car', id: '1' }], }, }, - }); - person = store.peekRecord('person', 1); + }, }); + person = store.peekRecord('person', 1); let all = store.peekAll('person'); let adapterPopulated = manager.createAdapterPopulatedRecordArray('person', query); @@ -101,20 +105,20 @@ module('integration/record_array_manager', function(hooks) { assert.equal(internalPersonModel._recordArrays.size, 1, 'expected the person to be a member of 1 recordArrays'); assert.equal('person' in manager._liveRecordArrays, true); - run(all, all.destroy); + await all.destroy(); 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); - run(manager, manager.destroy); + await manager.destroy(); 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); }); - test('batch liveRecordArray changes', function(assert) { + test('batch liveRecordArray changes', async function(assert) { let cars = store.peekAll('car'); let arrayContentWillChangeCount = 0; @@ -128,34 +132,32 @@ module('integration/record_array_manager', function(hooks) { assert.deepEqual(cars.toArray(), []); assert.equal(arrayContentWillChangeCount, 0, 'expected NO arrayChangeEvents yet'); - run(() => { - store.push({ - data: [ - { - type: 'car', - id: '1', - attributes: { - make: 'BMC', - model: 'Mini Cooper', - }, + await store.push({ + data: [ + { + type: 'car', + id: '1', + attributes: { + make: 'BMC', + model: 'Mini Cooper', }, - { - type: 'car', - id: '2', - attributes: { - make: 'Jeep', - model: 'Wrangler', - }, + }, + { + type: 'car', + id: '2', + attributes: { + make: 'Jeep', + model: 'Wrangler', }, - ], - }); + }, + ], }); assert.equal(arrayContentWillChangeCount, 1, 'expected ONE array change event'); assert.deepEqual(cars.toArray(), [store.peekRecord('car', 1), store.peekRecord('car', 2)]); - run(() => store.peekRecord('car', 1).set('model', 'Mini')); + store.peekRecord('car', 1).set('model', 'Mini'); assert.equal(arrayContentWillChangeCount, 1, 'expected ONE array change event'); @@ -168,60 +170,56 @@ module('integration/record_array_manager', function(hooks) { arrayContentWillChangeCount = 0; - run(() => { - store.push({ - data: [ - { - type: 'car', - id: 2, // this ID is already present, array wont need to change - attributes: { - make: 'Tesla', - model: 'S', - }, + await store.push({ + data: [ + { + type: 'car', + id: 2, // this ID is already present, array wont need to change + attributes: { + make: 'Tesla', + model: 'S', }, - ], - }); + }, + ], }); assert.equal(arrayContentWillChangeCount, 0, 'expected NO array change events'); - run(() => { - store.push({ - data: [ - { - type: 'car', - id: 3, - attributes: { - make: 'Tesla', - model: 'S', - }, + await store.push({ + data: [ + { + type: 'car', + id: 3, + attributes: { + make: 'Tesla', + model: 'S', }, - ], - }); + }, + ], }); assert.equal(arrayContentWillChangeCount, 1, 'expected ONE array change event'); + // reset function so it doesn't execute after test finishes and store is torn down + cars.arrayContentWillChange = function() {}; }); - test('#GH-4041 store#query AdapterPopulatedRecordArrays are removed from their managers instead of retained when #destroy is called', function(assert) { - run(() => { - store.push({ - data: { - type: 'car', - id: '1', - attributes: { - make: 'Honda', - model: 'fit', - }, + test('#GH-4041 store#query AdapterPopulatedRecordArrays are removed from their managers instead of retained when #destroy is called', async function(assert) { + store.push({ + data: { + type: 'car', + id: '1', + attributes: { + make: 'Honda', + model: 'fit', }, - }); + }, }); const query = {}; let adapterPopulated = manager.createAdapterPopulatedRecordArray('car', query); - run(() => adapterPopulated.destroy()); + await adapterPopulated.destroy(); assert.equal(manager._adapterPopulatedRecordArrays.length, 0); }); @@ -274,17 +272,15 @@ module('integration/record_array_manager', function(hooks) { return superCreateRecordArray.apply(this, arguments); }; - run(() => { - store.push({ - data: { - type: 'car', - id: '1', - attributes: { - make: 'BMC', - model: 'Mini Cooper', - }, + store.push({ + data: { + type: 'car', + id: '1', + attributes: { + make: 'BMC', + model: 'Mini Cooper', }, - }); + }, }); assert.equal(createRecordArrayCalled, 0, 'no record array has been created yet'); diff --git a/packages/-ember-data/tests/integration/snapshot-test.js b/packages/-ember-data/tests/integration/snapshot-test.js index a70eb5d670a..d0a59e5b1b3 100644 --- a/packages/-ember-data/tests/integration/snapshot-test.js +++ b/packages/-ember-data/tests/integration/snapshot-test.js @@ -1,48 +1,55 @@ import { resolve } from 'rsvp'; -import { run } from '@ember/runloop'; -import setupStore from 'dummy/tests/helpers/store'; - import { module, test } from 'qunit'; +import { setupTest } from 'ember-qunit'; -import DS from 'ember-data'; -const { Model, attr, hasMany, belongsTo, Snapshot } = DS; +import Model from '@ember-data/model'; +import { Snapshot } from 'ember-data/-private'; +import { attr, belongsTo, hasMany } from '@ember-data/model'; -let env, Post, Comment; +let owner, store, _Post; module('integration/snapshot - Snapshot', function(hooks) { + setupTest(hooks); hooks.beforeEach(function() { - Post = Model.extend({ - author: attr(), - title: attr(), - comments: hasMany({ async: true }), - }); - Comment = Model.extend({ - body: attr(), - post: belongsTo({ async: true }), - }); - - env = setupStore({ - post: Post, - comment: Comment, - }); - }); - - hooks.afterEach(function() { - run(() => { - env.store.destroy(); - }); + class Post extends Model { + @attr() + author; + + @attr() + title; + + @hasMany({ async: true }) + comments; + } + + class Comment extends Model { + @attr() + body; + + @belongsTo({ async: true }) + post; + } + _Post = Post; + + owner = this.owner; + owner.register('model:post', Post); + owner.register('model:comment', Comment); + store = owner.lookup('service:store'); }); test('snapshot.attributes() includes defaultValues when appropriate', function(assert) { - const Address = Model.extend({ - street: attr(), - country: attr({ defaultValue: 'USA' }), - state: attr({ defaultValue: () => 'CA' }), - }); + class Address extends Model { + @attr() + street; + + @attr({ defaultValue: 'USA' }) + country; + + @attr({ defaultValue: () => 'CA' }) + state; + } + owner.register('model:address', Address); - let { store } = setupStore({ - address: Address, - }); let newAddress = store.createRecord('address', {}); let snapshot = newAddress._createSnapshot(); let expected = { @@ -54,85 +61,79 @@ module('integration/snapshot - Snapshot', function(hooks) { assert.ok(snapshot instanceof Snapshot, 'snapshot is an instance of Snapshot'); assert.deepEqual(snapshot.attributes(), expected, 'We generated attributes with default values'); - run(() => store.destroy()); + store.destroy(); }); test('record._createSnapshot() returns a snapshot', function(assert) { assert.expect(1); - run(() => { - env.store.push({ - data: { - type: 'post', - id: '1', - attributes: { - title: 'Hello World', - }, + store.push({ + data: { + type: 'post', + id: '1', + attributes: { + title: 'Hello World', }, - }); - let post = env.store.peekRecord('post', 1); - let snapshot = post._createSnapshot(); - - assert.ok(snapshot instanceof Snapshot, 'snapshot is an instance of Snapshot'); + }, }); + let post = store.peekRecord('post', 1); + let snapshot = post._createSnapshot(); + + assert.ok(snapshot instanceof Snapshot, 'snapshot is an instance of Snapshot'); }); test('snapshot.id, snapshot.type and snapshot.modelName returns correctly', function(assert) { assert.expect(3); - run(() => { - env.store.push({ - data: { - type: 'post', - id: '1', - attributes: { - title: 'Hello World', - }, + store.push({ + data: { + type: 'post', + id: '1', + attributes: { + title: 'Hello World', }, - }); - let post = env.store.peekRecord('post', 1); - let snapshot = post._createSnapshot(); - - assert.equal(snapshot.id, '1', 'id is correct'); - assert.ok(Model.detect(snapshot.type), 'type is correct'); - assert.equal(snapshot.modelName, 'post', 'modelName is correct'); + }, }); + let post = store.peekRecord('post', 1); + let snapshot = post._createSnapshot(); + + assert.equal(snapshot.id, '1', 'id is correct'); + assert.ok(Model.detect(snapshot.type), 'type is correct'); + assert.equal(snapshot.modelName, 'post', 'modelName is correct'); }); - test('snapshot.type loads the class lazily', function(assert) { + test('snapshot.type loads the class lazily', async function(assert) { assert.expect(3); let postClassLoaded = false; - let modelFactoryFor = env.store._modelFactoryFor; - env.store._modelFactoryFor = name => { + let modelFactoryFor = store._modelFactoryFor; + store._modelFactoryFor = name => { if (name === 'post') { postClassLoaded = true; } - return modelFactoryFor.call(env.store, name); + return modelFactoryFor.call(store, name); }; - run(() => { - env.store._push({ - data: { - type: 'post', - id: '1', - attributes: { - title: 'Hello World', - }, + await store._push({ + data: { + type: 'post', + id: '1', + attributes: { + title: 'Hello World', }, - }); - let postInternalModel = env.store._internalModelForId('post', 1); - let snapshot = postInternalModel.createSnapshot(); - - assert.equal(false, postClassLoaded, 'model class is not eagerly loaded'); - assert.equal(snapshot.type, Post, 'type is correct'); - assert.equal(true, postClassLoaded, 'model class is loaded'); + }, }); + let postInternalModel = store._internalModelForId('post', 1); + let snapshot = await postInternalModel.createSnapshot(); + + assert.equal(false, postClassLoaded, 'model class is not eagerly loaded'); + assert.equal(snapshot.type, _Post, 'type is correct'); + assert.equal(true, postClassLoaded, 'model class is loaded'); }); test('an initial findRecord call has no record for internal-model when a snapshot is generated', function(assert) { assert.expect(2); - env.adapter.findRecord = (store, type, id, snapshot) => { + store.adapterFor('application').findRecord = (store, type, id, snapshot) => { assert.equal(snapshot._internalModel.hasRecord, false, 'We do not have a materialized record'); assert.equal(snapshot.__attributes, null, 'attributes were not populated initially'); return resolve({ @@ -146,25 +147,23 @@ module('integration/snapshot - Snapshot', function(hooks) { }); }; - run(() => env.store.findRecord('post', '1')); + store.findRecord('post', '1'); }); test('snapshots for un-materialized internal-models generate attributes lazily', function(assert) { assert.expect(2); - run(() => - env.store._push({ - data: { - type: 'post', - id: '1', - attributes: { - title: 'Hello World', - }, + store._push({ + data: { + type: 'post', + id: '1', + attributes: { + title: 'Hello World', }, - }) - ); + }, + }); - let postInternalModel = env.store._internalModelForId('post', 1); + let postInternalModel = store._internalModelForId('post', 1); let snapshot = postInternalModel.createSnapshot(); let expected = { author: undefined, @@ -179,19 +178,17 @@ module('integration/snapshot - Snapshot', function(hooks) { test('snapshots for materialized internal-models generate attributes greedily', function(assert) { assert.expect(1); - run(() => - env.store.push({ - data: { - type: 'post', - id: '1', - attributes: { - title: 'Hello World', - }, + store.push({ + data: { + type: 'post', + id: '1', + attributes: { + title: 'Hello World', }, - }) - ); + }, + }); - let postInternalModel = env.store._internalModelForId('post', 1); + let postInternalModel = store._internalModelForId('post', 1); let snapshot = postInternalModel.createSnapshot(); let expected = { author: undefined, @@ -204,323 +201,289 @@ module('integration/snapshot - Snapshot', function(hooks) { test('snapshot.attr() does not change when record changes', function(assert) { assert.expect(2); - run(() => { - env.store.push({ - data: { - type: 'post', - id: '1', - attributes: { - title: 'Hello World', - }, + store.push({ + data: { + type: 'post', + id: '1', + attributes: { + title: 'Hello World', }, - }); - let post = env.store.peekRecord('post', 1); - let snapshot = post._createSnapshot(); - - assert.equal(snapshot.attr('title'), 'Hello World', 'snapshot title is correct'); - post.set('title', 'Tomster'); - assert.equal(snapshot.attr('title'), 'Hello World', 'snapshot title is still correct'); + }, }); + let post = store.peekRecord('post', 1); + let snapshot = post._createSnapshot(); + + assert.equal(snapshot.attr('title'), 'Hello World', 'snapshot title is correct'); + post.set('title', 'Tomster'); + assert.equal(snapshot.attr('title'), 'Hello World', 'snapshot title is still correct'); }); test('snapshot.attr() throws an error attribute not found', function(assert) { assert.expect(1); - run(() => { - env.store.push({ - data: { - type: 'post', - id: '1', - attributes: { - title: 'Hello World', - }, - }, - }); - let post = env.store.peekRecord('post', 1); - let snapshot = post._createSnapshot(); - - assert.throws( - () => { - snapshot.attr('unknown'); + store.push({ + data: { + type: 'post', + id: '1', + attributes: { + title: 'Hello World', }, - /has no attribute named 'unknown' defined/, - 'attr throws error' - ); + }, }); + let post = store.peekRecord('post', 1); + let snapshot = post._createSnapshot(); + + assert.throws( + () => { + snapshot.attr('unknown'); + }, + /has no attribute named 'unknown' defined/, + 'attr throws error' + ); }); test('snapshot.attributes() returns a copy of all attributes for the current snapshot', function(assert) { assert.expect(1); - run(() => { - env.store.push({ - data: { - type: 'post', - id: '1', - attributes: { - title: 'Hello World', - }, + store.push({ + data: { + type: 'post', + id: '1', + attributes: { + title: 'Hello World', }, - }); - let post = env.store.peekRecord('post', 1); - let snapshot = post._createSnapshot(); + }, + }); + let post = store.peekRecord('post', 1); + let snapshot = post._createSnapshot(); - let attributes = snapshot.attributes(); + let attributes = snapshot.attributes(); - assert.deepEqual(attributes, { author: undefined, title: 'Hello World' }, 'attributes are returned correctly'); - }); + assert.deepEqual(attributes, { author: undefined, title: 'Hello World' }, 'attributes are returned correctly'); }); test('snapshot.changedAttributes() returns a copy of all changed attributes for the current snapshot', function(assert) { assert.expect(1); - run(() => { - env.store.push({ - data: { - type: 'post', - id: '1', - attributes: { - title: 'Hello World', - }, + store.push({ + data: { + type: 'post', + id: '1', + attributes: { + title: 'Hello World', }, - }); - let post = env.store.peekRecord('post', 1); - post.set('title', 'Hello World!'); - let snapshot = post._createSnapshot(); + }, + }); + let post = store.peekRecord('post', 1); + post.set('title', 'Hello World!'); + let snapshot = post._createSnapshot(); - let changes = snapshot.changedAttributes(); + let changes = snapshot.changedAttributes(); - assert.deepEqual(changes.title, ['Hello World', 'Hello World!'], 'changed attributes are returned correctly'); - }); + assert.deepEqual(changes.title, ['Hello World', 'Hello World!'], 'changed attributes are returned correctly'); }); test('snapshot.belongsTo() returns undefined if relationship is undefined', function(assert) { assert.expect(1); - run(() => { - env.store.push({ - data: { - type: 'comment', - id: '1', - attributes: { - body: 'This is comment', - }, + store.push({ + data: { + type: 'comment', + id: '1', + attributes: { + body: 'This is comment', }, - }); - let comment = env.store.peekRecord('comment', 1); - let snapshot = comment._createSnapshot(); - let relationship = snapshot.belongsTo('post'); - - assert.equal(relationship, undefined, 'relationship is undefined'); + }, }); + let comment = store.peekRecord('comment', 1); + let snapshot = comment._createSnapshot(); + let relationship = snapshot.belongsTo('post'); + + assert.equal(relationship, undefined, 'relationship is undefined'); }); test('snapshot.belongsTo() returns null if relationship is unset', function(assert) { assert.expect(1); - run(() => { - env.store.push({ - data: [ - { - type: 'post', - id: '1', - attributes: { - title: 'Hello World', - }, + store.push({ + data: [ + { + type: 'post', + id: '1', + attributes: { + title: 'Hello World', }, - { - type: 'comment', - id: '2', - attributes: { - body: 'This is comment', - }, - relationships: { - post: { - data: null, - }, + }, + { + type: 'comment', + id: '2', + attributes: { + body: 'This is comment', + }, + relationships: { + post: { + data: null, }, }, - ], - }); - let comment = env.store.peekRecord('comment', 2); - let snapshot = comment._createSnapshot(); - let relationship = snapshot.belongsTo('post'); - - assert.equal(relationship, null, 'relationship is unset'); + }, + ], }); + let comment = store.peekRecord('comment', 2); + let snapshot = comment._createSnapshot(); + let relationship = snapshot.belongsTo('post'); + + assert.equal(relationship, null, 'relationship is unset'); }); test('snapshot.belongsTo() returns a snapshot if relationship is set', function(assert) { assert.expect(3); - run(() => { - env.store.push({ - data: [ - { - type: 'post', - id: '1', - attributes: { - title: 'Hello World', - }, + store.push({ + data: [ + { + type: 'post', + id: '1', + attributes: { + title: 'Hello World', }, - { - type: 'comment', - id: '2', - attributes: { - body: 'This is comment', - }, - relationships: { - post: { - data: { type: 'post', id: '1' }, - }, + }, + { + type: 'comment', + id: '2', + attributes: { + body: 'This is comment', + }, + relationships: { + post: { + data: { type: 'post', id: '1' }, }, }, - ], - }); - let comment = env.store.peekRecord('comment', 2); - let snapshot = comment._createSnapshot(); - let relationship = snapshot.belongsTo('post'); - - assert.ok(relationship instanceof Snapshot, 'snapshot is an instance of Snapshot'); - assert.equal(relationship.id, '1', 'post id is correct'); - assert.equal(relationship.attr('title'), 'Hello World', 'post title is correct'); + }, + ], }); + let comment = store.peekRecord('comment', 2); + let snapshot = comment._createSnapshot(); + let relationship = snapshot.belongsTo('post'); + + assert.ok(relationship instanceof Snapshot, 'snapshot is an instance of Snapshot'); + assert.equal(relationship.id, '1', 'post id is correct'); + assert.equal(relationship.attr('title'), 'Hello World', 'post title is correct'); }); test('snapshot.belongsTo() returns null if relationship is deleted', function(assert) { assert.expect(1); - run(() => { - env.store.push({ - data: [ - { - type: 'post', - id: '1', - attributes: { - title: 'Hello World', - }, + store.push({ + data: [ + { + type: 'post', + id: '1', + attributes: { + title: 'Hello World', }, - { - type: 'comment', - id: '2', - attributes: { - body: 'This is comment', - }, - relationships: { - post: { - data: { type: 'post', id: '1' }, - }, + }, + { + type: 'comment', + id: '2', + attributes: { + body: 'This is comment', + }, + relationships: { + post: { + data: { type: 'post', id: '1' }, }, }, - ], - }); - let post = env.store.peekRecord('post', 1); - let comment = env.store.peekRecord('comment', 2); + }, + ], + }); + let post = store.peekRecord('post', 1); + let comment = store.peekRecord('comment', 2); - post.deleteRecord(); + post.deleteRecord(); - let snapshot = comment._createSnapshot(); - let relationship = snapshot.belongsTo('post'); + let snapshot = comment._createSnapshot(); + let relationship = snapshot.belongsTo('post'); - assert.equal(relationship, null, 'relationship unset after deleted'); - }); + assert.equal(relationship, null, 'relationship unset after deleted'); }); test('snapshot.belongsTo() returns undefined if relationship is a link', function(assert) { assert.expect(1); - run(() => { - env.store.push({ - data: { - type: 'comment', - id: '2', - attributes: { - body: 'This is comment', - }, - relationships: { - post: { - links: { - related: 'post', - }, + store.push({ + data: { + type: 'comment', + id: '2', + attributes: { + body: 'This is comment', + }, + relationships: { + post: { + links: { + related: 'post', }, }, }, - }); - let comment = env.store.peekRecord('comment', 2); - let snapshot = comment._createSnapshot(); - let relationship = snapshot.belongsTo('post'); - - assert.equal(relationship, undefined, 'relationship is undefined'); + }, }); + let comment = store.peekRecord('comment', 2); + let snapshot = comment._createSnapshot(); + let relationship = snapshot.belongsTo('post'); + + assert.equal(relationship, undefined, 'relationship is undefined'); }); test("snapshot.belongsTo() throws error if relation doesn't exist", function(assert) { assert.expect(1); - run(() => { - env.store.push({ - data: { - type: 'post', - id: '1', - attributes: { - title: 'Hello World', - }, - }, - }); - let post = env.store.peekRecord('post', 1); - let snapshot = post._createSnapshot(); - - assert.throws( - () => { - snapshot.belongsTo('unknown'); + store.push({ + data: { + type: 'post', + id: '1', + attributes: { + title: 'Hello World', }, - /has no belongsTo relationship named 'unknown'/, - 'throws error' - ); + }, }); + let post = store.peekRecord('post', 1); + let snapshot = post._createSnapshot(); + + assert.throws( + () => { + snapshot.belongsTo('unknown'); + }, + /has no belongsTo relationship named 'unknown'/, + 'throws error' + ); }); - test('snapshot.belongsTo() returns a snapshot if relationship link has been fetched', function(assert) { + test('snapshot.belongsTo() returns a snapshot if relationship link has been fetched', async function(assert) { assert.expect(2); - env.adapter.findBelongsTo = function(store, snapshot, link, relationship) { + store.adapterFor('application').findBelongsTo = function(store, snapshot, link, relationship) { return resolve({ data: { id: 1, type: 'post', attributes: { title: 'Hello World' } } }); }; - return run(() => { - env.store.push({ - data: { - type: 'comment', - id: '2', - attributes: { - body: 'This is comment', - }, - relationships: { - post: { - links: { - related: 'post', - }, + store.push({ + data: { + type: 'comment', + id: '2', + attributes: { + body: 'This is comment', + }, + relationships: { + post: { + links: { + related: 'post', }, }, }, - }); - let comment = env.store.peekRecord('comment', 2); - - return comment.get('post').then(post => { - let snapshot = comment._createSnapshot(); - let relationship = snapshot.belongsTo('post'); - - assert.ok(relationship instanceof Snapshot, 'snapshot is an instance of Snapshot'); - assert.equal(relationship.id, '1', 'post id is correct'); - }); + }, }); - }); - - test('snapshot.belongsTo() and snapshot.hasMany() returns correctly when adding an object to a hasMany relationship', function(assert) { - assert.expect(4); + let comment = store.peekRecord('comment', 2); - return run(() => { - env.store.push({ + await comment.get('post').then(post => { + store.push({ data: [ { type: 'post', @@ -538,10 +501,9 @@ module('integration/snapshot - Snapshot', function(hooks) { }, ], }); - let post = env.store.peekRecord('post', 1); - let comment = env.store.peekRecord('comment', 2); + let comment = store.peekRecord('comment', 2); - return post.get('comments').then(comments => { + post.get('comments').then(comments => { comments.addObject(comment); let postSnapshot = post._createSnapshot(); @@ -563,32 +525,32 @@ module('integration/snapshot - Snapshot', function(hooks) { }); }); - test('snapshot.belongsTo() and snapshot.hasMany() returns correctly when setting an object to a belongsTo relationship', function(assert) { + test('snapshot.belongsTo() and snapshot.hasMany() returns correctly when adding an object to a hasMany relationship', async function(assert) { assert.expect(4); - run(() => { - env.store.push({ - data: [ - { - type: 'post', - id: '1', - attributes: { - title: 'Hello World', - }, + store.push({ + data: [ + { + type: 'post', + id: '1', + attributes: { + title: 'Hello World', }, - { - type: 'comment', - id: '2', - attributes: { - body: 'This is comment', - }, + }, + { + type: 'comment', + id: '2', + attributes: { + body: 'This is comment', }, - ], - }); - let post = env.store.peekRecord('post', 1); - let comment = env.store.peekRecord('comment', 2); + }, + ], + }); + let post = store.peekRecord('post', 1); + let comment = store.peekRecord('comment', 2); - comment.set('post', post); + await post.get('comments').then(comments => { + comments.addObject(comment); let postSnapshot = post._createSnapshot(); let commentSnapshot = comment._createSnapshot(); @@ -608,471 +570,483 @@ module('integration/snapshot - Snapshot', function(hooks) { }); }); - test('snapshot.belongsTo() returns ID if option.id is set', function(assert) { - assert.expect(1); + test('snapshot.belongsTo() and snapshot.hasMany() returns correctly when setting an object to a belongsTo relationship', function(assert) { + assert.expect(4); - run(() => { - env.store.push({ - data: [ - { - type: 'post', - id: '1', - attributes: { - title: 'Hello World', - }, + store.push({ + data: [ + { + type: 'post', + id: '1', + attributes: { + title: 'Hello World', }, - { - type: 'comment', - id: '2', - attributes: { - body: 'This is comment', - }, - relationships: { - post: { - data: { type: 'post', id: '1' }, - }, - }, + }, + { + type: 'comment', + id: '2', + attributes: { + body: 'This is comment', }, - ], - }); - let comment = env.store.peekRecord('comment', 2); - let snapshot = comment._createSnapshot(); - let relationship = snapshot.belongsTo('post', { id: true }); - - assert.equal(relationship, '1', 'relationship ID correctly returned'); + }, + ], }); - }); + let post = store.peekRecord('post', 1); + let comment = store.peekRecord('comment', 2); - test('snapshot.belongsTo() returns null if option.id is set but relationship was deleted', function(assert) { - assert.expect(1); + comment.set('post', post); - run(() => { - env.store.push({ - data: [ - { - type: 'post', - id: '1', - attributes: { - title: 'Hello World', - }, - }, - { - type: 'comment', - id: '2', - attributes: { - body: 'This is comment', - }, - relationships: { - post: { - data: { type: 'post', id: '1' }, - }, - }, - }, - ], - }); - let post = env.store.peekRecord('post', 1); - let comment = env.store.peekRecord('comment', 2); + let postSnapshot = post._createSnapshot(); + let commentSnapshot = comment._createSnapshot(); - post.deleteRecord(); + let hasManyRelationship = postSnapshot.hasMany('comments'); + let belongsToRelationship = commentSnapshot.belongsTo('post'); - let snapshot = comment._createSnapshot(); - let relationship = snapshot.belongsTo('post', { id: true }); + assert.ok(hasManyRelationship instanceof Array, 'hasMany relationship is an instance of Array'); + assert.equal(hasManyRelationship.length, 1, 'hasMany relationship contains related object'); - assert.equal(relationship, null, 'relationship unset after deleted'); - }); + assert.ok(belongsToRelationship instanceof Snapshot, 'belongsTo relationship is an instance of Snapshot'); + assert.equal(belongsToRelationship.attr('title'), 'Hello World', 'belongsTo relationship contains related object'); }); - test('snapshot.hasMany() returns undefined if relationship is undefined', function(assert) { + test('snapshot.belongsTo() returns ID if option.id is set', function(assert) { assert.expect(1); - run(() => { - env.store.push({ - data: { + store.push({ + data: [ + { type: 'post', id: '1', attributes: { title: 'Hello World', }, }, - }); - let post = env.store.peekRecord('post', 1); - let snapshot = post._createSnapshot(); - let relationship = snapshot.hasMany('comments'); - - assert.equal(relationship, undefined, 'relationship is undefined'); + { + type: 'comment', + id: '2', + attributes: { + body: 'This is comment', + }, + relationships: { + post: { + data: { type: 'post', id: '1' }, + }, + }, + }, + ], }); + let comment = store.peekRecord('comment', 2); + let snapshot = comment._createSnapshot(); + let relationship = snapshot.belongsTo('post', { id: true }); + + assert.equal(relationship, '1', 'relationship ID correctly returned'); }); - test('snapshot.hasMany() returns empty array if relationship is empty', function(assert) { - assert.expect(2); + test('snapshot.belongsTo() returns null if option.id is set but relationship was deleted', function(assert) { + assert.expect(1); - run(() => { - env.store.push({ - data: { + store.push({ + data: [ + { type: 'post', id: '1', attributes: { title: 'Hello World', }, + }, + { + type: 'comment', + id: '2', + attributes: { + body: 'This is comment', + }, relationships: { - comments: { - data: [], + post: { + data: { type: 'post', id: '1' }, }, }, }, - }); - let post = env.store.peekRecord('post', 1); - let snapshot = post._createSnapshot(); - let relationship = snapshot.hasMany('comments'); + ], + }); + let post = store.peekRecord('post', 1); + let comment = store.peekRecord('comment', 2); - assert.ok(relationship instanceof Array, 'relationship is an instance of Array'); - assert.equal(relationship.length, 0, 'relationship is empty'); + post.deleteRecord(); + + let snapshot = comment._createSnapshot(); + let relationship = snapshot.belongsTo('post', { id: true }); + + assert.equal(relationship, null, 'relationship unset after deleted'); + }); + + test('snapshot.hasMany() returns undefined if relationship is undefined', function(assert) { + assert.expect(1); + + store.push({ + data: { + type: 'post', + id: '1', + attributes: { + title: 'Hello World', + }, + }, + }); + let post = store.peekRecord('post', 1); + let snapshot = post._createSnapshot(); + let relationship = snapshot.hasMany('comments'); + + assert.equal(relationship, undefined, 'relationship is undefined'); + }); + + test('snapshot.hasMany() returns empty array if relationship is empty', function(assert) { + assert.expect(2); + + store.push({ + data: { + type: 'post', + id: '1', + attributes: { + title: 'Hello World', + }, + relationships: { + comments: { + data: [], + }, + }, + }, }); + let post = store.peekRecord('post', 1); + let snapshot = post._createSnapshot(); + let relationship = snapshot.hasMany('comments'); + + assert.ok(relationship instanceof Array, 'relationship is an instance of Array'); + assert.equal(relationship.length, 0, 'relationship is empty'); }); test('snapshot.hasMany() returns array of snapshots if relationship is set', function(assert) { assert.expect(5); - run(() => { - env.store.push({ - data: [ - { - type: 'comment', - id: '1', - attributes: { - body: 'This is the first comment', - }, + store.push({ + data: [ + { + type: 'comment', + id: '1', + attributes: { + body: 'This is the first comment', }, - { - type: 'comment', - id: '2', - attributes: { - body: 'This is the second comment', - }, + }, + { + type: 'comment', + id: '2', + attributes: { + body: 'This is the second comment', }, - { - type: 'post', - id: '3', - attributes: { - title: 'Hello World', - }, - relationships: { - comments: { - data: [{ type: 'comment', id: '1' }, { type: 'comment', id: '2' }], - }, + }, + { + type: 'post', + id: '3', + attributes: { + title: 'Hello World', + }, + relationships: { + comments: { + data: [{ type: 'comment', id: '1' }, { type: 'comment', id: '2' }], }, }, - ], - }); - let post = env.store.peekRecord('post', 3); - let snapshot = post._createSnapshot(); - let relationship = snapshot.hasMany('comments'); - - assert.ok(relationship instanceof Array, 'relationship is an instance of Array'); - assert.equal(relationship.length, 2, 'relationship has two items'); + }, + ], + }); + let post = store.peekRecord('post', 3); + let snapshot = post._createSnapshot(); + let relationship = snapshot.hasMany('comments'); - let relationship1 = relationship[0]; + assert.ok(relationship instanceof Array, 'relationship is an instance of Array'); + assert.equal(relationship.length, 2, 'relationship has two items'); - assert.ok(relationship1 instanceof Snapshot, 'relationship item is an instance of Snapshot'); + let relationship1 = relationship[0]; - assert.equal(relationship1.id, '1', 'relationship item id is correct'); - assert.equal(relationship1.attr('body'), 'This is the first comment', 'relationship item body is correct'); - }); + assert.ok(relationship1 instanceof Snapshot, 'relationship item is an instance of Snapshot'); + assert.equal(relationship1.id, '1', 'relationship item id is correct'); + assert.equal(relationship1.attr('body'), 'This is the first comment', 'relationship item body is correct'); }); test('snapshot.hasMany() returns empty array if relationship records are deleted', function(assert) { assert.expect(2); - run(() => { - env.store.push({ - data: [ - { - type: 'comment', - id: '1', - attributes: { - body: 'This is the first comment', - }, + store.push({ + data: [ + { + type: 'comment', + id: '1', + attributes: { + body: 'This is the first comment', }, - { - type: 'comment', - id: '2', - attributes: { - body: 'This is the second comment', - }, + }, + { + type: 'comment', + id: '2', + attributes: { + body: 'This is the second comment', }, - { - type: 'post', - id: '3', - attributes: { - title: 'Hello World', - }, - relationships: { - comments: { - data: [{ type: 'comment', id: '1' }, { type: 'comment', id: '2' }], - }, + }, + { + type: 'post', + id: '3', + attributes: { + title: 'Hello World', + }, + relationships: { + comments: { + data: [{ type: 'comment', id: '1' }, { type: 'comment', id: '2' }], }, }, - ], - }); - let comment1 = env.store.peekRecord('comment', 1); - let comment2 = env.store.peekRecord('comment', 2); - let post = env.store.peekRecord('post', 3); + }, + ], + }); + let comment1 = store.peekRecord('comment', 1); + let comment2 = store.peekRecord('comment', 2); + let post = store.peekRecord('post', 3); - comment1.deleteRecord(); - comment2.deleteRecord(); + comment1.deleteRecord(); + comment2.deleteRecord(); - let snapshot = post._createSnapshot(); - let relationship = snapshot.hasMany('comments'); + let snapshot = post._createSnapshot(); + let relationship = snapshot.hasMany('comments'); - assert.ok(relationship instanceof Array, 'relationship is an instance of Array'); - assert.equal(relationship.length, 0, 'relationship is empty'); - }); + assert.ok(relationship instanceof Array, 'relationship is an instance of Array'); + assert.equal(relationship.length, 0, 'relationship is empty'); }); test('snapshot.hasMany() returns array of IDs if option.ids is set', function(assert) { assert.expect(1); - run(() => { - env.store.push({ - data: { - type: 'post', - id: '1', - attributes: { - title: 'Hello World', - }, - relationships: { - comments: { - data: [{ type: 'comment', id: '2' }, { type: 'comment', id: '3' }], - }, + store.push({ + data: { + type: 'post', + id: '1', + attributes: { + title: 'Hello World', + }, + relationships: { + comments: { + data: [{ type: 'comment', id: '2' }, { type: 'comment', id: '3' }], }, }, - }); - let post = env.store.peekRecord('post', 1); - let snapshot = post._createSnapshot(); - let relationship = snapshot.hasMany('comments', { ids: true }); - - assert.deepEqual(relationship, ['2', '3'], 'relationship IDs correctly returned'); + }, }); + let post = store.peekRecord('post', 1); + let snapshot = post._createSnapshot(); + let relationship = snapshot.hasMany('comments', { ids: true }); + + assert.deepEqual(relationship, ['2', '3'], 'relationship IDs correctly returned'); }); test('snapshot.hasMany() returns empty array of IDs if option.ids is set but relationship records were deleted', function(assert) { assert.expect(2); - run(() => { - env.store.push({ - data: [ - { - type: 'comment', - id: '1', - attributes: { - body: 'This is the first comment', - }, + store.push({ + data: [ + { + type: 'comment', + id: '1', + attributes: { + body: 'This is the first comment', }, - { - type: 'comment', - id: '2', - attributes: { - body: 'This is the second comment', - }, + }, + { + type: 'comment', + id: '2', + attributes: { + body: 'This is the second comment', }, - { - type: 'post', - id: '3', - attributes: { - title: 'Hello World', - }, - relationships: { - comments: { - data: [{ type: 'comment', id: '1' }, { type: 'comment', id: '2' }], - }, + }, + { + type: 'post', + id: '3', + attributes: { + title: 'Hello World', + }, + relationships: { + comments: { + data: [{ type: 'comment', id: '1' }, { type: 'comment', id: '2' }], }, }, - ], - }); - let comment1 = env.store.peekRecord('comment', 1); - let comment2 = env.store.peekRecord('comment', 2); - let post = env.store.peekRecord('post', 3); + }, + ], + }); + let comment1 = store.peekRecord('comment', 1); + let comment2 = store.peekRecord('comment', 2); + let post = store.peekRecord('post', 3); - comment1.deleteRecord(); - comment2.deleteRecord(); + comment1.deleteRecord(); + comment2.deleteRecord(); - let snapshot = post._createSnapshot(); - let relationship = snapshot.hasMany('comments', { ids: true }); + let snapshot = post._createSnapshot(); + let relationship = snapshot.hasMany('comments', { ids: true }); - assert.ok(relationship instanceof Array, 'relationship is an instance of Array'); - assert.equal(relationship.length, 0, 'relationship is empty'); - }); + assert.ok(relationship instanceof Array, 'relationship is an instance of Array'); + assert.equal(relationship.length, 0, 'relationship is empty'); }); test('snapshot.hasMany() returns undefined if relationship is a link', function(assert) { assert.expect(1); - run(() => { - env.store.push({ - data: { - type: 'post', - id: '1', - attributes: { - title: 'Hello World', - }, - relationships: { - comments: { - links: { - related: 'comments', - }, + store.push({ + data: { + type: 'post', + id: '1', + attributes: { + title: 'Hello World', + }, + relationships: { + comments: { + links: { + related: 'comments', }, }, }, - }); - let post = env.store.peekRecord('post', 1); - let snapshot = post._createSnapshot(); - let relationship = snapshot.hasMany('comments'); - - assert.equal(relationship, undefined, 'relationship is undefined'); + }, }); + let post = store.peekRecord('post', 1); + let snapshot = post._createSnapshot(); + let relationship = snapshot.hasMany('comments'); + + assert.equal(relationship, undefined, 'relationship is undefined'); }); - test('snapshot.hasMany() returns array of snapshots if relationship link has been fetched', function(assert) { + test('snapshot.hasMany() returns array of snapshots if relationship link has been fetched', async function(assert) { assert.expect(2); - env.adapter.findHasMany = function(store, snapshot, link, relationship) { + store.adapterFor('application').findHasMany = function(store, snapshot, link, relationship) { return resolve({ data: [{ id: 2, type: 'comment', attributes: { body: 'This is comment' } }], }); }; - return run(() => { - env.store.push({ - data: { - type: 'post', - id: '1', - attributes: { - title: 'Hello World', - }, - relationships: { - comments: { - links: { - related: 'comments', - }, + store.push({ + data: { + type: 'post', + id: '1', + attributes: { + title: 'Hello World', + }, + relationships: { + comments: { + links: { + related: 'comments', }, }, }, - }); + }, + }); - let post = env.store.peekRecord('post', 1); + let post = store.peekRecord('post', 1); - return post.get('comments').then(comments => { - let snapshot = post._createSnapshot(); - let relationship = snapshot.hasMany('comments'); + await post.get('comments').then(comments => { + let snapshot = post._createSnapshot(); + let relationship = snapshot.hasMany('comments'); - assert.ok(relationship instanceof Array, 'relationship is an instance of Array'); - assert.equal(relationship.length, 1, 'relationship has one item'); - }); + assert.ok(relationship instanceof Array, 'relationship is an instance of Array'); + assert.equal(relationship.length, 1, 'relationship has one item'); }); }); test("snapshot.hasMany() throws error if relation doesn't exist", function(assert) { assert.expect(1); - run(() => { - env.store.push({ - data: { - type: 'post', - id: '1', - attributes: { - title: 'Hello World', - }, - }, - }); - let post = env.store.peekRecord('post', 1); - let snapshot = post._createSnapshot(); - - assert.throws( - () => { - snapshot.hasMany('unknown'); + store.push({ + data: { + type: 'post', + id: '1', + attributes: { + title: 'Hello World', }, - /has no hasMany relationship named 'unknown'/, - 'throws error' - ); + }, }); + let post = store.peekRecord('post', 1); + let snapshot = post._createSnapshot(); + + assert.throws( + () => { + snapshot.hasMany('unknown'); + }, + /has no hasMany relationship named 'unknown'/, + 'throws error' + ); }); test('snapshot.hasMany() respects the order of items in the relationship', function(assert) { assert.expect(3); - run(() => { - env.store.push({ - data: [ - { - type: 'comment', - id: '1', - attributes: { - body: 'This is the first comment', - }, + store.push({ + data: [ + { + type: 'comment', + id: '1', + attributes: { + body: 'This is the first comment', }, - { - type: 'comment', - id: '2', - attributes: { - body: 'This is the second comment', - }, + }, + { + type: 'comment', + id: '2', + attributes: { + body: 'This is the second comment', }, - { - type: 'comment', - id: '3', - attributes: { - body: 'This is the third comment', - }, + }, + { + type: 'comment', + id: '3', + attributes: { + body: 'This is the third comment', }, - { - type: 'post', - id: '4', - attributes: { - title: 'Hello World', - }, - relationships: { - comments: { - data: [{ type: 'comment', id: '1' }, { type: 'comment', id: '2' }, { type: 'comment', id: '3' }], - }, + }, + { + type: 'post', + id: '4', + attributes: { + title: 'Hello World', + }, + relationships: { + comments: { + data: [{ type: 'comment', id: '1' }, { type: 'comment', id: '2' }, { type: 'comment', id: '3' }], }, }, - ], - }); - let comment3 = env.store.peekRecord('comment', 3); - let post = env.store.peekRecord('post', 4); + }, + ], + }); + let comment3 = store.peekRecord('comment', 3); + let post = store.peekRecord('post', 4); - post.get('comments').removeObject(comment3); - post.get('comments').insertAt(0, comment3); + post.get('comments').removeObject(comment3); + post.get('comments').insertAt(0, comment3); - let snapshot = post._createSnapshot(); - let relationship = snapshot.hasMany('comments'); + let snapshot = post._createSnapshot(); + let relationship = snapshot.hasMany('comments'); - assert.equal(relationship[0].id, '3', 'order of comment 3 is correct'); - assert.equal(relationship[1].id, '1', 'order of comment 1 is correct'); - assert.equal(relationship[2].id, '2', 'order of comment 2 is correct'); - }); + assert.equal(relationship[0].id, '3', 'order of comment 3 is correct'); + assert.equal(relationship[1].id, '1', 'order of comment 1 is correct'); + assert.equal(relationship[2].id, '2', 'order of comment 2 is correct'); }); test('snapshot.eachAttribute() proxies to record', function(assert) { assert.expect(1); - run(() => { - env.store.push({ - data: { - type: 'post', - id: '1', - attributes: { - title: 'Hello World', - }, + store.push({ + data: { + type: 'post', + id: '1', + attributes: { + title: 'Hello World', }, - }); - let post = env.store.peekRecord('post', 1); - let snapshot = post._createSnapshot(); - - let attributes = []; - snapshot.eachAttribute(name => attributes.push(name)); - assert.deepEqual(attributes, ['author', 'title'], 'attributes are iterated correctly'); + }, }); + let post = store.peekRecord('post', 1); + let snapshot = post._createSnapshot(); + + let attributes = []; + snapshot.eachAttribute(name => attributes.push(name)); + assert.deepEqual(attributes, ['author', 'title'], 'attributes are iterated correctly'); }); test('snapshot.eachRelationship() proxies to record', function(assert) { @@ -1084,125 +1058,117 @@ module('integration/snapshot - Snapshot', function(hooks) { return relationships; }; - run(() => { - env.store.push({ - data: [ - { - type: 'comment', - id: '1', - attributes: { - body: 'This is the first comment', - }, + store.push({ + data: [ + { + type: 'comment', + id: '1', + attributes: { + body: 'This is the first comment', }, - { - type: 'post', - id: '2', - attributes: { - title: 'Hello World', - }, + }, + { + type: 'post', + id: '2', + attributes: { + title: 'Hello World', }, - ], - }); - let comment = env.store.peekRecord('comment', 1); - let post = env.store.peekRecord('post', 2); - let snapshot; + }, + ], + }); + let comment = store.peekRecord('comment', 1); + let post = store.peekRecord('post', 2); + let snapshot; - snapshot = comment._createSnapshot(); - assert.deepEqual(getRelationships(snapshot), ['post'], 'relationships are iterated correctly'); + snapshot = comment._createSnapshot(); + assert.deepEqual(getRelationships(snapshot), ['post'], 'relationships are iterated correctly'); - snapshot = post._createSnapshot(); - assert.deepEqual(getRelationships(snapshot), ['comments'], 'relationships are iterated correctly'); - }); + snapshot = post._createSnapshot(); + assert.deepEqual(getRelationships(snapshot), ['comments'], 'relationships are iterated correctly'); }); test('snapshot.belongsTo() does not trigger a call to store._scheduleFetch', function(assert) { assert.expect(0); - env.store._scheduleFetch = function() { + store._scheduleFetch = function() { assert.ok(false, 'store._scheduleFetch should not be called'); }; - run(() => { - env.store.push({ - data: { - type: 'comment', - id: '1', - attributes: { - body: 'This is the first comment', - }, - relationships: { - post: { - data: { type: 'post', id: '2' }, - }, + store.push({ + data: { + type: 'comment', + id: '1', + attributes: { + body: 'This is the first comment', + }, + relationships: { + post: { + data: { type: 'post', id: '2' }, }, }, - }); - let comment = env.store.peekRecord('comment', 1); - let snapshot = comment._createSnapshot(); - - snapshot.belongsTo('post'); + }, }); + let comment = store.peekRecord('comment', 1); + let snapshot = comment._createSnapshot(); + + snapshot.belongsTo('post'); }); test('snapshot.hasMany() does not trigger a call to store._scheduleFetch', function(assert) { assert.expect(0); - env.store._scheduleFetch = function() { + store._scheduleFetch = function() { assert.ok(false, 'store._scheduleFetch should not be called'); }; - run(() => { - env.store.push({ - data: { - type: 'post', - id: '1', - attributes: { - title: 'Hello World', - }, - relationships: { - comments: { - data: [{ type: 'comment', id: '2' }, { type: 'comment', id: '3' }], - }, + store.push({ + data: { + type: 'post', + id: '1', + attributes: { + title: 'Hello World', + }, + relationships: { + comments: { + data: [{ type: 'comment', id: '2' }, { type: 'comment', id: '3' }], }, }, - }); - let post = env.store.peekRecord('post', 1); - let snapshot = post._createSnapshot(); - - snapshot.hasMany('comments'); + }, }); + let post = store.peekRecord('post', 1); + let snapshot = post._createSnapshot(); + + snapshot.hasMany('comments'); }); test('snapshot.serialize() serializes itself', function(assert) { assert.expect(2); - run(() => { - env.store.push({ - data: { - type: 'post', - id: '1', - attributes: { - title: 'Hello World', - }, + store.push({ + data: { + type: 'post', + id: '1', + attributes: { + title: 'Hello World', }, - }); - let post = env.store.peekRecord('post', 1); - let snapshot = post._createSnapshot(); + }, + }); + let post = store.peekRecord('post', 1); + let snapshot = post._createSnapshot(); - post.set('title', 'New Title'); + post.set('title', 'New Title'); - let expected = { - data: { - attributes: { - author: undefined, - title: 'Hello World', - }, - type: 'posts', + let expected = { + data: { + attributes: { + author: undefined, + title: 'Hello World', }, - }; - assert.deepEqual(snapshot.serialize(), expected, 'shapshot serializes correctly'); - expected.data.id = '1'; - assert.deepEqual(snapshot.serialize({ includeId: true }), expected, 'serialize takes options'); - }); + type: 'posts', + }, + }; + assert.deepEqual(snapshot.serialize(), expected, 'shapshot serializes correctly'); + expected.data.id = '1'; + assert.deepEqual(snapshot.serialize({ includeId: true }), expected, 'serialize takes options'); }); });