From 33f2cfc19c47de57561feb5e3c8d5b8361159a4c Mon Sep 17 00:00:00 2001 From: Eric Kelly Date: Thu, 22 Aug 2019 13:09:32 -0700 Subject: [PATCH] [CHORE tests] Replace setupStore for adapter integration tests Part of https://github.com/emberjs/data/issues/6166 This replaces usage of the `setupStore` helper for `tests/integration/adapter/*`. --- .../adapter/client-side-delete-test.js | 50 +-- .../tests/integration/adapter/find-test.js | 91 +++-- .../adapter/json-api-adapter-test.js | 38 +- .../tests/integration/adapter/queries-test.js | 60 ++-- .../adapter/record-persistence-test.js | 109 +++--- .../integration/adapter/rest-adapter-test.js | 59 +-- .../integration/adapter/serialize-test.js | 37 +- .../integration/adapter/store-adapter-test.js | 337 ++++++++++++------ 8 files changed, 475 insertions(+), 306 deletions(-) diff --git a/packages/-ember-data/tests/integration/adapter/client-side-delete-test.js b/packages/-ember-data/tests/integration/adapter/client-side-delete-test.js index 6df4a20e11e..618e8a9e0c0 100644 --- a/packages/-ember-data/tests/integration/adapter/client-side-delete-test.js +++ b/packages/-ember-data/tests/integration/adapter/client-side-delete-test.js @@ -1,35 +1,37 @@ import { resolve } from 'rsvp'; 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'; import { settled } from '@ember/test-helpers'; +import Adapter from '@ember-data/adapter'; +import JSONAPISerializer from '@ember-data/serializer/json-api'; +import Model, { belongsTo, hasMany } from '@ember-data/model'; + module('integration/adapter/store-adapter - client-side delete', function(hooks) { + setupTest(hooks); + hooks.beforeEach(function() { - this.Bookstore = DS.Model.extend({ - books: DS.hasMany('book', { async: false, inverse: 'bookstore' }), - }); - this.Book = DS.Model.extend({ - bookstore: DS.belongsTo('bookstore', { inverse: 'books' }), + this.owner.register('adapter:application', Adapter.extend()); + this.owner.register('serializer:application', JSONAPISerializer.extend()); + }); + + test('client-side deleted records can be added back from an inverse', async function(assert) { + const Bookstore = Model.extend({ + books: hasMany('book', { async: false, inverse: 'bookstore' }), }); - this.env = setupStore({ - bookstore: this.Bookstore, - book: this.Book, + const Book = Model.extend({ + bookstore: belongsTo('bookstore', { inverse: 'books' }), }); - this.store = this.env.store; - this.adapter = this.env.adapter; - }); - hooks.afterEach(function() { - run(this.env.container, 'destroy'); - }); + this.owner.register('model:bookstore', Bookstore); + this.owner.register('model:book', Book); - test('client-side deleted records can be added back from an inverse', async function(assert) { - this.adapter.deleteRecord = function(store, modelClass, snapshot) { + let store = this.owner.lookup('service:store'); + let adapter = store.adapterFor('application'); + + adapter.deleteRecord = function(store, modelClass, snapshot) { if (snapshot.adapterOptions.clientSideDelete) { return resolve(); } @@ -37,7 +39,7 @@ module('integration/adapter/store-adapter - client-side delete', function(hooks) assert.ok(false, 'unreachable'); }; - let bookstore = this.store.push({ + let bookstore = store.push({ data: { id: '1', type: 'bookstore', @@ -70,7 +72,7 @@ module('integration/adapter/store-adapter - client-side delete', function(hooks) assert.deepEqual(bookstore.get('books').mapBy('id'), ['1', '2'], 'initial hasmany loaded'); - let book2 = this.store.peekRecord('book', '2'); + let book2 = store.peekRecord('book', '2'); await book2.destroyRecord({ adapterOptions: { clientSideDelete: true } }); @@ -78,10 +80,10 @@ module('integration/adapter/store-adapter - client-side delete', function(hooks) await settled(); - assert.equal(this.store.hasRecordForId('book', '2'), false, 'book 2 unloaded'); + assert.equal(store.hasRecordForId('book', '2'), false, 'book 2 unloaded'); assert.deepEqual(bookstore.get('books').mapBy('id'), ['1'], 'one book client-side deleted'); - this.store.push({ + store.push({ data: { id: '2', type: 'book', diff --git a/packages/-ember-data/tests/integration/adapter/find-test.js b/packages/-ember-data/tests/integration/adapter/find-test.js index 0b991410efb..a0a47d7e9a4 100644 --- a/packages/-ember-data/tests/integration/adapter/find-test.js +++ b/packages/-ember-data/tests/integration/adapter/find-test.js @@ -1,35 +1,28 @@ import { Promise, reject, defer, resolve } from 'rsvp'; 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'; +import Adapter from 'ember-data/adapter'; import JSONAPISerializer from 'ember-data/serializers/json-api'; - -const { attr } = DS; - -let Person, store, env; +import Model, { attr } from '@ember-data/model'; module('integration/adapter/find - Finding Records', function(hooks) { + setupTest(hooks); + hooks.beforeEach(function() { - Person = DS.Model.extend({ - updatedAt: attr('string'), + const Person = Model.extend({ name: attr('string'), - firstName: attr('string'), - lastName: attr('string'), }); - env = setupStore({ - person: Person, - }); - store = env.store; - }); - - hooks.afterEach(function() { - run(store, 'destroy'); + this.owner.register('model:person', Person); + this.owner.register('adapter:application', Adapter.extend()); + this.owner.register('serializer:application', JSONAPISerializer.extend()); }); testInDebug('It raises an assertion when `undefined` is passed as id (#1705)', function(assert) { + let store = this.owner.lookup('service:store'); + assert.expectAssertion(() => { store.find('person', undefined); }, `You cannot pass 'undefined' as id to the store's find method`); @@ -42,11 +35,13 @@ module('integration/adapter/find - Finding Records', function(hooks) { test("When a single record is requested, the adapter's find method should be called unless it's loaded.", function(assert) { assert.expect(2); - let count = 0; + let store = this.owner.lookup('service:store'); + let Person = store.modelFor('person'); - env.owner.register( + let count = 0; + this.owner.register( 'adapter:person', - DS.Adapter.extend({ + Adapter.extend({ findRecord(_, type) { assert.equal(type, Person, 'the find method is called with the correct type'); assert.equal(count, 0, 'the find method is only called once'); @@ -65,6 +60,8 @@ module('integration/adapter/find - Finding Records', function(hooks) { }) ); + this.owner.register('serializer:application', JSONAPISerializer.extend()); + run(() => { store.findRecord('person', 1); store.findRecord('person', 1); @@ -74,15 +71,19 @@ module('integration/adapter/find - Finding Records', function(hooks) { test('When a single record is requested multiple times, all .findRecord() calls are resolved after the promise is resolved', function(assert) { let deferred = defer(); - env.owner.register( + this.owner.register( 'adapter:person', - DS.Adapter.extend({ + Adapter.extend({ findRecord() { return deferred.promise; }, }) ); + this.owner.register('serializer:application', JSONAPISerializer.extend()); + + let store = this.owner.lookup('service:store'); + let requestOne = run(() => { return store.findRecord('person', 1).then(person => { assert.equal(person.get('id'), '1'); @@ -113,15 +114,17 @@ module('integration/adapter/find - Finding Records', function(hooks) { }); test('When a single record is requested, and the promise is rejected, .findRecord() is rejected.', function(assert) { - env.owner.register( + this.owner.register( 'adapter:person', - DS.Adapter.extend({ + Adapter.extend({ findRecord() { return reject(); }, }) ); + let store = this.owner.lookup('service:store'); + return run(() => { return store.findRecord('person', 1).catch(() => { assert.ok(true, 'The rejection handler was called'); @@ -132,15 +135,17 @@ module('integration/adapter/find - Finding Records', function(hooks) { test('When a single record is requested, and the promise is rejected, the record should be unloaded.', function(assert) { assert.expect(2); - env.owner.register( + this.owner.register( 'adapter:person', - DS.Adapter.extend({ + Adapter.extend({ findRecord() { return reject(); }, }) ); + let store = this.owner.lookup('service:store'); + return run(() => { return store.findRecord('person', 1).catch(reason => { assert.ok(true, 'The rejection handler was called'); @@ -150,27 +155,31 @@ module('integration/adapter/find - Finding Records', function(hooks) { }); testInDebug('When a single record is requested, and the payload is blank', function(assert) { - env.owner.register( + this.owner.register( 'adapter:person', - DS.Adapter.extend({ + Adapter.extend({ findRecord: () => resolve({}), }) ); + let store = this.owner.lookup('service:store'); + assert.expectAssertion(() => { run(() => store.findRecord('person', 'the-id')); }, /You made a 'findRecord' request for a 'person' with id 'the-id', but the adapter's response did not have any data/); }); testInDebug('When multiple records are requested, and the payload is blank', function(assert) { - env.owner.register( + this.owner.register( 'adapter:person', - DS.Adapter.extend({ + Adapter.extend({ coalesceFindRequests: true, findMany: () => resolve({}), }) ); + let store = this.owner.lookup('service:store'); + assert.expectAssertion(() => { run(() => { store.findRecord('person', '1'); @@ -180,9 +189,9 @@ module('integration/adapter/find - Finding Records', function(hooks) { }); testInDebug('warns when returned record has different id', function(assert) { - env.owner.register( + this.owner.register( 'adapter:person', - DS.Adapter.extend({ + Adapter.extend({ findRecord() { return { data: { @@ -197,14 +206,16 @@ module('integration/adapter/find - Finding Records', function(hooks) { }) ); + let store = this.owner.lookup('service:store'); + assert.expectWarning( - () => run(() => env.store.findRecord('person', 'me')), + () => run(() => store.findRecord('person', 'me')), /You requested a record of type 'person' with id 'me' but the adapter returned a payload with primary data having an id of '1'/ ); }); testInDebug('coerces ids before warning when returned record has different id', async function(assert) { - env.owner.register( + this.owner.register( 'serializer:application', JSONAPISerializer.extend({ normalizeResponse(_, __, payload) { @@ -213,9 +224,9 @@ module('integration/adapter/find - Finding Records', function(hooks) { }) ); - env.owner.register( + this.owner.register( 'adapter:person', - DS.Adapter.extend({ + Adapter.extend({ findRecord() { return { data: { @@ -230,12 +241,14 @@ module('integration/adapter/find - Finding Records', function(hooks) { }) ); + let store = this.owner.lookup('service:store'); + assert.expectNoWarning( - () => run(() => env.store.findRecord('person', 1)), + () => run(() => store.findRecord('person', 1)), /You requested a record of type 'person' with id '1' but the adapter returned a payload with primary data having an id of '1'/ ); assert.expectNoWarning( - () => run(() => env.store.findRecord('person', '1')), + () => run(() => store.findRecord('person', '1')), /You requested a record of type 'person' with id '1' but the adapter returned a payload with primary data having an id of '1'/ ); }); diff --git a/packages/-ember-data/tests/integration/adapter/json-api-adapter-test.js b/packages/-ember-data/tests/integration/adapter/json-api-adapter-test.js index 3b8de568113..85219a7ef74 100644 --- a/packages/-ember-data/tests/integration/adapter/json-api-adapter-test.js +++ b/packages/-ember-data/tests/integration/adapter/json-api-adapter-test.js @@ -1,18 +1,20 @@ import RSVP from 'rsvp'; import { run } from '@ember/runloop'; -import setupStore from 'dummy/tests/helpers/store'; +import { setupTest } from 'ember-qunit'; import { module, test } from 'qunit'; import testInDebug from 'dummy/tests/helpers/test-in-debug'; import DS from 'ember-data'; -let env, store, adapter; +let store, adapter; let passedUrl, passedVerb, passedHash; let User, Post, Comment, Handle, GithubHandle, TwitterHandle, Company, DevelopmentShop, DesignStudio; module('integration/adapter/json-api-adapter - JSONAPIAdapter', function(hooks) { + setupTest(hooks); + hooks.beforeEach(function() { User = DS.Model.extend({ firstName: DS.attr('string'), @@ -58,26 +60,20 @@ module('integration/adapter/json-api-adapter - JSONAPIAdapter', function(hooks) hipsters: DS.attr('number'), }); - env = setupStore({ - adapter: DS.JSONAPIAdapter.extend(), - - user: User, - post: Post, - comment: Comment, - handle: Handle, - 'github-handle': GithubHandle, - 'twitter-handle': TwitterHandle, - company: Company, - 'development-shop': DevelopmentShop, - 'design-studio': DesignStudio, - }); + this.owner.register('adapter:application', DS.JSONAPIAdapter.extend()); - store = env.store; - adapter = env.adapter; - }); + this.owner.register('model:user', User); + this.owner.register('model:post', Post); + this.owner.register('model:comment', Comment); + this.owner.register('model:handle', Handle); + this.owner.register('model:github-handle', GithubHandle); + this.owner.register('model:twitter-handle', TwitterHandle); + this.owner.register('model:company', Company); + this.owner.register('model:development-shop', DevelopmentShop); + this.owner.register('model:design-studio', DesignStudio); - hooks.afterEach(function() { - run(env.store, 'destroy'); + store = this.owner.lookup('service:store'); + adapter = store.adapterFor('application'); }); function ajaxResponse(responses) { @@ -925,7 +921,7 @@ module('integration/adapter/json-api-adapter - JSONAPIAdapter', function(hooks) }, ]); - env.owner.register( + this.owner.register( 'serializer:user', DS.JSONAPISerializer.extend({ attrs: { diff --git a/packages/-ember-data/tests/integration/adapter/queries-test.js b/packages/-ember-data/tests/integration/adapter/queries-test.js index 04f28c9fb6a..c58a5df27be 100644 --- a/packages/-ember-data/tests/integration/adapter/queries-test.js +++ b/packages/-ember-data/tests/integration/adapter/queries-test.js @@ -1,46 +1,47 @@ import { Promise as EmberPromise, resolve } from 'rsvp'; import { get } from '@ember/object'; 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 Person, env, store, adapter; +import Model, { attr } from '@ember-data/model'; module('integration/adapter/queries - Queries', function(hooks) { - hooks.beforeEach(function() { - Person = DS.Model.extend({ - updatedAt: DS.attr('string'), - name: DS.attr('string'), - firstName: DS.attr('string'), - lastName: DS.attr('string'), - }); + setupTest(hooks); - env = setupStore({ person: Person }); - store = env.store; - adapter = env.adapter; - }); + testInDebug('It raises an assertion when no type is passed', function(assert) { + const Person = Model.extend(); - hooks.afterEach(function() { - run(env.container, 'destroy'); - }); + this.owner.register('model:person', Person); + + let store = this.owner.lookup('service:store'); - testInDebug('It raises an assertion when no type is passed', function(assert) { assert.expectAssertion(() => { store.query(); }, "You need to pass a model name to the store's query method"); }); testInDebug('It raises an assertion when no query hash is passed', function(assert) { + const Person = Model.extend(); + + this.owner.register('model:person', Person); + + let store = this.owner.lookup('service:store'); + assert.expectAssertion(() => { store.query('person'); }, "You need to pass a query hash to the store's query method"); }); test('When a query is made, the adapter should receive a record array it can populate with the results of the query.', function(assert) { + const Person = Model.extend({ name: attr() }); + + this.owner.register('model:person', Person); + + let store = this.owner.lookup('service:store'); + let adapter = store.adapterFor('application'); + adapter.query = function(store, type, query, recordArray) { assert.equal(type, Person, 'the query method is called with the correct type'); @@ -74,6 +75,13 @@ module('integration/adapter/queries - Queries', function(hooks) { }); test('a query can be updated via `update()`', function(assert) { + const Person = Model.extend(); + + this.owner.register('model:person', Person); + + let store = this.owner.lookup('service:store'); + let adapter = store.adapterFor('application'); + adapter.query = function() { return resolve({ data: [{ id: 'first', type: 'person' }] }); }; @@ -107,12 +115,12 @@ module('integration/adapter/queries - Queries', function(hooks) { }); testInDebug('The store asserts when query is made and the adapter responses with a single record.', function(assert) { - env = setupStore({ - person: Person, - adapter: DS.RESTAdapter, - }); - store = env.store; - adapter = env.adapter; + const Person = Model.extend({ name: attr() }); + + this.owner.register('model:person', Person); + + let store = this.owner.lookup('service:store'); + let adapter = store.adapterFor('application'); adapter.query = function(store, type, query, recordArray) { assert.equal(type, Person, 'the query method is called with the correct type'); diff --git a/packages/-ember-data/tests/integration/adapter/record-persistence-test.js b/packages/-ember-data/tests/integration/adapter/record-persistence-test.js index 29cb4ea2256..1db25975ad0 100644 --- a/packages/-ember-data/tests/integration/adapter/record-persistence-test.js +++ b/packages/-ember-data/tests/integration/adapter/record-persistence-test.js @@ -1,42 +1,41 @@ import { set, get } from '@ember/object'; import { run } from '@ember/runloop'; -import RSVP, { resolve } from 'rsvp'; -import setupStore from 'dummy/tests/helpers/store'; - +import RSVP, { all, hash, resolve } from 'rsvp'; +import { setupTest } from 'ember-qunit'; import { module, test } from 'qunit'; -import DS from 'ember-data'; - -const { all, hash } = RSVP; -const { attr } = DS; -let Person, env, store; +import Model, { attr } from '@ember-data/model'; +import Adapter from '@ember-data/adapter'; +import JSONAPISerializer from '@ember-data/serializer/json-api'; module('integration/adapter/record_persistence - Persisting Records', function(hooks) { + setupTest(hooks); + hooks.beforeEach(function() { - Person = DS.Model.extend({ + const Person = Model.extend({ updatedAt: attr('string'), name: attr('string'), firstName: attr('string'), lastName: attr('string'), }); - env = setupStore({ - adapter: DS.Adapter.extend({ - shouldBackgroundReloadRecord: () => false, - }), - person: Person, + const ApplicationAdapter = Adapter.extend({ + shouldBackgroundReloadRecord: () => false, }); - store = env.store; - }); - hooks.afterEach(function() { - run(env.container, 'destroy'); + this.owner.register('model:person', Person); + this.owner.register('adapter:application', ApplicationAdapter); + this.owner.register('serializer:application', JSONAPISerializer.extend()); }); test("When a store is committed, the adapter's `commit` method should be called with records that have been changed.", function(assert) { assert.expect(2); - env.adapter.updateRecord = function(store, type, snapshot) { + let store = this.owner.lookup('service:store'); + let adapter = store.adapterFor('application'); + let Person = store.modelFor('person'); + + adapter.updateRecord = function(store, type, snapshot) { assert.equal(type, Person, 'the type is correct'); assert.equal(snapshot.record, tom, 'the record is correct'); @@ -44,7 +43,7 @@ module('integration/adapter/record_persistence - Persisting Records', function(h }; run(() => { - env.store.push({ + store.push({ data: { type: 'person', id: '1', @@ -58,7 +57,7 @@ module('integration/adapter/record_persistence - Persisting Records', function(h let tom; return run(() => { - return env.store.findRecord('person', 1).then(person => { + return store.findRecord('person', 1).then(person => { tom = person; set(tom, 'name', 'Tom Dale'); return tom.save(); @@ -68,9 +67,14 @@ module('integration/adapter/record_persistence - Persisting Records', function(h test("When a store is committed, the adapter's `commit` method should be called with records that have been created.", function(assert) { assert.expect(2); + + let store = this.owner.lookup('service:store'); + let adapter = store.adapterFor('application'); + let Person = store.modelFor('person'); + let tom; - env.adapter.createRecord = function(store, type, snapshot) { + adapter.createRecord = function(store, type, snapshot) { assert.equal(type, Person, 'the type is correct'); assert.equal(snapshot.record, tom, 'the record is correct'); @@ -78,31 +82,39 @@ module('integration/adapter/record_persistence - Persisting Records', function(h }; return run(() => { - tom = env.store.createRecord('person', { name: 'Tom Dale' }); + tom = store.createRecord('person', { name: 'Tom Dale' }); return tom.save(); }); }); test('After a created record has been assigned an ID, finding a record by that ID returns the original record.', function(assert) { assert.expect(1); + + let store = this.owner.lookup('service:store'); + let adapter = store.adapterFor('application'); + let tom; - env.adapter.createRecord = function(store, type, snapshot) { + adapter.createRecord = function(store, type, snapshot) { return resolve({ data: { id: 1, type: 'person', attributes: { name: 'Tom Dale' } } }); }; return run(() => { - tom = env.store.createRecord('person', { name: 'Tom Dale' }); + tom = store.createRecord('person', { name: 'Tom Dale' }); return tom.save(); }).then(tom => { - return env.store.find('person', 1).then(nextTom => { + return store.findRecord('person', 1).then(nextTom => { assert.equal(tom, nextTom, 'the retrieved record is the same as the created record'); }); }); }); test("when a store is committed, the adapter's `commit` method should be called with records that have been deleted.", function(assert) { - env.adapter.deleteRecord = function(store, type, snapshot) { + let store = this.owner.lookup('service:store'); + let adapter = store.adapterFor('application'); + let Person = store.modelFor('person'); + + adapter.deleteRecord = function(store, type, snapshot) { assert.equal(type, Person, 'the type is correct'); assert.equal(snapshot.record, tom, 'the record is correct'); @@ -112,7 +124,7 @@ module('integration/adapter/record_persistence - Persisting Records', function(h let tom; run(() => { - env.store.push({ + store.push({ data: { type: 'person', id: '1', @@ -123,7 +135,7 @@ module('integration/adapter/record_persistence - Persisting Records', function(h }); }); - return env.store + return store .findRecord('person', 1) .then(person => { tom = person; @@ -138,14 +150,17 @@ module('integration/adapter/record_persistence - Persisting Records', function(h test('An adapter can notify the store that records were updated by calling `didSaveRecords`.', function(assert) { assert.expect(6); + let store = this.owner.lookup('service:store'); + let adapter = store.adapterFor('application'); + let tom, yehuda; - env.adapter.updateRecord = function(store, type, snapshot) { + adapter.updateRecord = function(store, type, snapshot) { return resolve(); }; run(() => { - env.store.push({ + store.push({ data: [ { type: 'person', @@ -159,7 +174,7 @@ module('integration/adapter/record_persistence - Persisting Records', function(h }); }); - return all([env.store.findRecord('person', 1), env.store.findRecord('person', 2)]).then(array => { + return all([store.findRecord('person', 1), store.findRecord('person', 2)]).then(array => { tom = array[0]; yehuda = array[1]; @@ -182,7 +197,10 @@ module('integration/adapter/record_persistence - Persisting Records', function(h }); test('An adapter can notify the store that records were updated and provide new data by calling `didSaveRecords`.', function(assert) { - env.adapter.updateRecord = function(store, type, snapshot) { + let store = this.owner.lookup('service:store'); + let adapter = store.adapterFor('application'); + + adapter.updateRecord = function(store, type, snapshot) { if (snapshot.id === '1') { return resolve({ data: { id: 1, type: 'person', attributes: { name: 'Tom Dale', 'updated-at': 'now' } }, @@ -199,7 +217,7 @@ module('integration/adapter/record_persistence - Persisting Records', function(h }; run(() => { - env.store.push({ + store.push({ data: [ { type: 'person', @@ -220,8 +238,8 @@ module('integration/adapter/record_persistence - Persisting Records', function(h }); return hash({ - tom: env.store.findRecord('person', 1), - yehuda: env.store.findRecord('person', 2), + tom: store.findRecord('person', 1), + yehuda: store.findRecord('person', 2), }) .then(people => { people.tom.set('name', 'Draaaaaahm Dale'); @@ -257,7 +275,10 @@ module('integration/adapter/record_persistence - Persisting Records', function(h }); test('An adapter can notify the store that a record was updated by calling `didSaveRecord`.', function(assert) { - env.adapter.updateRecord = function(store, type, snapshot) { + let store = this.owner.lookup('service:store'); + let adapter = store.adapterFor('application'); + + adapter.updateRecord = function(store, type, snapshot) { return resolve(); }; @@ -292,7 +313,10 @@ module('integration/adapter/record_persistence - Persisting Records', function(h }); test('An adapter can notify the store that a record was updated and provide new data by calling `didSaveRecord`.', function(assert) { - env.adapter.updateRecord = function(store, type, snapshot) { + let store = this.owner.lookup('service:store'); + let adapter = store.adapterFor('application'); + + adapter.updateRecord = function(store, type, snapshot) { switch (snapshot.id) { case '1': return resolve({ @@ -310,7 +334,7 @@ module('integration/adapter/record_persistence - Persisting Records', function(h }; run(() => { - env.store.push({ + store.push({ data: [ { type: 'person', @@ -368,12 +392,15 @@ module('integration/adapter/record_persistence - Persisting Records', function(h }); test('An adapter can notify the store that records were deleted by calling `didSaveRecords`.', function(assert) { - env.adapter.deleteRecord = function(store, type, snapshot) { + let store = this.owner.lookup('service:store'); + let adapter = store.adapterFor('application'); + + adapter.deleteRecord = function(store, type, snapshot) { return resolve(); }; run(() => { - env.store.push({ + store.push({ data: [ { type: 'person', diff --git a/packages/-ember-data/tests/integration/adapter/rest-adapter-test.js b/packages/-ember-data/tests/integration/adapter/rest-adapter-test.js index d1d1cf2a4a3..75683970606 100644 --- a/packages/-ember-data/tests/integration/adapter/rest-adapter-test.js +++ b/packages/-ember-data/tests/integration/adapter/rest-adapter-test.js @@ -4,7 +4,7 @@ import { underscore } from '@ember/string'; import { resolve, reject } from 'rsvp'; import { run } from '@ember/runloop'; import { get } from '@ember/object'; -import setupStore from 'dummy/tests/helpers/store'; +import { setupTest } from 'ember-qunit'; import { singularize } from 'ember-inflector'; import deepCopy from 'dummy/tests/helpers/deep-copy'; import testInDebug from 'dummy/tests/helpers/test-in-debug'; @@ -16,11 +16,13 @@ import DS from 'ember-data'; const hasJQuery = typeof jQuery !== 'undefined'; -let env, store, adapter, Post, Comment, SuperUser; +let store, adapter, Post, Comment, SuperUser; let passedUrl, passedVerb, passedHash; let server; module('integration/adapter/rest_adapter - REST Adapter', function(hooks) { + setupTest(hooks); + hooks.beforeEach(function() { Post = DS.Model.extend({ name: DS.attr('string'), @@ -32,16 +34,15 @@ module('integration/adapter/rest_adapter - REST Adapter', function(hooks) { SuperUser = DS.Model.extend(); - env = setupStore({ - post: Post, - comment: Comment, - superUser: SuperUser, - adapter: DS.RESTAdapter, - }); + this.owner.register('model:post', Post); + this.owner.register('model:comment', Comment); + this.owner.register('model:super-user', SuperUser); + + this.owner.register('adapter:application', DS.RESTAdapter.extend()); server = new Pretender(); - store = env.store; - adapter = env.adapter; + store = this.owner.lookup('service:store'); + adapter = store.adapterFor('application'); passedUrl = passedVerb = passedHash = null; }); @@ -213,7 +214,7 @@ module('integration/adapter/rest_adapter - REST Adapter', function(hooks) { }); test('findRecord - payload with an serializer-specified primary key', function(assert) { - env.owner.register( + this.owner.register( 'serializer:post', DS.RESTSerializer.extend({ primaryKey: '_ID_', @@ -235,7 +236,7 @@ module('integration/adapter/rest_adapter - REST Adapter', function(hooks) { }); test('findRecord - payload with a serializer-specified attribute mapping', function(assert) { - env.owner.register( + this.owner.register( 'serializer:post', DS.RESTSerializer.extend({ attrs: { @@ -382,7 +383,7 @@ module('integration/adapter/rest_adapter - REST Adapter', function(hooks) { }); test("createRecord - a serializer's primary key and attributes are consulted when building the payload", function(assert) { - env.owner.register( + this.owner.register( 'serializer:post', DS.RESTSerializer.extend({ primaryKey: '_id_', @@ -408,7 +409,7 @@ module('integration/adapter/rest_adapter - REST Adapter', function(hooks) { test("createRecord - a serializer's attributes are consulted when building the payload if no id is pre-defined", function(assert) { let post; - env.owner.register( + this.owner.register( 'serializer:post', DS.RESTSerializer.extend({ attrs: { @@ -431,7 +432,7 @@ module('integration/adapter/rest_adapter - REST Adapter', function(hooks) { }); test("createRecord - a serializer's attribute mapping takes precedence over keyForAttribute when building the payload", function(assert) { - env.owner.register( + this.owner.register( 'serializer:post', DS.RESTSerializer.extend({ attrs: { @@ -458,7 +459,7 @@ module('integration/adapter/rest_adapter - REST Adapter', function(hooks) { }); test("createRecord - a serializer's attribute mapping takes precedence over keyForRelationship (belongsTo) when building the payload", function(assert) { - env.owner.register( + this.owner.register( 'serializer:comment', DS.RESTSerializer.extend({ attrs: { @@ -492,7 +493,7 @@ module('integration/adapter/rest_adapter - REST Adapter', function(hooks) { }); test("createRecord - a serializer's attribute mapping takes precedence over keyForRelationship (hasMany) when building the payload", function(assert) { - env.owner.register( + this.owner.register( 'serializer:post', DS.RESTSerializer.extend({ attrs: { @@ -892,7 +893,7 @@ module('integration/adapter/rest_adapter - REST Adapter', function(hooks) { test("updateRecord - a serializer's primary key and attributes are consulted when building the payload", function(assert) { adapter.shouldBackgroundReloadRecord = () => false; - env.owner.register( + this.owner.register( 'serializer:post', DS.RESTSerializer.extend({ primaryKey: '_id_', @@ -1202,7 +1203,7 @@ module('integration/adapter/rest_adapter - REST Adapter', function(hooks) { }); test('findAll - data is normalized through custom serializers', function(assert) { - env.owner.register( + this.owner.register( 'serializer:post', DS.RESTSerializer.extend({ primaryKey: '_ID_', @@ -1365,7 +1366,7 @@ module('integration/adapter/rest_adapter - REST Adapter', function(hooks) { }); test('query - data is normalized through custom serializers', function(assert) { - env.owner.register( + this.owner.register( 'serializer:post', DS.RESTSerializer.extend({ primaryKey: '_ID_', @@ -1482,7 +1483,7 @@ module('integration/adapter/rest_adapter - REST Adapter', function(hooks) { }); test('queryRecord - data is normalized through custom serializers', function(assert) { - env.owner.register( + this.owner.register( 'serializer:post', DS.RESTSerializer.extend({ primaryKey: '_ID_', @@ -1709,7 +1710,7 @@ module('integration/adapter/rest_adapter - REST Adapter', function(hooks) { test('findMany - a custom serializer is used if present', function(assert) { adapter.shouldBackgroundReloadRecord = () => false; - env.owner.register( + this.owner.register( 'serializer:post', DS.RESTSerializer.extend({ primaryKey: '_ID_', @@ -1717,7 +1718,7 @@ module('integration/adapter/rest_adapter - REST Adapter', function(hooks) { }) ); - env.owner.register( + this.owner.register( 'serializer:comment', DS.RESTSerializer.extend({ primaryKey: '_ID_', @@ -1923,7 +1924,7 @@ module('integration/adapter/rest_adapter - REST Adapter', function(hooks) { test('findMany - a custom serializer is used if present', function(assert) { adapter.shouldBackgroundReloadRecord = () => false; - env.owner.register( + this.owner.register( 'serializer:post', DS.RESTSerializer.extend({ primaryKey: '_ID_', @@ -1931,7 +1932,7 @@ module('integration/adapter/rest_adapter - REST Adapter', function(hooks) { }) ); - env.owner.register( + this.owner.register( 'serializer:comment', DS.RESTSerializer.extend({ primaryKey: '_ID_', @@ -2150,7 +2151,7 @@ module('integration/adapter/rest_adapter - REST Adapter', function(hooks) { }); test('normalizeKey - to set up _ids and _id', function(assert) { - env.owner.register( + this.owner.register( 'serializer:application', DS.RESTSerializer.extend({ keyForAttribute(attr) { @@ -2171,7 +2172,7 @@ module('integration/adapter/rest_adapter - REST Adapter', function(hooks) { }) ); - env.owner.register( + this.owner.register( 'model:post', DS.Model.extend({ name: DS.attr(), @@ -2181,7 +2182,7 @@ module('integration/adapter/rest_adapter - REST Adapter', function(hooks) { }) ); - env.owner.register( + this.owner.register( 'model:user', DS.Model.extend({ createdAt: DS.attr(), @@ -2189,7 +2190,7 @@ module('integration/adapter/rest_adapter - REST Adapter', function(hooks) { }) ); - env.owner.register( + this.owner.register( 'model:comment', DS.Model.extend({ body: DS.attr(), diff --git a/packages/-ember-data/tests/integration/adapter/serialize-test.js b/packages/-ember-data/tests/integration/adapter/serialize-test.js index 48585e9c086..36decc2f460 100644 --- a/packages/-ember-data/tests/integration/adapter/serialize-test.js +++ b/packages/-ember-data/tests/integration/adapter/serialize-test.js @@ -1,36 +1,31 @@ -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'; - -let env, store, adapter, serializer; +import Adapter from '@ember-data/adapter'; +import JSONAPISerializer from '@ember-data/serializer/json-api'; +import Model from '@ember-data/model'; module('integration/adapter/serialize - DS.Adapter integration test', function(hooks) { - hooks.beforeEach(function() { - const Person = DS.Model.extend({ - name: DS.attr('string'), - }); - - env = setupStore({ person: Person }); - store = env.store; - adapter = env.adapter; - serializer = store.serializerFor('person'); - }); - - hooks.afterEach(function() { - run(env.container, 'destroy'); - }); + setupTest(hooks); test('serialize() is delegated to the serializer', function(assert) { assert.expect(1); + const Person = Model.extend(); + + this.owner.register('model:person', Person); + this.owner.register('adapter:application', Adapter.extend()); + this.owner.register('serializer:application', JSONAPISerializer.extend()); + + let store = this.owner.lookup('service:store'); + let adapter = store.adapterFor('application'); + let serializer = store.serializerFor('application'); + serializer.serialize = function(snapshot, options) { assert.deepEqual(options, { foo: 'bar' }); }; let person = store.createRecord('person'); + adapter.serialize(person._createSnapshot(), { foo: 'bar' }); }); }); diff --git a/packages/-ember-data/tests/integration/adapter/store-adapter-test.js b/packages/-ember-data/tests/integration/adapter/store-adapter-test.js index b3949b35f07..1e65beeaac4 100644 --- a/packages/-ember-data/tests/integration/adapter/store-adapter-test.js +++ b/packages/-ember-data/tests/integration/adapter/store-adapter-test.js @@ -1,14 +1,19 @@ import { resolve, hash, Promise as EmberPromise, reject } from 'rsvp'; import { set, get } from '@ember/object'; import { run } from '@ember/runloop'; -import setupStore from 'dummy/tests/helpers/store'; +import { setupTest } from 'ember-qunit'; import { module, test } from 'qunit'; import testInDebug from 'dummy/tests/helpers/test-in-debug'; -import DS from 'ember-data'; +import Model, { attr, belongsTo, hasMany } from '@ember-data/model'; +import AdapterError, { InvalidError } from '@ember-data/adapter/error'; +import JSONAPIAdapter from '@ember-data/adapter/json-api'; +import JSONAPISerializer from '@ember-data/serializer/json-api'; +import RESTAdapter from '@ember-data/adapter/rest'; +import RESTSerializer from '@ember-data/serializer/rest'; -let Person, Dog, env, store, adapter; +import { PromiseArray, Snapshot } from '@ember-data/store/-private'; function moveRecordOutOfInFlight(record) { run(() => { @@ -20,33 +25,28 @@ function moveRecordOutOfInFlight(record) { } module('integration/adapter/store-adapter - DS.Store and DS.Adapter integration test', function(hooks) { + setupTest(hooks); + hooks.beforeEach(function() { - Person = DS.Model.extend({ - updatedAt: DS.attr('string'), - name: DS.attr('string'), - firstName: DS.attr('string'), - lastName: DS.attr('string'), + const Person = Model.extend({ + updatedAt: attr('string'), + name: attr('string'), }); - Dog = DS.Model.extend({ - name: DS.attr('string'), + const Dog = Model.extend({ + name: attr('string'), }); - env = setupStore({ - serializer: DS.JSONAPISerializer, - adapter: DS.JSONAPIAdapter, - person: Person, - dog: Dog, - }); - store = env.store; - adapter = env.adapter; - }); - - hooks.afterEach(function() { - run(env.container, 'destroy'); + this.owner.register('adapter:application', JSONAPIAdapter.extend()); + this.owner.register('serializer:application', JSONAPISerializer.extend()); + this.owner.register('model:person', Person); + this.owner.register('model:dog', Dog); }); test('Records loaded multiple times and retrieved in recordArray are ready to send state events', function(assert) { + let store = this.owner.lookup('service:store'); + let adapter = store.adapterFor('application'); + adapter.query = function(store, type, query, recordArray) { return resolve({ data: [ @@ -90,6 +90,10 @@ module('integration/adapter/store-adapter - DS.Store and DS.Adapter integration }); test('by default, createRecords calls createRecord once per record', function(assert) { + let store = this.owner.lookup('service:store'); + let adapter = store.adapterFor('application'); + let Person = store.modelFor('person'); + let count = 1; adapter.shouldBackgroundReloadRecord = () => false; adapter.createRecord = function(store, type, snapshot) { @@ -139,6 +143,10 @@ module('integration/adapter/store-adapter - DS.Store and DS.Adapter integration }); test('by default, updateRecords calls updateRecord once per record', function(assert) { + let store = this.owner.lookup('service:store'); + let adapter = store.adapterFor('application'); + let Person = store.modelFor('person'); + let count = 0; adapter.shouldBackgroundReloadRecord = () => false; adapter.updateRecord = function(store, type, snapshot) { @@ -160,7 +168,7 @@ module('integration/adapter/store-adapter - DS.Store and DS.Adapter integration }; run(() => { - env.store.push({ + store.push({ data: [ { type: 'person', @@ -213,6 +221,10 @@ module('integration/adapter/store-adapter - DS.Store and DS.Adapter integration }); test('calling store.didSaveRecord can provide an optional hash', function(assert) { + let store = this.owner.lookup('service:store'); + let adapter = store.adapterFor('application'); + let Person = store.modelFor('person'); + let count = 0; adapter.shouldBackgroundReloadRecord = () => false; adapter.updateRecord = function(store, type, snapshot) { @@ -239,7 +251,7 @@ module('integration/adapter/store-adapter - DS.Store and DS.Adapter integration }; run(() => { - env.store.push({ + store.push({ data: [ { type: 'person', @@ -294,6 +306,10 @@ module('integration/adapter/store-adapter - DS.Store and DS.Adapter integration test('by default, deleteRecord calls deleteRecord once per record', function(assert) { assert.expect(4); + let store = this.owner.lookup('service:store'); + let adapter = store.adapterFor('application'); + let Person = store.modelFor('person'); + let count = 0; adapter.shouldBackgroundReloadRecord = () => false; adapter.deleteRecord = function(store, type, snapshot) { @@ -313,7 +329,7 @@ module('integration/adapter/store-adapter - DS.Store and DS.Adapter integration }; run(() => { - env.store.push({ + store.push({ data: [ { type: 'person', @@ -354,6 +370,10 @@ module('integration/adapter/store-adapter - DS.Store and DS.Adapter integration test('by default, destroyRecord calls deleteRecord once per record without requiring .save', function(assert) { assert.expect(4); + let store = this.owner.lookup('service:store'); + let adapter = store.adapterFor('application'); + let Person = store.modelFor('person'); + let count = 0; adapter.shouldBackgroundReloadRecord = () => false; @@ -374,7 +394,7 @@ module('integration/adapter/store-adapter - DS.Store and DS.Adapter integration }; run(() => { - env.store.push({ + store.push({ data: [ { type: 'person', @@ -412,6 +432,9 @@ module('integration/adapter/store-adapter - DS.Store and DS.Adapter integration test('if an existing model is edited then deleted, deleteRecord is called on the adapter', function(assert) { assert.expect(5); + let store = this.owner.lookup('service:store'); + let adapter = store.adapterFor('application'); + let count = 0; adapter.shouldBackgroundReloadRecord = () => false; adapter.deleteRecord = function(store, type, snapshot) { @@ -428,7 +451,7 @@ module('integration/adapter/store-adapter - DS.Store and DS.Adapter integration // Load data for a record into the store. run(() => { - env.store.push({ + store.push({ data: { type: 'person', id: 'deleted-record', @@ -459,8 +482,11 @@ module('integration/adapter/store-adapter - DS.Store and DS.Adapter integration }); test('if a deleted record errors, it enters the error state', function(assert) { + let store = this.owner.lookup('service:store'); + let adapter = store.adapterFor('application'); + let count = 0; - let error = new DS.AdapterError(); + let error = new AdapterError(); adapter.shouldBackgroundReloadRecord = () => false; adapter.deleteRecord = function(store, type, snapshot) { @@ -472,7 +498,7 @@ module('integration/adapter/store-adapter - DS.Store and DS.Adapter integration }; run(() => { - env.store.push({ + store.push({ data: { type: 'person', id: 'deleted-record', @@ -507,12 +533,16 @@ module('integration/adapter/store-adapter - DS.Store and DS.Adapter integration }); test('if a created record is marked as invalid by the server, it enters an error state', function(assert) { + let store = this.owner.lookup('service:store'); + let adapter = store.adapterFor('application'); + let Person = store.modelFor('person'); + adapter.createRecord = function(store, type, snapshot) { assert.equal(type, Person, 'the type is correct'); if (snapshot.attr('name').indexOf('Bro') === -1) { return reject( - new DS.InvalidError([ + new InvalidError([ { title: 'Invalid Attribute', detail: 'common... name requires a "bro"', @@ -559,10 +589,13 @@ module('integration/adapter/store-adapter - DS.Store and DS.Adapter integration }); test('allows errors on arbitrary properties on create', function(assert) { + let store = this.owner.lookup('service:store'); + let adapter = store.adapterFor('application'); + adapter.createRecord = function(store, type, snapshot) { if (snapshot.attr('name').indexOf('Bro') === -1) { return reject( - new DS.InvalidError([ + new InvalidError([ { title: 'Invalid Attribute', detail: 'is a generally unsavoury character', @@ -614,6 +647,10 @@ module('integration/adapter/store-adapter - DS.Store and DS.Adapter integration }); test('if a created record is marked as invalid by the server, you can attempt the save again', function(assert) { + let store = this.owner.lookup('service:store'); + let adapter = store.adapterFor('application'); + let Person = store.modelFor('person'); + let saveCount = 0; adapter.createRecord = function(store, type, snapshot) { assert.equal(type, Person, 'the type is correct'); @@ -621,7 +658,7 @@ module('integration/adapter/store-adapter - DS.Store and DS.Adapter integration if (snapshot.attr('name').indexOf('Bro') === -1) { return reject( - new DS.InvalidError([ + new InvalidError([ { title: 'Invalid Attribute', detail: 'common... name requires a "bro"', @@ -678,7 +715,10 @@ module('integration/adapter/store-adapter - DS.Store and DS.Adapter integration }); test('if a created record is marked as erred by the server, it enters an error state', function(assert) { - let error = new DS.AdapterError(); + let store = this.owner.lookup('service:store'); + let adapter = store.adapterFor('application'); + + let error = new AdapterError(); adapter.createRecord = function(store, type, snapshot) { return reject(error); @@ -695,13 +735,17 @@ module('integration/adapter/store-adapter - DS.Store and DS.Adapter integration }); test('if an updated record is marked as invalid by the server, it enters an error state', function(assert) { + let store = this.owner.lookup('service:store'); + let adapter = store.adapterFor('application'); + let Person = store.modelFor('person'); + adapter.shouldBackgroundReloadRecord = () => false; adapter.updateRecord = function(store, type, snapshot) { assert.equal(type, Person, 'the type is correct'); if (snapshot.attr('name').indexOf('Bro') === -1) { return reject( - new DS.InvalidError([ + new InvalidError([ { title: 'Invalid Attribute', detail: 'common... name requires a "bro"', @@ -717,7 +761,7 @@ module('integration/adapter/store-adapter - DS.Store and DS.Adapter integration }; let yehuda = run(() => { - env.store.push({ + store.push({ data: { type: 'person', id: '1', @@ -765,11 +809,14 @@ module('integration/adapter/store-adapter - DS.Store and DS.Adapter integration }); test('records can have errors on arbitrary properties after update', function(assert) { + let store = this.owner.lookup('service:store'); + let adapter = store.adapterFor('application'); + adapter.shouldBackgroundReloadRecord = () => false; adapter.updateRecord = function(store, type, snapshot) { if (snapshot.attr('name').indexOf('Bro') === -1) { return reject( - new DS.InvalidError([ + new InvalidError([ { title: 'Invalid Attribute', detail: 'is a generally unsavoury character', @@ -785,7 +832,7 @@ module('integration/adapter/store-adapter - DS.Store and DS.Adapter integration }; let yehuda = run(() => { - env.store.push({ + store.push({ data: { type: 'person', id: '1', @@ -842,6 +889,10 @@ module('integration/adapter/store-adapter - DS.Store and DS.Adapter integration }); test('if an updated record is marked as invalid by the server, you can attempt the save again', function(assert) { + let store = this.owner.lookup('service:store'); + let adapter = store.adapterFor('application'); + let Person = store.modelFor('person'); + let saveCount = 0; adapter.shouldBackgroundReloadRecord = () => false; adapter.updateRecord = function(store, type, snapshot) { @@ -849,7 +900,7 @@ module('integration/adapter/store-adapter - DS.Store and DS.Adapter integration saveCount++; if (snapshot.attr('name').indexOf('Bro') === -1) { return reject( - new DS.InvalidError([ + new InvalidError([ { title: 'Invalid Attribute', detail: 'common... name requires a "bro"', @@ -865,7 +916,7 @@ module('integration/adapter/store-adapter - DS.Store and DS.Adapter integration }; let yehuda = run(() => { - env.store.push({ + store.push({ data: { type: 'person', id: '1', @@ -922,7 +973,10 @@ module('integration/adapter/store-adapter - DS.Store and DS.Adapter integration }); test('if a updated record is marked as erred by the server, it enters an error state', function(assert) { - let error = new DS.AdapterError(); + let store = this.owner.lookup('service:store'); + let adapter = store.adapterFor('application'); + + let error = new AdapterError(); adapter.shouldBackgroundReloadRecord = () => false; adapter.updateRecord = function(store, type, snapshot) { @@ -930,7 +984,7 @@ module('integration/adapter/store-adapter - DS.Store and DS.Adapter integration }; let person = run(() => { - env.store.push({ + store.push({ data: { type: 'person', id: '1', @@ -957,9 +1011,13 @@ module('integration/adapter/store-adapter - DS.Store and DS.Adapter integration ); }); - test('can be created after the DS.Store', function(assert) { + test('can be created after the Store', function(assert) { assert.expect(1); + let store = this.owner.lookup('service:store'); + let adapter = store.adapterFor('application'); + let Person = store.modelFor('person'); + adapter.findRecord = function(store, type, id, snapshot) { assert.equal(type, Person, 'the type is correct'); return resolve({ data: { id: 1, type: 'person' } }); @@ -969,8 +1027,12 @@ module('integration/adapter/store-adapter - DS.Store and DS.Adapter integration }); test('relationships returned via `commit` do not trigger additional findManys', async function(assert) { + let store = this.owner.lookup('service:store'); + let adapter = store.adapterFor('application'); + let Person = store.modelFor('person'); + Person.reopen({ - dogs: DS.hasMany('dog', { async: false }), + dogs: hasMany('dog', { async: false }), }); store.push({ @@ -1002,7 +1064,7 @@ module('integration/adapter/store-adapter - DS.Store and DS.Adapter integration adapter.updateRecord = function(store, type, snapshot) { return new EmberPromise((resolve, reject) => { - env.store.push({ + store.push({ data: { type: 'person', id: '1', @@ -1043,13 +1105,17 @@ module('integration/adapter/store-adapter - DS.Store and DS.Adapter integration }); test("relationships don't get reset if the links is the same", function(assert) { - adapter.shouldBackgroundReloadRecord = () => false; + let store = this.owner.lookup('service:store'); + let adapter = store.adapterFor('application'); + let Person = store.modelFor('person'); + Person.reopen({ - dogs: DS.hasMany({ async: true }), + dogs: hasMany({ async: true }), }); - let count = 0; + adapter.shouldBackgroundReloadRecord = () => false; + let count = 0; adapter.findHasMany = function(store, snapshot, link, relationship) { assert.ok(count++ === 0, 'findHasMany is only called once'); @@ -1103,7 +1169,7 @@ module('integration/adapter/store-adapter - DS.Store and DS.Adapter integration }, }, }); - assert.ok(tom.get('dogs') instanceof DS.PromiseArray, 'dogs is a promise'); + assert.ok(tom.get('dogs') instanceof PromiseArray, 'dogs is a promise'); return tom.get('dogs'); }) .then(dogs => { @@ -1113,8 +1179,12 @@ module('integration/adapter/store-adapter - DS.Store and DS.Adapter integration }); test('async hasMany always returns a promise', function(assert) { + let store = this.owner.lookup('service:store'); + let adapter = store.adapterFor('application'); + let Person = store.modelFor('person'); + Person.reopen({ - dogs: DS.hasMany({ async: true }), + dogs: hasMany({ async: true }), }); adapter.createRecord = function(store, type, snapshot) { @@ -1135,12 +1205,12 @@ module('integration/adapter/store-adapter - DS.Store and DS.Adapter integration let tom = store.createRecord('person', { name: 'Tom Dale' }); run(() => { - assert.ok(tom.get('dogs') instanceof DS.PromiseArray, 'dogs is a promise before save'); + assert.ok(tom.get('dogs') instanceof PromiseArray, 'dogs is a promise before save'); }); return run(() => { return tom.save().then(() => { - assert.ok(tom.get('dogs') instanceof DS.PromiseArray, 'dogs is a promise after save'); + assert.ok(tom.get('dogs') instanceof PromiseArray, 'dogs is a promise after save'); }); }); }); @@ -1148,8 +1218,11 @@ module('integration/adapter/store-adapter - DS.Store and DS.Adapter integration test('createRecord receives a snapshot', function(assert) { assert.expect(1); + let store = this.owner.lookup('service:store'); + let adapter = store.adapterFor('application'); + adapter.createRecord = function(store, type, snapshot) { - assert.ok(snapshot instanceof DS.Snapshot, 'snapshot is an instance of DS.Snapshot'); + assert.ok(snapshot instanceof Snapshot, 'snapshot is an instance of Snapshot'); return resolve(); }; @@ -1161,8 +1234,11 @@ module('integration/adapter/store-adapter - DS.Store and DS.Adapter integration test('updateRecord receives a snapshot', function(assert) { assert.expect(1); + let store = this.owner.lookup('service:store'); + let adapter = store.adapterFor('application'); + adapter.updateRecord = function(store, type, snapshot) { - assert.ok(snapshot instanceof DS.Snapshot, 'snapshot is an instance of DS.Snapshot'); + assert.ok(snapshot instanceof Snapshot, 'snapshot is an instance of Snapshot'); return resolve(); }; @@ -1190,8 +1266,11 @@ module('integration/adapter/store-adapter - DS.Store and DS.Adapter integration test('deleteRecord receives a snapshot', function(assert) { assert.expect(1); + let store = this.owner.lookup('service:store'); + let adapter = store.adapterFor('application'); + adapter.deleteRecord = function(store, type, snapshot) { - assert.ok(snapshot instanceof DS.Snapshot, 'snapshot is an instance of DS.Snapshot'); + assert.ok(snapshot instanceof Snapshot, 'snapshot is an instance of Snapshot'); return resolve(); }; @@ -1219,8 +1298,11 @@ module('integration/adapter/store-adapter - DS.Store and DS.Adapter integration test('findRecord receives a snapshot', function(assert) { assert.expect(1); + let store = this.owner.lookup('service:store'); + let adapter = store.adapterFor('application'); + adapter.findRecord = function(store, type, id, snapshot) { - assert.ok(snapshot instanceof DS.Snapshot, 'snapshot is an instance of DS.Snapshot'); + assert.ok(snapshot instanceof Snapshot, 'snapshot is an instance of Snapshot'); return resolve({ data: { id: 1, type: 'person' } }); }; @@ -1230,14 +1312,18 @@ module('integration/adapter/store-adapter - DS.Store and DS.Adapter integration test('findMany receives an array of snapshots', function(assert) { assert.expect(2); + let store = this.owner.lookup('service:store'); + let adapter = store.adapterFor('application'); + let Person = store.modelFor('person'); + Person.reopen({ - dogs: DS.hasMany({ async: true }), + dogs: hasMany({ async: true }), }); adapter.coalesceFindRequests = true; adapter.findMany = function(store, type, ids, snapshots) { - assert.ok(snapshots[0] instanceof DS.Snapshot, 'snapshots[0] is an instance of DS.Snapshot'); - assert.ok(snapshots[1] instanceof DS.Snapshot, 'snapshots[1] is an instance of DS.Snapshot'); + assert.ok(snapshots[0] instanceof Snapshot, 'snapshots[0] is an instance of Snapshot'); + assert.ok(snapshots[1] instanceof Snapshot, 'snapshots[1] is an instance of Snapshot'); return resolve({ data: [{ id: 2, type: 'dog' }, { id: 3, type: 'dog' }] }); }; @@ -1264,12 +1350,16 @@ module('integration/adapter/store-adapter - DS.Store and DS.Adapter integration test('findHasMany receives a snapshot', function(assert) { assert.expect(1); + let store = this.owner.lookup('service:store'); + let adapter = store.adapterFor('application'); + let Person = store.modelFor('person'); + Person.reopen({ - dogs: DS.hasMany({ async: true }), + dogs: hasMany({ async: true }), }); - env.adapter.findHasMany = function(store, snapshot, link, relationship) { - assert.ok(snapshot instanceof DS.Snapshot, 'snapshot is an instance of DS.Snapshot'); + adapter.findHasMany = function(store, snapshot, link, relationship) { + assert.ok(snapshot instanceof Snapshot, 'snapshot is an instance of Snapshot'); return resolve({ data: [{ id: 2, type: 'dog' }, { id: 3, type: 'dog' }] }); }; @@ -1298,12 +1388,16 @@ module('integration/adapter/store-adapter - DS.Store and DS.Adapter integration test('findBelongsTo receives a snapshot', function(assert) { assert.expect(1); + let store = this.owner.lookup('service:store'); + let adapter = store.adapterFor('application'); + let Person = store.modelFor('person'); + Person.reopen({ - dog: DS.belongsTo({ async: true }), + dog: belongsTo({ async: true }), }); - env.adapter.findBelongsTo = function(store, snapshot, link, relationship) { - assert.ok(snapshot instanceof DS.Snapshot, 'snapshot is an instance of DS.Snapshot'); + adapter.findBelongsTo = function(store, snapshot, link, relationship) { + assert.ok(snapshot instanceof Snapshot, 'snapshot is an instance of Snapshot'); return resolve({ data: { id: 2, type: 'dog' } }); }; @@ -1332,7 +1426,10 @@ module('integration/adapter/store-adapter - DS.Store and DS.Adapter integration test('record.save should pass adapterOptions to the updateRecord method', function(assert) { assert.expect(1); - env.adapter.updateRecord = function(store, type, snapshot) { + let store = this.owner.lookup('service:store'); + let adapter = store.adapterFor('application'); + + adapter.updateRecord = function(store, type, snapshot) { assert.deepEqual(snapshot.adapterOptions, { subscribe: true }); return resolve({ data: { id: 1, type: 'person' } }); }; @@ -1355,7 +1452,10 @@ module('integration/adapter/store-adapter - DS.Store and DS.Adapter integration test('record.save should pass adapterOptions to the createRecord method', function(assert) { assert.expect(1); - env.adapter.createRecord = function(store, type, snapshot) { + let store = this.owner.lookup('service:store'); + let adapter = store.adapterFor('application'); + + adapter.createRecord = function(store, type, snapshot) { assert.deepEqual(snapshot.adapterOptions, { subscribe: true }); return resolve({ data: { id: 1, type: 'person' } }); }; @@ -1368,7 +1468,10 @@ module('integration/adapter/store-adapter - DS.Store and DS.Adapter integration test('record.save should pass adapterOptions to the deleteRecord method', function(assert) { assert.expect(1); - env.adapter.deleteRecord = function(store, type, snapshot) { + let store = this.owner.lookup('service:store'); + let adapter = store.adapterFor('application'); + + adapter.deleteRecord = function(store, type, snapshot) { assert.deepEqual(snapshot.adapterOptions, { subscribe: true }); return resolve({ data: { id: 1, type: 'person' } }); }; @@ -1391,7 +1494,10 @@ module('integration/adapter/store-adapter - DS.Store and DS.Adapter integration test('store.findRecord should pass adapterOptions to adapter.findRecord', function(assert) { assert.expect(1); - env.adapter.findRecord = function(store, type, id, snapshot) { + let store = this.owner.lookup('service:store'); + let adapter = store.adapterFor('application'); + + adapter.findRecord = function(store, type, id, snapshot) { assert.deepEqual(snapshot.adapterOptions, { query: { embed: true } }); return resolve({ data: { id: 1, type: 'person' } }); }; @@ -1404,7 +1510,10 @@ module('integration/adapter/store-adapter - DS.Store and DS.Adapter integration test('store.query should pass adapterOptions to adapter.query ', function(assert) { assert.expect(2); - env.adapter.query = function(store, type, query, array, options) { + let store = this.owner.lookup('service:store'); + let adapter = store.adapterFor('application'); + + adapter.query = function(store, type, query, array, options) { assert.ok(!('adapterOptions' in query)); assert.deepEqual(options.adapterOptions, { query: { embed: true } }); return { data: [] }; @@ -1418,7 +1527,10 @@ module('integration/adapter/store-adapter - DS.Store and DS.Adapter integration test('store.queryRecord should pass adapterOptions to adapter.queryRecord', function(assert) { assert.expect(2); - env.adapter.queryRecord = function(store, type, query, snapshot) { + let store = this.owner.lookup('service:store'); + let adapter = store.adapterFor('application'); + + adapter.queryRecord = function(store, type, query, snapshot) { assert.ok(!('adapterOptions' in query)); assert.deepEqual(snapshot.adapterOptions, { query: { embed: true } }); return { data: { type: 'person', id: 1, attributes: {} } }; @@ -1432,7 +1544,10 @@ module('integration/adapter/store-adapter - DS.Store and DS.Adapter integration test("store.findRecord should pass 'include' to adapter.findRecord", function(assert) { assert.expect(1); - env.adapter.findRecord = (store, type, id, snapshot) => { + let store = this.owner.lookup('service:store'); + let adapter = store.adapterFor('application'); + + adapter.findRecord = (store, type, id, snapshot) => { assert.equal(snapshot.include, 'books', 'include passed to adapter.findRecord'); return resolve({ data: { id: 1, type: 'person' } }); }; @@ -1443,7 +1558,10 @@ module('integration/adapter/store-adapter - DS.Store and DS.Adapter integration test('store.findAll should pass adapterOptions to the adapter.findAll method', function(assert) { assert.expect(1); - env.adapter.findAll = function(store, type, sinceToken, arraySnapshot) { + let store = this.owner.lookup('service:store'); + let adapter = store.adapterFor('application'); + + adapter.findAll = function(store, type, sinceToken, arraySnapshot) { let adapterOptions = arraySnapshot.adapterOptions; assert.deepEqual(adapterOptions, { query: { embed: true } }); return resolve({ data: [{ id: 1, type: 'person' }] }); @@ -1457,7 +1575,10 @@ module('integration/adapter/store-adapter - DS.Store and DS.Adapter integration test("store.findAll should pass 'include' to adapter.findAll", function(assert) { assert.expect(1); - env.adapter.findAll = function(store, type, sinceToken, arraySnapshot) { + let store = this.owner.lookup('service:store'); + let adapter = store.adapterFor('application'); + + adapter.findAll = function(store, type, sinceToken, arraySnapshot) { assert.equal(arraySnapshot.include, 'books', 'include passed to adapter.findAll'); return resolve({ data: [{ id: 1, type: 'person' }] }); }; @@ -1466,44 +1587,41 @@ module('integration/adapter/store-adapter - DS.Store and DS.Adapter integration }); test('An async hasMany relationship with links should not trigger shouldBackgroundReloadRecord', function(assert) { - const Post = DS.Model.extend({ - name: DS.attr('string'), - comments: DS.hasMany('comment', { async: true }), + const Post = Model.extend({ + name: attr('string'), + comments: hasMany('comment', { async: true }), }); - const Comment = DS.Model.extend({ - name: DS.attr('string'), + const Comment = Model.extend({ + name: attr('string'), }); - env = setupStore({ - post: Post, - comment: Comment, - adapter: DS.RESTAdapter.extend({ - findRecord() { - return { - posts: { - id: 1, - name: 'Rails is omakase', - links: { comments: '/posts/1/comments' }, - }, - }; - }, - findHasMany() { - return resolve({ - comments: [ - { id: 1, name: 'FIRST' }, - { id: 2, name: 'Rails is unagi' }, - { id: 3, name: 'What is omakase?' }, - ], - }); - }, - shouldBackgroundReloadRecord() { - assert.ok(false, 'shouldBackgroundReloadRecord should not be called'); - }, - }), + const ApplicationAdapter = RESTAdapter.extend({ + findRecord() { + return { + posts: { + id: 1, + name: 'Rails is omakase', + links: { comments: '/posts/1/comments' }, + }, + }; + }, + findHasMany() { + return resolve({ + comments: [{ id: 1, name: 'FIRST' }, { id: 2, name: 'Rails is unagi' }, { id: 3, name: 'What is omakase?' }], + }); + }, + shouldBackgroundReloadRecord() { + assert.ok(false, 'shouldBackgroundReloadRecord should not be called'); + }, }); - store = env.store; + this.owner.register('model:post', Post); + this.owner.register('model:comment', Comment); + this.owner.register('adapter:application', ApplicationAdapter); + this.owner.register('serializer:application', RESTSerializer.extend()); + + let store = this.owner.lookup('service:store'); return run(() => store @@ -1518,6 +1636,9 @@ module('integration/adapter/store-adapter - DS.Store and DS.Adapter integration }); testInDebug('There should be a friendly error for if the adapter does not implement createRecord', function(assert) { + let store = this.owner.lookup('service:store'); + let adapter = store.adapterFor('application'); + adapter.createRecord = null; let tom = run(() => store.createRecord('person', { name: 'Tom Dale' })); @@ -1530,6 +1651,9 @@ module('integration/adapter/store-adapter - DS.Store and DS.Adapter integration }); testInDebug('There should be a friendly error for if the adapter does not implement updateRecord', function(assert) { + let store = this.owner.lookup('service:store'); + let adapter = store.adapterFor('application'); + adapter.updateRecord = null; let tom = run(() => store.push({ data: { type: 'person', id: 1 } })); @@ -1541,6 +1665,9 @@ module('integration/adapter/store-adapter - DS.Store and DS.Adapter integration }); testInDebug('There should be a friendly error for if the adapter does not implement deleteRecord', function(assert) { + let store = this.owner.lookup('service:store'); + let adapter = store.adapterFor('application'); + adapter.deleteRecord = null; let tom = run(() => store.push({ data: { type: 'person', id: 1 } }));