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 20, 2019
1 parent 7c7ebdf commit 6164e5a
Show file tree
Hide file tree
Showing 12 changed files with 1,394 additions and 1,388 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 @@ -105,7 +105,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,35 +1,35 @@
import { run } from '@ember/runloop';
import { Promise } from 'rsvp';
import { setupStore, createStore } from 'dummy/tests/helpers/store';

import { setupTest } from 'ember-qunit';
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) {
let 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 @@ -70,6 +70,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 @@ -109,6 +110,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 @@ -152,6 +154,7 @@ module('integration/record-arrays/adapter_populated_record_array - DS.AdapterPop
});

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

assert.throws(
Expand All @@ -164,8 +167,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 @@ -174,14 +177,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 @@ -190,8 +193,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 @@ -214,14 +217,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 @@ -239,8 +242,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 @@ -249,7 +252,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 @@ -282,18 +285,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({
let 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
Loading

0 comments on commit 6164e5a

Please sign in to comment.