Skip to content

Commit

Permalink
[CHORE] RFC-0329 - adding deprecation about using evented in ember-data
Browse files Browse the repository at this point in the history
  • Loading branch information
pete committed May 17, 2019
1 parent a9e1efb commit 85461de
Show file tree
Hide file tree
Showing 12 changed files with 416 additions and 168 deletions.
44 changes: 44 additions & 0 deletions packages/-ember-data/tests/helpers/deprecated-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { test } from 'qunit';
import VERSION from 'ember-data/version';

// small comparison function for major and minor semver values
function gte(EDVersion, DeprecationVersion) {
let _edv = EDVersion.split('.');
let _depv = DeprecationVersion.split('.');
// compare major
let major = +_edv[0] >= +_depv[0];
// compare minor
let minor = +_edv[1] >= +_depv[1];
return major || minor;
}

export function deprecatedTest(testName, deprecation, testCallback) {
// '4.0'
if (typeof deprecation.until !== 'string' || deprecation.until.length < 3) {
throw new Error(`deprecatedTest expects { until } to be a version.`);
}
// 'ds.<some-name>'
if (typeof deprecation.id !== 'string' || deprecation.id.length < 8) {
throw new Error(`deprecatedTest expects { id } to be a meaningful string`);
}

if (gte(VERSION, deprecation.until)) {
test(`DEPRECATION ${deprecation.id} until ${deprecation.until} | ${testName}`, testCallback);
} else {
test(`DEPRECATION ${deprecation.id} until ${
deprecation.until
} | ${testName}`, function(assert) {
if (deprecation.refactor === true) {
assert.ok(
false,
'This test includes use of a deprecated feature that should now be refactored.'
);
} else {
assert.ok(
false,
'This test is for a deprecated feature whose time has come and should be removed'
);
}
});
}
}
91 changes: 53 additions & 38 deletions packages/-ember-data/tests/integration/lifecycle-hooks-test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { resolve } from 'rsvp';
import { run } from '@ember/runloop';
import { deprecatedTest } from 'dummy/tests/helpers/deprecated-test';
import setupStore from 'dummy/tests/helpers/store';

import { module, test } from 'qunit';
import { module } from 'qunit';

import DS from 'ember-data';

Expand All @@ -24,41 +25,55 @@ module('integration/lifecycle_hooks - Lifecycle Hooks', function(hooks) {
run(env.container, 'destroy');
});

test('When the adapter acknowledges that a record has been created, a `didCreate` event is triggered.', function(assert) {
let done = assert.async();
assert.expect(3);

env.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' });

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');
done();
});

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

test('When the adapter acknowledges that a record has been created without a new data payload, a `didCreate` event is triggered.', function(assert) {
assert.expect(3);

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

let person = env.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');
});
deprecatedTest(
'When the adapter acknowledges that a record has been created, a `didCreate` event is triggered.',
{
id: 'ember-evented',
until: '3.12',
},
function(assert) {
let done = assert.async();
assert.expect(3);

env.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' });

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');
done();
});

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

deprecatedTest(
'When the adapter acknowledges that a record has been created without a new data payload, a `didCreate` event is triggered.',
{
id: 'ember-evented',
until: '3.12',
},
function(assert) {
assert.expect(3);

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

let person = env.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');
}
);
});
106 changes: 57 additions & 49 deletions packages/-ember-data/tests/integration/relationships/has-many-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { get } from '@ember/object';
import { run } from '@ember/runloop';
import setupStore from 'dummy/tests/helpers/store';
import testInDebug from 'dummy/tests/helpers/test-in-debug';
import { deprecatedTest } from 'dummy/tests/helpers/deprecated-test';
import { module, test, skip } from 'qunit';
import { relationshipStateFor, relationshipsFor } from 'ember-data/-private';
import DS from 'ember-data';
Expand Down Expand Up @@ -1266,71 +1267,78 @@ module('integration/relationships/has_many - Has-Many Relationships', function(h
});
});

test('PromiseArray proxies evented methods to its ManyArray', function(assert) {
assert.expect(6);

Post.reopen({
comments: DS.hasMany('comment', { async: true }),
});
deprecatedTest(
'PromiseArray proxies evented methods to its ManyArray',
{
id: 'ember-evented',
until: '3.12',
},
function(assert) {
assert.expect(6);

env.adapter.findHasMany = function(store, snapshot, link, relationship) {
return resolve({
data: [
{ id: 1, type: 'comment', attributes: { body: 'First' } },
{ id: 2, type: 'comment', attributes: { body: 'Second' } },
],
Post.reopen({
comments: DS.hasMany('comment', { async: true }),
});
};
let post, comments;

run(function() {
env.store.push({
data: {
type: 'post',
id: '1',
relationships: {
comments: {
links: {
related: 'someLink',
env.adapter.findHasMany = function(store, snapshot, link, relationship) {
return resolve({
data: [
{ id: 1, type: 'comment', attributes: { body: 'First' } },
{ id: 2, type: 'comment', attributes: { body: 'Second' } },
],
});
};
let post, comments;

run(function() {
env.store.push({
data: {
type: 'post',
id: '1',
relationships: {
comments: {
links: {
related: 'someLink',
},
},
},
},
},
});
post = env.store.peekRecord('post', 1);
comments = post.get('comments');
});
post = env.store.peekRecord('post', 1);
comments = post.get('comments');
});

comments.on('on-event', function() {
assert.ok(true);
});
comments.on('on-event', function() {
assert.ok(true);
});

run(function() {
comments.trigger('on-event');
});
run(function() {
comments.trigger('on-event');
});

assert.equal(comments.has('on-event'), true);
const cb = function() {
assert.ok(false, 'We should not trigger this event');
};
assert.equal(comments.has('on-event'), true);
const cb = function() {
assert.ok(false, 'We should not trigger this event');
};

comments.on('off-event', cb);
comments.off('off-event', cb);
comments.on('off-event', cb);
comments.off('off-event', cb);

assert.equal(comments.has('off-event'), false);
assert.equal(comments.has('off-event'), false);

comments.one('one-event', function() {
assert.ok(true);
});
comments.one('one-event', function() {
assert.ok(true);
});

assert.equal(comments.has('one-event'), true);
assert.equal(comments.has('one-event'), true);

run(function() {
comments.trigger('one-event');
});
run(function() {
comments.trigger('one-event');
});

assert.equal(comments.has('one-event'), false);
});
assert.equal(comments.has('one-event'), false);
}
);

test('An updated `links` value should invalidate a relationship cache', function(assert) {
assert.expect(8);
Expand Down
Loading

0 comments on commit 85461de

Please sign in to comment.