Skip to content

Commit

Permalink
CHORE: Modernize integration/ tests and remove run loop calls
Browse files Browse the repository at this point in the history
  • Loading branch information
pliljegr committed Jul 23, 2019
1 parent e356cce commit bf7611d
Show file tree
Hide file tree
Showing 7 changed files with 1,000 additions and 1,047 deletions.
12 changes: 3 additions & 9 deletions packages/-ember-data/tests/integration/application-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import Namespace from '@ember/application/namespace';
import Service, { inject as service } from '@ember/service';
import Controller from '@ember/controller';
import Application from '@ember/application';
import { run } from '@ember/runloop';
import Store from 'ember-data/store';
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
Expand Down Expand Up @@ -110,12 +109,6 @@ module('integration/application - Attaching initializer', function(hooks) {
this.owner = null;
});

hooks.afterEach(function() {
if (this.application !== null) {
run(this.application, 'destroy');
}
});

test('ember-data initializer is run', async function(assert) {
let ran = false;

Expand All @@ -129,7 +122,7 @@ module('integration/application - Attaching initializer', function(hooks) {

this.application = this.TestApplication.create({ autoboot: false });

await run(() => this.application.boot());
await this.application.boot();

assert.ok(ran, 'ember-data initializer was found');
});
Expand All @@ -149,7 +142,8 @@ module('integration/application - Attaching initializer', function(hooks) {

this.application = this.TestApplication.create({ autoboot: false });

await run(() => this.application.boot().then(() => (this.owner = this.application.buildInstance())));
await this.application.boot();
this.owner = this.application.buildInstance();

let store = this.owner.lookup('service:store');
assert.ok(
Expand Down
3 changes: 1 addition & 2 deletions packages/-ember-data/tests/integration/debug-adapter-test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { setupTest } from 'ember-qunit';
import { A } from '@ember/array';
import { get } from '@ember/object';
import { run } from '@ember/runloop';
import Model from '@ember-data/model';
import Adapter from '@ember-data/adapter';
import { module, test } from 'qunit';
Expand Down Expand Up @@ -156,7 +155,7 @@ module('integration/debug-adapter - DS.DebugAdapter', function(hooks) {
addedRecords = updatedRecords = [];
removedCount = removedIndex = null;

run(() => post.unloadRecord());
post.unloadRecord();

await settled();

Expand Down
149 changes: 78 additions & 71 deletions packages/-ember-data/tests/integration/inverse-test.js
Original file line number Diff line number Diff line change
@@ -1,68 +1,77 @@
import { run } from '@ember/runloop';
import setupStore from 'dummy/tests/helpers/store';

import { setupTest } from 'ember-qunit';
import testInDebug from 'dummy/tests/helpers/test-in-debug';
import { module, test } from 'qunit';

import DS from 'ember-data';

let env, store, User, Job, ReflexiveModel;

const { attr, belongsTo } = DS;
import Model from '@ember-data/model';
import { attr, belongsTo } from '@ember-data/model';

function stringify(string) {
return function() {
return string;
};
}

// @pete_the_pete refer to https://github.com/emberjs/data/commit/e34cb5a37391ce2e5d25401ca189a9bd19a29340
module('integration/inverse_test - inverseFor', function(hooks) {
hooks.beforeEach(function() {
User = DS.Model.extend({
name: attr('string'),
bestFriend: belongsTo('user', { async: true, inverse: null }),
job: belongsTo('job', { async: false }),
});

User.toString = stringify('user');

Job = DS.Model.extend({
isGood: attr(),
user: belongsTo('user', { async: false }),
});

Job.toString = stringify('job');
setupTest(hooks);
let store;
let user;
let job;

ReflexiveModel = DS.Model.extend({
reflexiveProp: belongsTo('reflexive-model', { async: false }),
});

ReflexiveModel.toString = stringify('reflexiveModel');

env = setupStore({
user: User,
job: Job,
reflexiveModel: ReflexiveModel,
});

store = env.store;

Job = store.modelFor('job');
User = store.modelFor('user');
ReflexiveModel = store.modelFor('reflexive-model');
});

hooks.afterEach(function() {
run(env.container, 'destroy');
hooks.beforeEach(function() {
let { owner } = this;
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');
}
}

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

toString() {
return stringify('reflexiveModel');
}
}
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 inverseDefinition = Job.inverseFor('user', store);
let inverseDefinition = job.inverseFor('user', store);

assert.deepEqual(
inverseDefinition,
{
type: User,
type: user,
name: 'job',
kind: 'belongsTo',
options: {
Expand All @@ -74,18 +83,18 @@ module('integration/inverse_test - inverseFor', function(hooks) {
});

test('Finds the inverse when only one side has defined it manually', function(assert) {
Job.reopen({
job.reopen({
owner: belongsTo('user', { inverse: 'previousJob', async: false }),
});

User.reopen({
user.reopen({
previousJob: belongsTo('job', { async: false }),
});

assert.deepEqual(
Job.inverseFor('owner', store),
job.inverseFor('owner', store),
{
type: User, //the model's type
type: user, //the model's type
name: 'previousJob', //the models relationship key
kind: 'belongsTo',
options: {
Expand All @@ -96,9 +105,9 @@ module('integration/inverse_test - inverseFor', function(hooks) {
);

assert.deepEqual(
User.inverseFor('previousJob', store),
user.inverseFor('previousJob', store),
{
type: Job, //the model's type
type: job, //the model's type
name: 'owner', //the models relationship key
kind: 'belongsTo',
options: {
Expand All @@ -111,57 +120,55 @@ 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({
job.reopen({
user: belongsTo('user', { inverse: 'previousJob', async: false }),
});

User.reopen({
user.reopen({
job: belongsTo('job', { async: false }),
});

assert.equal(User.inverseFor('job', store), null, 'There is no inverse');
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({
job.reopen({
user: belongsTo('user', { inverse: 'job', async: false }),
owner: belongsTo('user', { inverse: 'job', async: false }),
});

User.reopen({
user.reopen({
job: belongsTo('job', { async: false }),
});

assert.expectAssertion(() => {
User.inverseFor('job', store);
}, /You defined the 'job' relationship on user, but you defined the inverse relationships of type job multiple times/i);
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);

var inverseForUser = Job.inverseFor('user', store);
Job.findInverseFor = function() {
let inverseForUser = job.inverseFor('user', store);
job.findInverseFor = function() {
assert.ok(false, 'Find is not called anymore');
};

assert.equal(inverseForUser, Job.inverseFor('user', store), 'Inverse cached succesfully');
assert.equal(inverseForUser, job.inverseFor('user', store), 'Inverse cached succesfully');
});

testInDebug('Errors out if you do not define an inverse for a reflexive relationship', function(assert) {
//Maybe store is evaluated lazily, so we need this :(
assert.expectWarning(() => {
var reflexiveModel;
run(() => {
store.push({
data: {
type: 'reflexive-model',
id: '1',
},
});
reflexiveModel = store.peekRecord('reflexive-model', 1);
reflexiveModel.get('reflexiveProp');
store.push({
data: {
type: 'reflexive-model',
id: '1',
},
});
reflexiveModel = store.peekRecord('reflexive-model', 1);
reflexiveModel.get('reflexiveProp');
}, /Detected a reflexive relationship by the name of 'reflexiveProp'/);
});
});
47 changes: 22 additions & 25 deletions packages/-ember-data/tests/integration/lifecycle-hooks-test.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
import { resolve } from 'rsvp';
import { run } from '@ember/runloop';
import { setupTest } from 'ember-qunit';
import { deprecatedTest } from 'dummy/tests/helpers/deprecated-test';
import setupStore from 'dummy/tests/helpers/store';
import Model from '@ember-data/model';
import { attr } from '@ember-data/model';

import { module } from 'qunit';

import DS from 'ember-data';

let Person, env;
const { attr } = DS;

module('integration/lifecycle_hooks - Lifecycle Hooks', function(hooks) {
setupTest(hooks);
let store;
let adapter;
hooks.beforeEach(function() {
Person = DS.Model.extend({
name: attr('string'),
});

env = setupStore({
person: Person,
});
});
let { owner } = this;
class Person extends Model {
@attr()
name;
}

hooks.afterEach(function() {
run(env.container, 'destroy');
owner.register('model:person', Person);
store = owner.lookup('service:store');
adapter = store.adapterFor('application');
});

deprecatedTest(
Expand All @@ -31,15 +28,15 @@ module('integration/lifecycle_hooks - Lifecycle Hooks', function(hooks) {
id: 'ember-data:evented-api-usage',
until: '4.0',
},
function(assert) {
async function(assert) {
let done = assert.async();
assert.expect(3);

env.adapter.createRecord = function(store, type, snapshot) {
adapter.createRecord = function(store, type, snapshot) {
return resolve({ data: { id: 99, type: 'person', attributes: { name: 'Yehuda Katz' } } });
};

let person = env.store.createRecord('person', { name: 'Yehuda Katz' });
let person = store.createRecord('person', { name: 'Yehuda Katz' });

person.on('didCreate', function() {
assert.equal(this, person, 'this is bound to the record');
Expand All @@ -48,7 +45,7 @@ module('integration/lifecycle_hooks - Lifecycle Hooks', function(hooks) {
done();
});

run(person, 'save');
await person.save();
}
);

Expand All @@ -58,22 +55,22 @@ module('integration/lifecycle_hooks - Lifecycle Hooks', function(hooks) {
id: 'ember-data:evented-api-usage',
until: '4.0',
},
function(assert) {
async function(assert) {
assert.expect(3);

env.adapter.createRecord = function(store, type, snapshot) {
adapter.createRecord = function(store, type, snapshot) {
return resolve();
};

let person = env.store.createRecord('person', { id: 99, name: 'Yehuda Katz' });
let person = store.createRecord('person', { id: 99, name: 'Yehuda Katz' });

person.on('didCreate', function() {
assert.equal(this, person, 'this is bound to the record');
assert.equal(this.get('id'), '99', 'the ID has been assigned');
assert.equal(this.get('name'), 'Yehuda Katz', 'the attribute has been assigned');
});

run(person, 'save');
await person.save();
}
);
});
Loading

0 comments on commit bf7611d

Please sign in to comment.