PageRenderTime 36ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/test/model.js

https://github.com/olleolleolle/backbone
JavaScript | 133 lines | 112 code | 19 blank | 2 comment | 4 complexity | 18693efc5b4baf111bccbe4a6e911c3d MD5 | raw file
  1. $(document).ready(function() {
  2. module("Backbone.Model");
  3. // Variable to catch the last request.
  4. window.lastRequest = null;
  5. // Stub out Backbone.request...
  6. Backbone.sync = function() {
  7. lastRequest = _.toArray(arguments);
  8. };
  9. var attrs = {
  10. id : '1-the-tempest',
  11. title : "The Tempest",
  12. author : "Bill Shakespeare",
  13. length : 123
  14. };
  15. var doc = new Backbone.Model(attrs);
  16. var klass = Backbone.Collection.extend({
  17. url : function() { return '/collection'; }
  18. });
  19. var collection = new klass();
  20. collection.add(doc);
  21. test("Model: initialize", function() {
  22. var Model = Backbone.Model.extend({
  23. initialize: function() {
  24. this.one = 1;
  25. }
  26. });
  27. var model = new Model;
  28. equals(model.one, 1);
  29. });
  30. test("Model: url", function() {
  31. equals(doc.url(), '/collection/1-the-tempest');
  32. });
  33. test("Model: clone", function() {
  34. attrs = { 'foo': 1, 'bar': 2, 'baz': 3};
  35. a = new Backbone.Model(attrs);
  36. b = a.clone();
  37. equals(a.get('foo'), 1);
  38. equals(a.get('bar'), 2);
  39. equals(a.get('baz'), 3);
  40. equals(b.get('foo'), a.get('foo'), "Foo should be the same on the clone.");
  41. equals(b.get('bar'), a.get('bar'), "Bar should be the same on the clone.");
  42. equals(b.get('baz'), a.get('baz'), "Baz should be the same on the clone.");
  43. a.set({foo : 100});
  44. equals(a.get('foo'), 100);
  45. equals(b.get('foo'), 1, "Changing a parent attribute does not change the clone.");
  46. });
  47. test("Model: isNew", function() {
  48. attrs = { 'foo': 1, 'bar': 2, 'baz': 3};
  49. a = new Backbone.Model(attrs);
  50. ok(a.isNew(), "it should be new");
  51. attrs = { 'foo': 1, 'bar': 2, 'baz': 3, 'id': -5 };
  52. ok(a.isNew(), "any defined ID is legal, negative or positive");
  53. });
  54. test("Model: get", function() {
  55. equals(doc.get('title'), 'The Tempest');
  56. equals(doc.get('author'), 'Bill Shakespeare');
  57. });
  58. test("Model: set and unset", function() {
  59. attrs = { 'foo': 1, 'bar': 2, 'baz': 3};
  60. a = new Backbone.Model(attrs);
  61. var changeCount = 0;
  62. a.bind("change:foo", function() { changeCount += 1; });
  63. a.set({'foo': 2});
  64. ok(a.get('foo')== 2, "Foo should have changed.");
  65. ok(changeCount == 1, "Change count should have incremented.");
  66. a.set({'foo': 2}); // set with value that is not new shouldn't fire change event
  67. ok(a.get('foo')== 2, "Foo should NOT have changed, still 2");
  68. ok(changeCount == 1, "Change count should NOT have incremented.");
  69. a.unset('foo');
  70. ok(a.get('foo')== null, "Foo should have changed");
  71. ok(changeCount == 2, "Change count should have incremented for unset.");
  72. });
  73. test("Model: changed, hasChanged, changedAttributes, previous, previousAttributes", function() {
  74. var model = new Backbone.Model({name : "Tim", age : 10});
  75. model.bind('change', function() {
  76. ok(model.hasChanged('name'), 'name changed');
  77. ok(!model.hasChanged('age'), 'age did not');
  78. ok(_.isEqual(model.changedAttributes(), {name : 'Rob'}), 'changedAttributes returns the changed attrs');
  79. equals(model.previous('name'), 'Tim');
  80. ok(_.isEqual(model.previousAttributes(), {name : "Tim", age : 10}), 'previousAttributes is correct');
  81. });
  82. model.set({name : 'Rob'}, {silent : true});
  83. model.change();
  84. equals(model.get('name'), 'Rob');
  85. });
  86. test("Model: save", function() {
  87. doc.save({title : "Henry V"});
  88. equals(lastRequest[0], 'update');
  89. ok(_.isEqual(lastRequest[1], doc));
  90. });
  91. test("Model: destroy", function() {
  92. doc.destroy();
  93. equals(lastRequest[0], 'delete');
  94. ok(_.isEqual(lastRequest[1], doc));
  95. });
  96. test("Model: validate", function() {
  97. var lastError;
  98. var model = new Backbone.Model();
  99. model.validate = function(attrs) {
  100. if (attrs.admin) return "Can't change admin status.";
  101. };
  102. model.bind('error', function(model, error) {
  103. lastError = error;
  104. });
  105. var result = model.set({a: 100});
  106. equals(result, model);
  107. equals(model.get('a'), 100);
  108. equals(lastError, undefined);
  109. result = model.set({a: 200, admin: true});
  110. equals(result, false);
  111. equals(model.get('a'), 100);
  112. equals(lastError, "Can't change admin status.");
  113. });
  114. });