Skip to content

Commit

Permalink
More cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
pliljegr committed Jul 16, 2019
1 parent 6fd0a3d commit f80b5ec
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 44 deletions.
7 changes: 4 additions & 3 deletions packages/-ember-data/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,14 @@
"ember-qunit": "*",
"ember-qunit-assert-helpers": "*",
"ember-resolver": "*",
"ember-source": "*",
"ember-source": "~3.4.0",
"ember-source-channel-url": "*",
"ember-try": "*",
"github": "*",
"json-typescript": "*",
"loader.js": "*",
"typescript": "*"
"typescript": "*",
"@ember/jquery": "^0.6.0"
},
"engines": {
"node": ">= 8.0.0"
Expand All @@ -91,4 +92,4 @@
"ember-addon": {
"configPath": "tests/dummy/config"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ module('integration/adapter/find-all - Finding All Records of a Type', function(
);
});

test('When all records for a type are requested, records that are created on the client should be added to the record array.', assert => {
test('When all records for a type are requested, records that are created on the client should be added to the record array.', async assert => {
assert.expect(3);

let allRecords = store.peekAll('person');
Expand All @@ -153,6 +153,7 @@ module('integration/adapter/find-all - Finding All Records of a Type', function(
);

store.createRecord('person', { name: 'Carsten Nielsen' });
await settled();

assert.equal(get(allRecords, 'length'), 1, "the record array's length is 1");
assert.equal(
Expand Down
185 changes: 151 additions & 34 deletions packages/-ember-data/tests/integration/inverse-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ function stringify(string) {
module('integration/inverse_test - inverseFor', function(hooks) {
setupTest(hooks);
let store;
let user;
let job;

hooks.beforeEach(function() {
let { owner } = this;
store = owner.lookup('service:store');
});

test('Finds the inverse when there is only one possible available', function(assert) {
class User extends Model {
@attr()
name;
Expand Down Expand Up @@ -47,25 +49,12 @@ module('integration/inverse_test - inverseFor', function(hooks) {
}
}

class ReflexiveModel extends Model {
@belongsTo('reflexive-model', { async: false })
reflexiveProp;

toString() {
return stringify('reflexiveModel');
}
}
let { owner } = this;
owner.register('model:user', User);
owner.register('model:job', Job);
owner.register('model:reflexive-model', ReflexiveModel);

store = owner.lookup('service:store');

job = store.modelFor('job');
user = store.modelFor('user');
});

test('Finds the inverse when there is only one possible available', function(assert) {
let job = store.modelFor('job');
let user = store.modelFor('user');
let inverseDefinition = job.inverseFor('user', store);

assert.deepEqual(
Expand All @@ -83,13 +72,45 @@ module('integration/inverse_test - inverseFor', function(hooks) {
});

test('Finds the inverse when only one side has defined it manually', function(assert) {
job.reopen({
owner: belongsTo('user', { inverse: 'previousJob', async: false }),
});
class User extends Model {
@attr()
name;

user.reopen({
previousJob: belongsTo('job', { async: false }),
});
@belongsTo('user', { async: true, inverse: null })
bestFriend;

@belongsTo('job', { async: false })
job;

@belongsTo('job', { async: false })
previousJob;

toString() {
return stringify('user');
}
}

class Job extends Model {
@attr()
isGood;

@belongsTo('user', { async: false })
user;

@belongsTo('user', { inverse: 'previousJob', async: false })
owner;

toString() {
return stringify('job');
}
}

let { owner } = this;
owner.register('model:user', User);
owner.register('model:job', Job);

let job = store.modelFor('job');
let user = store.modelFor('user');

assert.deepEqual(
job.inverseFor('owner', store),
Expand Down Expand Up @@ -120,10 +141,38 @@ module('integration/inverse_test - inverseFor', function(hooks) {
});

test('Returns null if inverse relationship it is manually set with a different relationship key', function(assert) {
job.reopen({
user: belongsTo('user', { inverse: 'previousJob', async: false }),
});
class User extends Model {
@attr()
name;

@belongsTo('user', { async: true, inverse: null })
bestFriend;

@belongsTo('job', { async: false })
job;

toString() {
return stringify('user');
}
}

class Job extends Model {
@attr()
isGood;

@belongsTo('user', { inverse: 'previousJob', async: false })
user;

toString() {
return stringify('job');
}
}

let { owner } = this;
owner.register('model:user', User);
owner.register('model:job', Job);

let user = store.modelFor('user');
user.reopen({
job: belongsTo('job', { async: false }),
});
Expand All @@ -132,22 +181,78 @@ module('integration/inverse_test - inverseFor', function(hooks) {
});

testInDebug('Errors out if you define 2 inverses to the same model', function(assert) {
job.reopen({
user: belongsTo('user', { inverse: 'job', async: false }),
owner: belongsTo('user', { inverse: 'job', async: false }),
});
class User extends Model {
@attr()
name;

user.reopen({
job: belongsTo('job', { async: false }),
});
@belongsTo('user', { async: true, inverse: null })
bestFriend;

@belongsTo('job', { async: false })
job;

toString() {
return stringify('user');
}
}

class Job extends Model {
@attr()
isGood;

@belongsTo('user', { inverse: 'job', async: false })
user;

@belongsTo('user', { inverse: 'job', async: false })
owner;

toString() {
return stringify('job');
}
}
let { owner } = this;
owner.register('model:user', User);
owner.register('model:job', Job);

let user = store.modelFor('user');
assert.expectAssertion(() => {
user.inverseFor('job', store);
}, /Assertion Failed: You defined the 'job' relationship on model:user, but you defined the inverse relationships of type model:job multiple times/i);
});

test('Caches findInverseFor return value', function(assert) {
assert.expect(1);
class User extends Model {
@attr()
name;

@belongsTo('user', { async: true, inverse: null })
bestFriend;

@belongsTo('job', { async: false })
job;

toString() {
return stringify('user');
}
}

class Job extends Model {
@attr()
isGood;

@belongsTo('user', { async: false })
user;

toString() {
return stringify('job');
}
}
let { owner } = this;
owner.register('model:user', User);
owner.register('model:job', Job);

let job = store.modelFor('job');

let inverseForUser = job.inverseFor('user', store);
job.findInverseFor = function() {
Expand All @@ -158,6 +263,18 @@ module('integration/inverse_test - inverseFor', function(hooks) {
});

testInDebug('Errors out if you do not define an inverse for a reflexive relationship', function(assert) {
class ReflexiveModel extends Model {
@belongsTo('reflexive-model', { async: false })
reflexiveProp;

toString() {
return stringify('reflexiveModel');
}
}

let { owner } = this;
owner.register('model:reflexive-model', ReflexiveModel);

//Maybe store is evaluated lazily, so we need this :(
assert.expectWarning(() => {
var reflexiveModel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ module('integration/record_array_manager', function(hooks) {
assert.deepEqual(cars.toArray(), []);
assert.equal(arrayContentWillChangeCount, 0, 'expected NO arrayChangeEvents yet');

await store.push({
store.push({
data: [
{
type: 'car',
Expand All @@ -155,6 +155,7 @@ module('integration/record_array_manager', function(hooks) {
},
],
});
await settled();

assert.equal(arrayContentWillChangeCount, 1, 'expected ONE array change event');

Expand All @@ -173,7 +174,7 @@ module('integration/record_array_manager', function(hooks) {

arrayContentWillChangeCount = 0;

await store.push({
store.push({
data: [
{
type: 'car',
Expand All @@ -185,10 +186,11 @@ module('integration/record_array_manager', function(hooks) {
},
],
});
await settled();

assert.equal(arrayContentWillChangeCount, 0, 'expected NO array change events');

await store.push({
store.push({
data: [
{
type: 'car',
Expand All @@ -200,6 +202,7 @@ module('integration/record_array_manager', function(hooks) {
},
],
});
await settled();

assert.equal(arrayContentWillChangeCount, 1, 'expected ONE array change event');
// reset function so it doesn't execute after test finishes and store is torn down
Expand All @@ -222,7 +225,8 @@ module('integration/record_array_manager', function(hooks) {

let adapterPopulated = manager.createAdapterPopulatedRecordArray('car', query);

await adapterPopulated.destroy();
adapterPopulated.destroy();
await settled();

assert.equal(manager._adapterPopulatedRecordArrays.length, 0);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { run } from '@ember/runloop';
import { Promise } from 'rsvp';
import { setupStore, createStore } from 'dummy/tests/helpers/store';
import { settled } from '@ember/test-helpers';

import { module, test } from 'qunit';

Expand Down Expand Up @@ -151,8 +152,9 @@ module('integration/record-arrays/adapter_populated_record_array - DS.AdapterPop
);
});

test('recordArray.replace() throws error', function(assert) {
test('recordArray.replace() throws error', async function(assert) {
let recordArray = store.recordArrayManager.createAdapterPopulatedRecordArray('person', null);
await settled();

assert.throws(
() => {
Expand Down
2 changes: 1 addition & 1 deletion packages/-ember-data/tests/integration/snapshot-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ module('integration/snapshot - Snapshot', function(hooks) {
});

test('snapshot.belongsTo() returns a snapshot if relationship link has been fetched', async function(assert) {
assert.expect(2);
assert.expect(4);

store.adapterFor('application').findBelongsTo = function(store, snapshot, link, relationship) {
return resolve({ data: { id: 1, type: 'post', attributes: { title: 'Hello World' } } });
Expand Down

0 comments on commit f80b5ec

Please sign in to comment.