Skip to content

Commit

Permalink
[CHORE tests] Replace setupStore for adapter integration tests
Browse files Browse the repository at this point in the history
Part of emberjs#6166

This replaces usage of the `setupStore` helper for
`tests/integration/adapter/*`.
  • Loading branch information
HeroicEric committed Aug 22, 2019
1 parent c3cfbb9 commit 33f2cfc
Show file tree
Hide file tree
Showing 8 changed files with 475 additions and 306 deletions.
Original file line number Diff line number Diff line change
@@ -1,43 +1,45 @@
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();
}

assert.ok(false, 'unreachable');
};

let bookstore = this.store.push({
let bookstore = store.push({
data: {
id: '1',
type: 'bookstore',
Expand Down Expand Up @@ -70,18 +72,18 @@ 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 } });

run(() => book2.unloadRecord());

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',
Expand Down
91 changes: 52 additions & 39 deletions packages/-ember-data/tests/integration/adapter/find-test.js
Original file line number Diff line number Diff line change
@@ -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`);
Expand All @@ -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');
Expand All @@ -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);
Expand All @@ -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');
Expand Down Expand Up @@ -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');
Expand All @@ -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');
Expand All @@ -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');
Expand All @@ -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: {
Expand All @@ -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) {
Expand All @@ -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: {
Expand All @@ -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'/
);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -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'),
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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: {
Expand Down
Loading

0 comments on commit 33f2cfc

Please sign in to comment.