Skip to content

Commit

Permalink
Tests for destroyRecord in hasMany/belongsTo
Browse files Browse the repository at this point in the history
Side load vs links load

Co-authored-by: Stanley Stuart <[email protected]>
  • Loading branch information
jmar910 and fivetanley committed May 30, 2018
1 parent e3a24f6 commit 9c4ce4b
Show file tree
Hide file tree
Showing 2 changed files with 363 additions and 0 deletions.
203 changes: 203 additions & 0 deletions tests/integration/relationships/belongs-to-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,209 @@ test('Destroying a record with an unloaded aync belongsTo association does not f
run(post, 'destroyRecord');
});

test('destroying records in a belongsTo relationship that loaded via links', function(assert) {
assert.expect(2);

let Post = DS.Model.extend({
comments: DS.hasMany('comment', { async: true }),
});
Post.reopenClass({ toString: () => 'Post' });

let Comment = DS.Model.extend({
post: DS.belongsTo('post', { async: true }),
});
Comment.reopenClass({ toString: () => 'Comment' });
env = setupStore({
post: Post,
comment: Comment,
adapter: DS.RESTAdapter.extend({
shouldBackgroundReloadRecord: () => false,
})
});

env.registry.register(
'adapter:post',
DS.RESTAdapter.extend({
deleteRecord() {
return RSVP.resolve();
}
})
);

env.registry.register(
'adapter:comment',
DS.RESTAdapter.extend({
findBelongsTo() {
return {
post: {
id: '1',
type: 'post'
}
}
}
})
);

run(() => {
env.store.push({
data: [
{
type: 'comment',
id: '1',
relationships: {
post: {
links: {
related: '/comments/1/post'
}
}
}
},
{
type: 'comment',
id: '2',
relationships: {
post: {
links: {
related: '/comments/2/post'
}
}
}
},
{
type: 'comment',
id: '3',
relationships: {
post: {
links: {
related: '/comments/3/post'
}
}
}
}
]
});
});

return run(() => {
let comments = env.store.peekAll('comment').toArray();
let posts = RSVP.map(comments, (cc) => {
return cc.get('post');
});

return posts.then(posts => {
assert.deepEqual(Ember.A(posts).mapBy('id'), ['1', '1', '1']);
let post = env.store.peekRecord('post', '1');
return post.destroyRecord();
}).then(() => {
let comment = env.store.peekRecord('comment', '1');
assert.equal(comment.get('post.content'), null);
});
});
});

test('destroying records in a belongsTo relationship that loaded via sideloading', function(assert) {
assert.expect(2);

let Post = DS.Model.extend({
comments: DS.hasMany('comment', { async: true }),
});
Post.reopenClass({ toString: () => 'Post' });

let Comment = DS.Model.extend({
post: DS.belongsTo('post', { async: true }),
});
Comment.reopenClass({ toString: () => 'Comment' });
env = setupStore({
post: Post,
comment: Comment,
adapter: DS.RESTAdapter.extend()
});

env.registry.register(
'adapter:post',
DS.RESTAdapter.extend({
deleteRecord() {
return RSVP.resolve();
},
shouldBackgroundReloadRecord: () => false
})
);

env.registry.register(
'adapter:comment',
DS.RESTAdapter.extend({
findBelongsTo() {
return {
post: {
id: '1',
type: 'post'
}
}
}
})
);

run(() => {
env.store.push({
data: [
{
type: 'comment',
id: '1',
relationships: {
post: {
data: {
type: 'post', id: '1'
}
},
},
},
{
type: 'comment',
id: '2',
relationships: {
post: {
data: {
type: 'post', id: '1'
}
},
},
},
{
type: 'comment',
id: '3',
relationships: {
post: {
data: {
type: 'post', id: '1'
}
},
},
},
],
included: [
{ type: 'post', id: '1' }
]
});
});

return run(() => {
let comments = env.store.peekAll('comment').toArray();
let posts = RSVP.map(comments, (cc) => {
return cc.get('post');
});

return posts.then(posts => {
console.log('posts', posts);
assert.deepEqual(Ember.A(posts).mapBy('id'), ['1', '1', '1']);
let post = env.store.peekRecord('post', '1');
return post.destroyRecord();
}).then(() => {
let comment = env.store.peekRecord('comment', '1');
assert.equal(comment.get('post.content'), null);
});
});
});

testInDebug('A sync belongsTo errors out if the record is unlaoded', function(assert) {
let message;
run(() => {
Expand Down
160 changes: 160 additions & 0 deletions tests/integration/relationships/has-many-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2906,6 +2906,166 @@ test('adding and removing records from hasMany relationship #2666', function(ass
});
});

test('destroying records in a hasMany relationship that loaded via links', function(assert) {
assert.expect(2);

let Post = DS.Model.extend({
comments: DS.hasMany('comment', { async: true }),
});
Post.reopenClass({ toString: () => 'Post' });

let Comment = DS.Model.extend({
post: DS.belongsTo('post', { async: true }),
});
Comment.reopenClass({ toString: () => 'Comment' });
env = setupStore({
post: Post,
comment: Comment,
adapter: DS.RESTAdapter.extend({
shouldBackgroundReloadRecord: () => false,
findHasMany: () => {
return {
comments: [
{
type: 'comment',
id: '1'
},
{
type: 'comment',
id: '2'
},
{
type: 'comment',
id: '3'
}
]
};
}
})
});

env.registry.register(
'adapter:comment',
DS.RESTAdapter.extend({
deleteRecord(record) {
return resolve();
}
})
);

run(() => {
env.store.push({
data: [
{
type: 'post',
id: '1',
relationships: {
comments: {
links: {
related: '/comments'
}
}
}
}
]
});
});

return run(() => {
return env.store.findRecord('post', 1).then(post => {
return post.get('comments').then((comments) => {
assert.equal(comments.get('length'), 3, 'Initial comments count');

return comments.get('lastObject').destroyRecord()
.then(() => {
let comments = post.get('comments');
let length = comments.get('length');

assert.equal(length, 2, 'Comments count after destroy');
});
});
});
});
});

test('destroying records in a hasMany relationship that loaded via sideloading', function(assert) {
assert.expect(2);

let Post = DS.Model.extend({
comments: DS.hasMany('comment', { async: true }),
});
Post.reopenClass({ toString: () => 'Post' });

let Comment = DS.Model.extend({
post: DS.belongsTo('post', { async: true }),
});
Comment.reopenClass({ toString: () => 'Comment' });
env = setupStore({
post: Post,
comment: Comment,
adapter: DS.RESTAdapter.extend({
shouldBackgroundReloadRecord: () => false
})
});

env.registry.register(
'adapter:comment',
DS.RESTAdapter.extend({
deleteRecord(record) {
return resolve();
}
})
);

run(() => {
env.store.push({
data: [
{
type: 'post',
id: '1',
relationships: {
comments: {
data: [
{ type: 'comment', id: '1' },
{ type: 'comment', id: '2' },
{ type: 'comment', id: '3' },
],
},
},
},
{
type: 'comment',
id: '1',
},
{
type: 'comment',
id: '2',
},
{
type: 'comment',
id: '3',
},
],
});
});

return run(() => {
return env.store.findRecord('post', 1).then(post => {
return post.get('comments').then((comments) => {
assert.equal(comments.get('length'), 3, 'Initial comments count');

return comments.get('lastObject').destroyRecord()
.then(() => {
let comments = post.get('comments');
let length = comments.get('length');

assert.equal(length, 2, 'Comments count after destroy');
});
});
});
});
});

test('hasMany hasAnyRelationshipData async loaded', function(assert) {
assert.expect(1);

Expand Down

0 comments on commit 9c4ce4b

Please sign in to comment.