Skip to content

Commit

Permalink
[CHORE] Refactor away from createStore test helper
Browse files Browse the repository at this point in the history
From emberjs#6166

This refactors some tests so that they don't rely on the `createStore`
test helper.

`createStore` was helpful in the past but is less so now that we have
[nice APIs for dependency injection](https://guides.emberjs.com/release/applications/dependency-injection/).

---

One test was removed from `unit/store/adapter-interop - Store working
with a Adapter` because I don't think it was correct.

Here was the test:

https://github.com/emberjs/data/blob/39182f324d5777e4a75c4a582f0791631e37d8de/packages/-ember-data/tests/unit/store/adapter-interop-test.js#L28-L32

This test was, at one time, testing that it was possible to pass a
factory as the `adapter` property to `Store`. This feature was removed
in emberjs#3191 but the test continued
passing because of the way the `createStore` works.

https://github.com/emberjs/data/blob/39182f324d5777e4a75c4a582f0791631e37d8de/packages/-ember-data/tests/helpers/store.js#L57-L60
  • Loading branch information
HeroicEric committed Aug 22, 2019
1 parent d10bbc0 commit 0f2acb6
Show file tree
Hide file tree
Showing 12 changed files with 760 additions and 759 deletions.
4 changes: 0 additions & 4 deletions packages/-ember-data/tests/helpers/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,3 @@ export default function setupStore(options) {
}

export { setupStore };

export function createStore(options) {
return setupStore(options).store;
}
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
import { run } from '@ember/runloop';
import { Promise } from 'rsvp';
import { setupStore, createStore } from 'dummy/tests/helpers/store';
import { setupTest } from 'ember-qunit';
import { settled } from '@ember/test-helpers';

import { module, test } from 'qunit';

import DS from 'ember-data';

let store;
import Adapter from '@ember-data/adapter';
import Model, { attr } from '@ember-data/model';

const Person = DS.Model.extend({
name: DS.attr('string'),
const Person = Model.extend({
name: attr('string'),
toString() {
return `<Person#${this.get('id')}>`;
},
});

const adapter = DS.Adapter.extend({
deleteRecord() {
return Promise.resolve();
},
});
module('integration/record-arrays/adapter_populated_record_array - AdapterPopulatedRecordArray', function(hooks) {
setupTest(hooks);

module('integration/record-arrays/adapter_populated_record_array - DS.AdapterPopulatedRecordArray', function(hooks) {
hooks.beforeEach(function() {
store = createStore({
adapter: adapter,
person: Person,
});
this.owner.register('model:person', Person);
});

test('when a record is deleted in an adapter populated record array, it should be removed', function(assert) {
const ApplicationAdapter = Adapter.extend({
deleteRecord() {
return Promise.resolve();
},
});

this.owner.register('adapter:application', ApplicationAdapter);

let store = this.owner.lookup('service:store');
let recordArray = store.recordArrayManager.createAdapterPopulatedRecordArray('person', null);

let payload = {
Expand Down Expand Up @@ -71,6 +71,7 @@ module('integration/record-arrays/adapter_populated_record_array - DS.AdapterPop
});

test('stores the metadata off the payload', function(assert) {
let store = this.owner.lookup('service:store');
let recordArray = store.recordArrayManager.createAdapterPopulatedRecordArray('person', null);

let payload = {
Expand Down Expand Up @@ -110,6 +111,7 @@ module('integration/record-arrays/adapter_populated_record_array - DS.AdapterPop
});

test('stores the links off the payload', function(assert) {
let store = this.owner.lookup('service:store');
let recordArray = store.recordArrayManager.createAdapterPopulatedRecordArray('person', null);

let payload = {
Expand Down Expand Up @@ -153,7 +155,9 @@ module('integration/record-arrays/adapter_populated_record_array - DS.AdapterPop
});

test('recordArray.replace() throws error', async function(assert) {
let store = this.owner.lookup('service:store');
let recordArray = store.recordArrayManager.createAdapterPopulatedRecordArray('person', null);

await settled();

assert.throws(
Expand All @@ -166,8 +170,8 @@ module('integration/record-arrays/adapter_populated_record_array - DS.AdapterPop
});

test('pass record array to adapter.query regardless of arity', function(assert) {
let env = setupStore({ person: Person });
let store = env.store;
let store = this.owner.lookup('service:store');
let adapter = store.adapterFor('application');

let payload = {
data: [
Expand All @@ -176,14 +180,14 @@ module('integration/record-arrays/adapter_populated_record_array - DS.AdapterPop
],
};

env.adapter.query = function(store, type, query) {
adapter.query = function(store, type, query) {
// Due to #6232, we now expect 5 arguments regardless of arity
assert.equal(arguments.length, 5);
return payload;
};

return store.query('person', {}).then(recordArray => {
env.adapter.query = function(store, type, query, _recordArray) {
adapter.query = function(store, type, query, _recordArray) {
assert.equal(arguments.length, 5);
return payload;
};
Expand All @@ -192,8 +196,8 @@ module('integration/record-arrays/adapter_populated_record_array - DS.AdapterPop
});

test('pass record array to adapter.query regardless of arity', function(assert) {
let env = setupStore({ person: Person });
let store = env.store;
let store = this.owner.lookup('service:store');
let adapter = store.adapterFor('application');

let payload = {
data: [
Expand All @@ -216,14 +220,14 @@ module('integration/record-arrays/adapter_populated_record_array - DS.AdapterPop
return superCreateAdapterPopulatedRecordArray.apply(this, arguments);
};

env.adapter.query = function(store, type, query) {
adapter.query = function(store, type, query) {
// Due to #6232, we now expect 5 arguments regardless of arity
assert.equal(arguments.length, 5);
return payload;
};

return store.query('person', actualQuery).then(recordArray => {
env.adapter.query = function(store, type, query, _recordArray) {
adapter.query = function(store, type, query, _recordArray) {
assert.equal(arguments.length, 5);
return payload;
};
Expand All @@ -241,8 +245,8 @@ module('integration/record-arrays/adapter_populated_record_array - DS.AdapterPop
});

test('loadRecord re-syncs internalModels recordArrays', function(assert) {
let env = setupStore({ person: Person });
let store = env.store;
let store = this.owner.lookup('service:store');
let adapter = store.adapterFor('application');

let payload = {
data: [
Expand All @@ -251,7 +255,7 @@ module('integration/record-arrays/adapter_populated_record_array - DS.AdapterPop
],
};

env.adapter.query = function(store, type, query, recordArray) {
adapter.query = function(store, type, query, recordArray) {
return payload;
};

Expand Down Expand Up @@ -284,18 +288,18 @@ module('integration/record-arrays/adapter_populated_record_array - DS.AdapterPop
assert.expect(8);

let queryPromise, queryArr, findPromise, findArray;
let env = setupStore({ person: Person });
let store = env.store;
let store = this.owner.lookup('service:store');
let adapter = store.adapterFor('application');
let array = [{ id: '1', type: 'person', attributes: { name: 'Scumbag Dale' } }];

// resemble server side filtering
env.adapter.query = function(store, type, query, recordArray) {
adapter.query = function(store, type, query, recordArray) {
return { data: array.slice(query.slice) };
};

// implement findAll to further test that query updates won't muddle
// with the non-query record arrays
env.adapter.findAll = function(store, type, sinceToken) {
adapter.findAll = function(store, type, sinceToken) {
return { data: array.slice(0) };
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
import Model, { attr } from '@ember-data/model';
import { run } from '@ember/runloop';
import { createStore } from 'dummy/tests/helpers/store';
import { setupTest } from 'ember-qunit';
import { module, test } from 'qunit';
import DS from 'ember-data';
import { get } from '@ember/object';
import { watchProperties } from '../../helpers/watch-property';

let store;

const Person = DS.Model.extend({
name: DS.attr('string'),
const Person = Model.extend({
name: attr('string'),
toString() {
return `<Person#${this.get('id')}>`;
},
});

module('integration/peeked-records', function(hooks) {
setupTest(hooks);

hooks.beforeEach(function() {
store = createStore({
person: Person,
});
this.owner.register('model:person', Person);
store = this.owner.lookup('service:store');
});

test('repeated calls to peekAll in separate run-loops works as expected', function(assert) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
import { run } from '@ember/runloop';
import { Promise as EmberPromise } from 'rsvp';
import { createStore } from 'dummy/tests/helpers/store';
import { setupTest } from 'ember-qunit';

import { module, test } from 'qunit';

import DS from 'ember-data';
import Model from '@ember-data/model';
import RESTAdapter from '@ember-data/adapter/rest';

let GroupsAdapter, store, requests;
let store, requests;
let maxLength;
let lengths;

module('unit/adapters/rest_adapter/group_records_for_find_many_test - DS.RESTAdapter#groupRecordsForFindMany', function(
hooks
) {
setupTest(hooks);

hooks.beforeEach(function() {
maxLength = -1;
requests = [];
lengths = [];

GroupsAdapter = DS.RESTAdapter.extend({
const ApplicationAdapter = RESTAdapter.extend({
coalesceFindRequests: true,

findRecord(store, type, id, snapshot) {
return { id };
},
});

GroupsAdapter.reopen({
ajax(url, type, options) {
requests.push({
url,
Expand All @@ -48,14 +49,10 @@ module('unit/adapters/rest_adapter/group_records_for_find_many_test - DS.RESTAda
},
});

store = createStore({
adapter: GroupsAdapter,
testRecord: DS.Model.extend(),
});
});
this.owner.register('adapter:application', ApplicationAdapter);
this.owner.register('model:test-record', Model.extend());

hooks.afterEach(function() {
run(store, 'destroy');
store = this.owner.lookup('service:store');
});

test('groupRecordsForFindMany - findMany', function(assert) {
Expand Down
65 changes: 29 additions & 36 deletions packages/-ember-data/tests/unit/debug-test.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,33 @@
import Model, { attr, belongsTo, hasMany } from '@ember-data/model';
import { computed } from '@ember/object';
import { createStore } from 'dummy/tests/helpers/store';
import { setupTest } from 'ember-qunit';

import { module, test } from 'qunit';

import DS from 'ember-data';
module('Debug', function(hooks) {
setupTest(hooks);

const TestAdapter = DS.Adapter.extend();

module('Debug', function() {
test('_debugInfo groups the attributes and relationships correctly', function(assert) {
const MaritalStatus = DS.Model.extend({
name: DS.attr('string'),
const MaritalStatus = Model.extend({
name: attr('string'),
});

const Post = DS.Model.extend({
title: DS.attr('string'),
const Post = Model.extend({
title: attr('string'),
});

const User = DS.Model.extend({
name: DS.attr('string'),
isDrugAddict: DS.attr('boolean'),
maritalStatus: DS.belongsTo('marital-status', { async: false }),
posts: DS.hasMany('post', { async: false }),
const User = Model.extend({
name: attr('string'),
isDrugAddict: attr('boolean'),
maritalStatus: belongsTo('marital-status', { async: false }),
posts: hasMany('post', { async: false }),
});

let store = createStore({
adapter: TestAdapter.extend(),
maritalStatus: MaritalStatus,
post: Post,
user: User,
});
this.owner.register('model:marital-status', MaritalStatus);
this.owner.register('model:post', Post);
this.owner.register('model:user', User);

let record = store.createRecord('user');
let record = this.owner.lookup('service:store').createRecord('user');

let propertyInfo = record._debugInfo().propertyInfo;

Expand All @@ -45,18 +41,18 @@ module('Debug', function() {
});

test('_debugInfo supports arbitray relationship types', function(assert) {
const MaritalStatus = DS.Model.extend({
name: DS.attr('string'),
const MaritalStatus = Model.extend({
name: attr('string'),
});

const Post = DS.Model.extend({
title: DS.attr('string'),
const Post = Model.extend({
title: attr('string'),
});

const User = DS.Model.extend({
name: DS.attr('string'),
isDrugAddict: DS.attr('boolean'),
maritalStatus: DS.belongsTo('marital-status', { async: false }),
const User = Model.extend({
name: attr('string'),
isDrugAddict: attr('boolean'),
maritalStatus: belongsTo('marital-status', { async: false }),
posts: computed(() => [1, 2, 3])
.readOnly()
.meta({
Expand All @@ -68,14 +64,11 @@ module('Debug', function() {
}),
});

let store = createStore({
adapter: TestAdapter.extend(),
maritalStatus: MaritalStatus,
post: Post,
user: User,
});
this.owner.register('model:marital-status', MaritalStatus);
this.owner.register('model:post', Post);
this.owner.register('model:user', User);

let record = store.createRecord('user');
let record = this.owner.lookup('service:store').createRecord('user');

let propertyInfo = record._debugInfo().propertyInfo;

Expand Down
Loading

0 comments on commit 0f2acb6

Please sign in to comment.