ebryn / ember-model

A lightweight model library for Ember.js
MIT License
524 stars 159 forks source link

Failing test to demonstrate removing a record in an embedded has many #358

Closed scottmessinger closed 10 years ago

scottmessinger commented 10 years ago

Pull request with failing test for ebryn/ember-model#357

danii1 commented 10 years ago

This test case doesn't seem right, _dirtyAttributes is used to notify parent about state of it's child attributes and relationships, so although you clearing _dirtyAttributes, "comments" relationship is still in dirty state after you added new comment, removing new comment just sets comments to clean state and it will notify parent object that relationship became clean - _dirtyAttributes should be clean too, that's what happend in test case.

I modified test to check scenario in #357, and it's passing:

test("removing a record from the many array that was added after the initial", function() {
  var json = {
    id: 1,
    title: 'foo',
    comments: [
      {id: 1, text: 'uno'},
      {id: 2, text: 'dos'},
      {id: 3, text: 'tres'}
    ]
  };

  var Comment = Ember.Model.extend({
    text: attr()
  });

  var Article = Ember.Model.extend({
    title: attr(),
    comments: Ember.hasMany(Comment, { key: 'comments', embedded: true })
  });

  Article.adapter = Ember.FixtureAdapter.create();

  var article = Article.create();
  Ember.run(article, article.load, json.id, json);

  var comments = article.get('comments'),
      newComment = Comment.create({id: 4, text: "quatro"});

  article.get('comments').pushObject(newComment);

  equal(article.get('isDirty'), true);

  stop();
  Ember.run(function() {
    article.save().then(function() {
      start();
      equal(article.get('isDirty'), false, 'article should be clean after save');
      comments.removeObject(newComment);
      equal(article.get('comments.length'), 3, "removing an object should succeed");
      equal(article.get('isDirty'), true, "removing an object in an embedded has many array should dirty the model");

      article.get('comments').pushObject(newComment);
      equal(article.get('isDirty'), false, "article should become clean again after pushing back comment");
    });
  });
});
scottmessinger commented 10 years ago

@danii1 Thanks so much for taking the time to look at that. What you did makes sense -- I'll have to take a closer to look to see why it works. Closes #357