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 19, 2019
1 parent 963645c commit e73aa7a
Show file tree
Hide file tree
Showing 12 changed files with 1,952 additions and 1,831 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 @@ -107,7 +107,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,36 @@
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) {
this.owner.register(
'adapter:application',
Adapter.extend({
deleteRecord() {
return Promise.resolve();
},
})
);

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

let payload = {
Expand Down Expand Up @@ -70,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 @@ -109,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 @@ -152,6 +155,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 +168,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 +178,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 +194,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 +218,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 +243,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 +253,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 +286,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,61 +1,60 @@
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({
coalesceFindRequests: true,
this.owner.register(
'adapter:application',
RESTAdapter.extend({
coalesceFindRequests: true,

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

GroupsAdapter.reopen({
ajax(url, type, options) {
requests.push({
url,
ids: options.data.ids,
});

let queryString = options.data.ids
.map(i => {
return encodeURIComponent('ids[]') + '=' + encodeURIComponent(i);
})
.join('&');
let fullUrl = url + '?' + queryString;

maxLength = this.get('maxURLLength');
lengths.push(fullUrl.length);

let testRecords = options.data.ids.map(id => ({ id }));
return EmberPromise.resolve({ testRecords: testRecords });
},
});
ajax(url, type, options) {
requests.push({
url,
ids: options.data.ids,
});

store = createStore({
adapter: GroupsAdapter,
testRecord: DS.Model.extend(),
});
});
let queryString = options.data.ids
.map(i => {
return encodeURIComponent('ids[]') + '=' + encodeURIComponent(i);
})
.join('&');
let fullUrl = url + '?' + queryString;

maxLength = this.get('maxURLLength');
lengths.push(fullUrl.length);

let testRecords = options.data.ids.map(id => ({ id }));
return EmberPromise.resolve({ testRecords: testRecords });
},
})
);

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 e73aa7a

Please sign in to comment.