Skip to content

Commit

Permalink
[CHORE] Refactor integration/multiple-stores-test.js in order to remo…
Browse files Browse the repository at this point in the history
…ve run loop usage (#6632)
  • Loading branch information
dmuneras authored and runspired committed Oct 24, 2019
1 parent 55cec65 commit 4a44a18
Showing 1 changed file with 88 additions and 82 deletions.
170 changes: 88 additions & 82 deletions packages/-ember-data/tests/integration/multiple-stores-test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { run } from '@ember/runloop';
import { setupTest } from 'ember-qunit';
import { module, test } from 'qunit';
import { get } from '@ember/object';
// we intentionally test against the ember-data version here
// because the ember-data/store uses DefaultRecordData while @ember-data/store does not
import Store from 'ember-data/store';
Expand All @@ -18,22 +16,15 @@ module('integration/multiple_stores - Multiple Stores Tests', function(hooks) {
firstName: attr('string'),
lastName: attr('string'),
homePlanet: belongsTo('home-planet', { inverse: 'villains', async: false }),
evilMinions: hasMany('evil-minion', { async: false }),
});

const HomePlanet = Model.extend({
name: attr('string'),
villains: hasMany('super-villain', { inverse: 'homePlanet', async: false }),
});

const EvilMinion = Model.extend({
superVillain: belongsTo('super-villain', { async: false }),
name: attr('string'),
});

this.owner.register('model:super-villain', SuperVillain);
this.owner.register('model:home-planet', HomePlanet);
this.owner.register('model:evil-minion', EvilMinion);

this.owner.register('adapter:application', RESTAdapter);
this.owner.register('serializer:application', RESTSerializer);
Expand All @@ -42,42 +33,37 @@ module('integration/multiple_stores - Multiple Stores Tests', function(hooks) {
this.owner.register('store:store-b', Store);
});

test('should be able to push into multiple stores', function(assert) {
test('should be able to push into multiple stores', async function(assert) {
this.owner.register(
'adapter:home-planet',
RESTAdapter.extend({
shouldBackgroundReloadRecord: () => false,
})
);

let store = this.owner.lookup('service:store');
let store_a = this.owner.lookup('store:store-a');
let store_b = this.owner.lookup('store:store-b');
const andromedaStore = this.owner.lookup('service:store');
const cartwheelStore = this.owner.lookup('store:store-a');
const cigarStore = this.owner.lookup('store:store-b');

let home_planet_main = { id: '1', name: 'Earth' };
let home_planet_a = { id: '1', name: 'Mars' };
let home_planet_b = { id: '1', name: 'Saturn' };
const earth = { id: '1', name: 'Earth' };
const mars = { id: '1', name: 'Mars' };
const saturn = { id: '1', name: 'Saturn' };

run(() => {
store.push(store.normalize('home-planet', home_planet_main));
store_a.push(store_a.normalize('home-planet', home_planet_a));
store_b.push(store_b.normalize('home-planet', home_planet_b));
});
andromedaStore.push(andromedaStore.normalize('home-planet', earth));
cartwheelStore.push(cartwheelStore.normalize('home-planet', mars));
cigarStore.push(cigarStore.normalize('home-planet', saturn));

return store
.findRecord('home-planet', 1)
.then(homePlanet => {
assert.equal(homePlanet.get('name'), 'Earth');
let homePlanet = await andromedaStore.findRecord('home-planet', '1');

return store_a.findRecord('homePlanet', 1);
})
.then(homePlanet => {
assert.equal(homePlanet.get('name'), 'Mars');
return store_b.findRecord('homePlanet', 1);
})
.then(homePlanet => {
assert.equal(homePlanet.get('name'), 'Saturn');
});
assert.equal(homePlanet.name, 'Earth');

homePlanet = await cartwheelStore.findRecord('home-planet', '1');

assert.equal(homePlanet.name, 'Mars');

homePlanet = await cigarStore.findRecord('home-planet', '1');

assert.equal(homePlanet.name, 'Saturn');
});

test('embedded records should be created in multiple stores', function(assert) {
Expand All @@ -90,15 +76,15 @@ module('integration/multiple_stores - Multiple Stores Tests', function(hooks) {
})
);

let store = this.owner.lookup('service:store');
let store_a = this.owner.lookup('store:store-a');
let store_b = this.owner.lookup('store:store-b');
const andromedaStore = this.owner.lookup('service:store');
const cartwheelStore = this.owner.lookup('store:store-a');
const cigarStore = this.owner.lookup('store:store-b');

let serializer_main = store.serializerFor('home-planet');
let serializer_a = store_a.serializerFor('home-planet');
let serializer_b = store_b.serializerFor('home-planet');
const andromedaSerializer = andromedaStore.serializerFor('home-planet');
const cartwheelSerializer = cartwheelStore.serializerFor('home-planet');
const cigarSerializer = cigarStore.serializerFor('home-planet');

let json_hash_main = {
const andromedaJsonPayload = {
homePlanet: {
id: '1',
name: 'Earth',
Expand All @@ -111,7 +97,7 @@ module('integration/multiple_stores - Multiple Stores Tests', function(hooks) {
],
},
};
let json_hash_a = {
const cartWheelJsonPayload = {
homePlanet: {
id: '1',
name: 'Mars',
Expand All @@ -124,71 +110,91 @@ module('integration/multiple_stores - Multiple Stores Tests', function(hooks) {
],
},
};
let json_hash_b = {
const cigarJsonPayload = {
homePlanet: {
id: '1',
name: 'Saturn',
villains: [
{
id: '1',
firstName: 'Jade',
lastName: 'John',
firstName: 'Damita',
lastName: 'Giraldo',
},
],
},
};
let json_main, json_a, json_b;

run(() => {
json_main = serializer_main.normalizeResponse(
store,
store.modelFor('home-planet'),
json_hash_main,
1,
'findRecord'
);
store.push(json_main);
assert.equal(store.hasRecordForId('super-villain', '1'), true, 'superVillain should exist in service:store');
});

run(() => {
json_a = serializer_a.normalizeResponse(store_a, store_a.modelFor('home-planet'), json_hash_a, 1, 'findRecord');
store_a.push(json_a);
assert.equal(store_a.hasRecordForId('super-villain', '1'), true, 'superVillain should exist in store:store-a');
});
const normalizedAndromedaPayload = andromedaSerializer.normalizeResponse(
andromedaStore,
andromedaStore.modelFor('home-planet'),
andromedaJsonPayload,
'1',
'findRecord'
);

run(() => {
json_b = serializer_b.normalizeResponse(store_b, store_a.modelFor('home-planet'), json_hash_b, 1, 'findRecord');
store_b.push(json_b);
assert.equal(store_b.hasRecordForId('super-villain', '1'), true, 'superVillain should exist in store:store-b');
});
andromedaStore.push(normalizedAndromedaPayload);
assert.equal(
andromedaStore.hasRecordForId('super-villain', '1'),
true,
'superVillain should exist in service:store'
);

const normalizedCartWheelPayload = cartwheelSerializer.normalizeResponse(
cartwheelStore,
cartwheelStore.modelFor('home-planet'),
cartWheelJsonPayload,
'1',
'findRecord'
);

cartwheelStore.push(normalizedCartWheelPayload);
assert.equal(
cartwheelStore.hasRecordForId('super-villain', '1'),
true,
'superVillain should exist in store:store-a'
);

const normalizedCigarPayload = cigarSerializer.normalizeResponse(
cigarStore,
cigarStore.modelFor('home-planet'),
cigarJsonPayload,
'1',
'findRecord'
);
cigarStore.push(normalizedCigarPayload);

assert.equal(cigarStore.hasRecordForId('super-villain', '1'), true, 'superVillain should exist in store:store-b');
});

test('each store should have a unique instance of the serializers', function(assert) {
this.owner.register('serializer:home-planet', RESTSerializer.extend({}));

let store_a = this.owner.lookup('store:store-a');
let store_b = this.owner.lookup('store:store-b');
const andromedaStore = this.owner.lookup('store:store-a');
const cigarStore = this.owner.lookup('store:store-b');

let serializer_a = store_a.serializerFor('home-planet');
let serializer_b = store_b.serializerFor('home-planet');
const andromedaSerializer = andromedaStore.serializerFor('home-planet');
const cigarSerializer = cigarStore.serializerFor('home-planet');

assert.equal(get(serializer_a, 'store'), store_a, "serializer_a's store prop should be sotre_a");
assert.equal(get(serializer_b, 'store'), store_b, "serializer_b's store prop should be sotre_b");
assert.notEqual(serializer_a, serializer_b, 'serialier_a and serialier_b should be unique instances');
assert.equal(
andromedaSerializer.store,
andromedaStore,
"andromedaSerializer's store prop should be andromedaStore"
);
assert.equal(cigarSerializer.store, cigarStore, "cigarSerializer's store prop should be cigarStore");
assert.notEqual(andromedaSerializer, cigarSerializer, 'andromedaStore and cigarStore should be unique instances');
});

test('each store should have a unique instance of the adapters', function(assert) {
this.owner.register('adapter:home-planet', Adapter.extend({}));

let store_a = this.owner.lookup('store:store-a');
let store_b = this.owner.lookup('store:store-b');
const andromedaStore = this.owner.lookup('store:store-a');
const cigarStore = this.owner.lookup('store:store-b');

let adapter_a = store_a.adapterFor('home-planet');
let adapter_b = store_b.adapterFor('home-planet');
const andromedaAdapter = andromedaStore.adapterFor('home-planet');
const cigarAdapter = cigarStore.adapterFor('home-planet');

assert.equal(get(adapter_a, 'store'), store_a);
assert.equal(get(adapter_b, 'store'), store_b);
assert.notEqual(adapter_a, adapter_b);
assert.equal(andromedaAdapter.store, andromedaStore);
assert.equal(cigarAdapter.store, cigarStore);
assert.notEqual(andromedaAdapter, cigarAdapter);
});
});

0 comments on commit 4a44a18

Please sign in to comment.