Skip to content

Commit

Permalink
some cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
runspired authored and pliljegr committed Jul 23, 2019
1 parent 6ae48ed commit 56ce4b7
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 64 deletions.
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
14 changes: 10 additions & 4 deletions packages/-ember-data/tests/integration/debug-adapter-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { A } from '@ember/array';
import { get } from '@ember/object';
import Model from '@ember-data/model';
import Adapter from '@ember-data/adapter';
import { DebugAdapter } from 'ember-data/-private';
import { module, test } from 'qunit';
import { settled } from '@ember/test-helpers';
import { attr } from '@ember-data/model';
Expand All @@ -15,24 +16,25 @@ class Post extends Model {
module('integration/debug-adapter - DS.DebugAdapter', function(hooks) {
setupTest(hooks);

let store, debugAdapter;
let store;

hooks.beforeEach(function() {
let { owner } = this;

owner.register('model:post', Post);
store = owner.lookup('service:store');
debugAdapter = owner.lookup('data-adapter:main');

debugAdapter.reopen({
let _adapter = DebugAdapter.extend({
getModelTypes() {
return A([{ klass: Post, name: 'post' }]);
},
});
owner.register('data-adapter:main', _adapter);
});

test('Watching Model Types', async function(assert) {
assert.expect(5);
let { owner } = this;
let debugAdapter = owner.lookup('data-adapter:main');

function added(types) {
assert.equal(types.length, 1, 'added one type');
Expand All @@ -59,6 +61,8 @@ module('integration/debug-adapter - DS.DebugAdapter', function(hooks) {
});

test('Watching Records', async function(assert) {
let { owner } = this;
let debugAdapter = owner.lookup('data-adapter:main');
let addedRecords, updatedRecords, removedIndex, removedCount;

this.owner.register(
Expand Down Expand Up @@ -164,6 +168,8 @@ module('integration/debug-adapter - DS.DebugAdapter', function(hooks) {
});

test('Column names', function(assert) {
let { owner } = this;
let debugAdapter = owner.lookup('data-adapter:main');
class Person extends Model {
@attr()
title;
Expand Down
187 changes: 150 additions & 37 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;

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

user.reopen({
previousJob: belongsTo('job', { async: false }),
});
@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,34 +141,114 @@ 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');
}
}

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

let user = store.modelFor('user');
assert.equal(user.inverseFor('job', store), null, 'There is no inverse');
});

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;

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

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

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

user.reopen({
job: belongsTo('job', { async: false }),
});
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 +259,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
Loading

0 comments on commit 56ce4b7

Please sign in to comment.