Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure snapshot.belongsTo() and hasMany() do not return deleted records #3170

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions packages/ember-data/lib/system/snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ Snapshot.prototype = {
inverseRecord = get(relationship, 'inverseRecord');

if (hasData) {
if (inverseRecord) {
if (inverseRecord && !inverseRecord.isDeleted()) {
if (id) {
result = get(inverseRecord, 'id');
} else {
Expand Down Expand Up @@ -305,10 +305,12 @@ Snapshot.prototype = {
if (hasData) {
results = [];
members.forEach(function(member) {
if (ids) {
results.push(member.id);
} else {
results.push(member.createSnapshot());
if (!member.isDeleted()) {
if (ids) {
results.push(member.id);
} else {
results.push(member.createSnapshot());
}
}
});
}
Expand Down
72 changes: 71 additions & 1 deletion packages/ember-data/tests/integration/snapshot-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,22 @@ test("snapshot.belongsTo() returns a snapshot if relationship is set", function(
});
});

test("snapshot.belongsTo() returns null if relationship is deleted", function() {
expect(1);

run(function() {
var post = env.store.push('post', { id: 1, title: 'Hello World' });
var comment = env.store.push('comment', { id: 2, body: 'This is comment', post: 1 });

post.deleteRecord();

var snapshot = comment._createSnapshot();
var relationship = snapshot.belongsTo('post');

equal(relationship, null, 'relationship unset after deleted');
});
});

test("snapshot.belongsTo() returns undefined if relationship is a link", function() {
expect(1);

Expand Down Expand Up @@ -246,7 +262,7 @@ test("snapshot.belongsTo() and snapshot.hasMany() returns correctly when setting
});
});

test("snapshot.hasMany() returns ID if option.id is set", function() {
test("snapshot.belongsTo() returns ID if option.id is set", function() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test was incorrectly named

expect(1);

run(function() {
Expand All @@ -259,6 +275,22 @@ test("snapshot.hasMany() returns ID if option.id is set", function() {
});
});

test("snapshot.belongsTo() returns null if option.id is set but relationship was deleted", function() {
expect(1);

run(function() {
var post = env.store.push('post', { id: 1, title: 'Hello World' });
var comment = env.store.push('comment', { id: 2, body: 'This is comment', post: 1 });

post.deleteRecord();

var snapshot = comment._createSnapshot();
var relationship = snapshot.belongsTo('post', { id: true });

equal(relationship, null, 'relationship unset after deleted');
});
});

test("snapshot.hasMany() returns undefined if relationship is undefined", function() {
expect(1);

Expand Down Expand Up @@ -306,6 +338,25 @@ test("snapshot.hasMany() returns array of snapshots if relationship is set", fun
});
});

test("snapshot.hasMany() returns empty array if relationship records are deleted", function() {
expect(2);

run(function() {
var comment1 = env.store.push('comment', { id: 1, body: 'This is the first comment' });
var comment2 = env.store.push('comment', { id: 2, body: 'This is the second comment' });
var post = env.store.push('post', { id: 3, title: 'Hello World', comments: [1, 2] });

comment1.deleteRecord();
comment2.deleteRecord();

var snapshot = post._createSnapshot();
var relationship = snapshot.hasMany('comments');

ok(relationship instanceof Array, 'relationship is an instance of Array');
equal(relationship.length, 0, 'relationship is empty');
});
});

test("snapshot.hasMany() returns array of IDs if option.ids is set", function() {
expect(1);

Expand All @@ -318,6 +369,25 @@ test("snapshot.hasMany() returns array of IDs if option.ids is set", function()
});
});

test("snapshot.hasMany() returns empty array of IDs if option.ids is set but relationship records were deleted", function() {
expect(2);

run(function() {
var comment1 = env.store.push('comment', { id: 1, body: 'This is the first comment' });
var comment2 = env.store.push('comment', { id: 2, body: 'This is the second comment' });
var post = env.store.push('post', { id: 3, title: 'Hello World', comments: [1, 1] });

comment1.deleteRecord();
comment2.deleteRecord();

var snapshot = post._createSnapshot();
var relationship = snapshot.hasMany('comments', { ids: true });

ok(relationship instanceof Array, 'relationship is an instance of Array');
equal(relationship.length, 0, 'relationship is empty');
});
});

test("snapshot.hasMany() returns undefined if relationship is a link", function() {
expect(1);

Expand Down