var attr = Ember.attr;

module("Ember.HasManyArray - non embedded objects saving");

test("new records should remain after parent is saved", function() {
  expect(3);
  var json = {
    id: 1,
    title: 'foo',
    comment_ids: []
  };

  var Comment = Ember.Model.extend({
    id: attr(),
    text: attr()
  });
  Comment.adapter = Ember.RESTAdapter.create();
  Comment.url = '/comments';

  var Article = Ember.Model.extend({
    id: attr(),
    title: attr(),
    comments: Ember.hasMany(Comment, { key: 'comment_ids' })
  });
  Article.adapter = Ember.RESTAdapter.create();
  Article.url = '/articles';
  Article.adapter._ajax = function() {
    return new Ember.RSVP.Promise(function(resolve) {
        resolve(json);
    });
  };
  
  var article = Article.create({
    title: 'bar'
  });

  var comment = Comment.create({
    text: 'comment text'
  });

  article.get('comments').addObject(comment);

  var promise = Ember.run(article, article.save);
  promise.then(function(record) {
    start();
    ok(record.get('comments.firstObject') === comment, "Comment is the same object");
    equal(record.get('comments.length'), 1, "Article should still have one comment after save");
    equal(record.get('comments.firstObject.text'), comment.get('text'), 'Comment is the same');
  });
  stop();
});
@@ -1296,8 +1296,1010 @@
 p();%0A%7D);
+%0A%0Atest(%22saving child objects%22, function() %7B%0A  expect(1);%0A%0A  var commentJSON = %7B%0A    id: 2,%0A    title: 'bar',%0A  %7D;%0A%0A  var Comment = Ember.Model.extend(%7B%0A    id: attr(),%0A    text: attr()%0A  %7D);%0A  Comment.adapter = Ember.RESTAdapter.create();%0A  Comment.url = '/comments';%0A  Comment.adapter._ajax = function() %7B%0A    return new Ember.RSVP.Promise(function(resolve) %7B%0A        resolve(commentJSON);%0A    %7D);%0A  %7D;%0A%0A  var Article = Ember.Model.extend(%7B%0A    id: attr(),%0A    title: attr(),%0A    comments: Ember.hasMany(Comment, %7B key: 'comment_ids' %7D)%0A  %7D);%0A  Article.adapter = Ember.RESTAdapter.create();%0A  Article.url = '/articles';%0A  %0A  var article = Article.create();%0A  var comment = Comment.create();%0A%0A  var comments = article.get(%22comments%22);%0A  comments.addObject(comment);%0A%0A  var promise = Ember.run(comments, comments.save);%0A  promise.then(function(records) %7B%0A    start();%0A    var comment = records.get(%22firstObject%22);%0A    equal(comment.get(%22id%22), 2, %22Data from the response is loaded%22);%0A  %7D);%0A  stop();%0A%7D);%0A

add test for saving has many relationship