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

Add a test for embedded belongsTo create with id. #120

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