Skip to content

Commit

Permalink
Add a test for embedded belongsTo create with id.
Browse files Browse the repository at this point in the history
  • Loading branch information
benkonrath committed Jul 17, 2015
1 parent 7bb7d3c commit 60095cc
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 19 deletions.
76 changes: 60 additions & 16 deletions tests/acceptance/embedded-records-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ var embeddedPostComments = [
}
];

var posts = [
{
id: 2,
post_title: 'post title 2',
body: 'post body 2',
comments: []
}
];

module('Acceptance: Embedded Records', {
beforeEach: function() {
application = startApp();
Expand All @@ -52,13 +61,25 @@ module('Acceptance: Embedded Records', {

server = new Pretender(function() {

this.get('/test-api/embedded-comments-posts/:id/', function(request) {
return [200, {'Content-Type': 'application/json'}, JSON.stringify(embeddedCommentsPosts[request.params.id - 1])];
this.get('/test-api/embedded-comments-posts/1/', function( ) {
return [200, {'Content-Type': 'application/json'}, JSON.stringify(embeddedCommentsPosts[0])];
});

this.get('/test-api/embedded-post-comments/1/', function() {
return [200, {'Content-Type': 'application/json'}, JSON.stringify(embeddedPostComments[0])];
});

this.get('/test-api/embedded-post-comments/:id/', function(request) {
return [200, {'Content-Type': 'application/json'}, JSON.stringify(embeddedPostComments[request.params.id - 1])];
this.get('/test-api/posts/2/', function() {
return [200, {'Content-Type': 'application/json'}, JSON.stringify(posts[0])];
});

this.post('/test-api/embedded-post-comments/', function(request) {
let data = Ember.$.parseJSON(request.requestBody);
data['id'] = 2;
data['post'] = posts[0];
return [201, {'Content-Type': 'application/json'}, JSON.stringify(data)];
});

});
},

Expand All @@ -68,23 +89,20 @@ module('Acceptance: Embedded Records', {
}
});

test('belongsTo', function(assert) {
test('belongsTo retrieve', function(assert) {
assert.expect(2);

return Ember.run(function() {

return store.findRecord('embedded-post-comment', 1).then(function(comment) {

assert.ok(comment);

return comment.get('post').then(function(post) {
assert.ok(post);
});
assert.ok(comment.get('post'));
});
});
});

test('hasMany', function(assert) {
test('hasMany retrieve', function(assert) {
assert.expect(6);

return Ember.run(function() {
Expand All @@ -93,12 +111,38 @@ test('hasMany', function(assert) {

assert.ok(post);

return post.get('comments').then(function(comments) {
assert.ok(comments);
assert.equal(comments.get('length'), 3);
assert.ok(comments.objectAt(0));
assert.ok(comments.objectAt(1));
assert.ok(comments.objectAt(2));
let comments = post.get('comments');
assert.ok(comments);
assert.equal(comments.get('length'), 3);
assert.ok(comments.objectAt(0));
assert.ok(comments.objectAt(1));
assert.ok(comments.objectAt(2));
});
});
});

test('belongsTo create', function(assert) {
assert.expect(5);

return Ember.run(function() {

return store.findRecord('post', 2).then(function(post) {

let comment = store.createRecord('embedded-post-comment', {
body: 'comment body 2',
post: post
});

return comment.save().then(function(comment) {

assert.ok(comment);
assert.ok(comment.get('id'));
assert.equal(comment.get('body'), 'comment body 2');
assert.ok(comment.get('post'));

let requestBody = (JSON.parse(server.handledRequests.pop().requestBody));
assert.equal(requestBody.post, 2);

});
});
});
Expand Down
2 changes: 1 addition & 1 deletion tests/dummy/app/models/embedded-comments-post.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ import DS from 'ember-data';
export default DS.Model.extend({
postTitle: DS.attr(),
body: DS.attr(),
comments: DS.hasMany('comment', {async: true})
comments: DS.hasMany('comment', {async: false})
});
2 changes: 1 addition & 1 deletion tests/dummy/app/models/embedded-post-comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ import DS from 'ember-data';

export default DS.Model.extend({
body: DS.attr(),
post: DS.belongsTo('post', {async: true})
post: DS.belongsTo('post', {async: false})
});
2 changes: 1 addition & 1 deletion tests/dummy/app/serializers/embedded-post-comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import DS from 'ember-data';

export default DRFSerializer.extend(DS.EmbeddedRecordsMixin, {
attrs: {
post: {embedded: 'always'}
post: {serialize: 'id', deserialize: 'records'}
}
});

0 comments on commit 60095cc

Please sign in to comment.