PageRenderTime 42ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/test/sync.js

https://github.com/gotomypc/backbone
JavaScript | 160 lines | 140 code | 20 blank | 0 comment | 0 complexity | 79874c10b428e7f5a9dd716a5628644a MD5 | raw file
  1. $(document).ready(function() {
  2. var Library = Backbone.Collection.extend({
  3. url : function() { return '/library'; }
  4. });
  5. var library;
  6. var attrs = {
  7. title : "The Tempest",
  8. author : "Bill Shakespeare",
  9. length : 123
  10. };
  11. module("Backbone.sync", _.extend(new Environment, {
  12. setup : function() {
  13. Environment.prototype.setup.apply(this, arguments);
  14. library = new Library;
  15. library.create(attrs, {wait: false});
  16. }
  17. }));
  18. test("sync: read", 4, function() {
  19. library.fetch();
  20. equal(this.ajaxSettings.url, '/library');
  21. equal(this.ajaxSettings.type, 'GET');
  22. equal(this.ajaxSettings.dataType, 'json');
  23. ok(_.isEmpty(this.ajaxSettings.data));
  24. });
  25. test("sync: passing data", 3, function() {
  26. library.fetch({data: {a: 'a', one: 1}});
  27. equal(this.ajaxSettings.url, '/library');
  28. equal(this.ajaxSettings.data.a, 'a');
  29. equal(this.ajaxSettings.data.one, 1);
  30. });
  31. test("sync: create", 6, function() {
  32. equal(this.ajaxSettings.url, '/library');
  33. equal(this.ajaxSettings.type, 'POST');
  34. equal(this.ajaxSettings.dataType, 'json');
  35. var data = JSON.parse(this.ajaxSettings.data);
  36. equal(data.title, 'The Tempest');
  37. equal(data.author, 'Bill Shakespeare');
  38. equal(data.length, 123);
  39. });
  40. test("sync: update", 7, function() {
  41. library.first().save({id: '1-the-tempest', author: 'William Shakespeare'});
  42. equal(this.ajaxSettings.url, '/library/1-the-tempest');
  43. equal(this.ajaxSettings.type, 'PUT');
  44. equal(this.ajaxSettings.dataType, 'json');
  45. var data = JSON.parse(this.ajaxSettings.data);
  46. equal(data.id, '1-the-tempest');
  47. equal(data.title, 'The Tempest');
  48. equal(data.author, 'William Shakespeare');
  49. equal(data.length, 123);
  50. });
  51. test("sync: update with emulateHTTP and emulateJSON", 7, function() {
  52. Backbone.emulateHTTP = Backbone.emulateJSON = true;
  53. library.first().save({id: '2-the-tempest', author: 'Tim Shakespeare'});
  54. equal(this.ajaxSettings.url, '/library/2-the-tempest');
  55. equal(this.ajaxSettings.type, 'POST');
  56. equal(this.ajaxSettings.dataType, 'json');
  57. equal(this.ajaxSettings.data._method, 'PUT');
  58. var data = JSON.parse(this.ajaxSettings.data.model);
  59. equal(data.id, '2-the-tempest');
  60. equal(data.author, 'Tim Shakespeare');
  61. equal(data.length, 123);
  62. Backbone.emulateHTTP = Backbone.emulateJSON = false;
  63. });
  64. test("sync: update with just emulateHTTP", 6, function() {
  65. Backbone.emulateHTTP = true;
  66. library.first().save({id: '2-the-tempest', author: 'Tim Shakespeare'});
  67. equal(this.ajaxSettings.url, '/library/2-the-tempest');
  68. equal(this.ajaxSettings.type, 'POST');
  69. equal(this.ajaxSettings.contentType, 'application/json');
  70. var data = JSON.parse(this.ajaxSettings.data);
  71. equal(data.id, '2-the-tempest');
  72. equal(data.author, 'Tim Shakespeare');
  73. equal(data.length, 123);
  74. Backbone.emulateHTTP = false;
  75. });
  76. test("sync: update with just emulateJSON", 6, function() {
  77. Backbone.emulateJSON = true;
  78. library.first().save({id: '2-the-tempest', author: 'Tim Shakespeare'});
  79. equal(this.ajaxSettings.url, '/library/2-the-tempest');
  80. equal(this.ajaxSettings.type, 'PUT');
  81. equal(this.ajaxSettings.contentType, 'application/x-www-form-urlencoded');
  82. var data = JSON.parse(this.ajaxSettings.data.model);
  83. equal(data.id, '2-the-tempest');
  84. equal(data.author, 'Tim Shakespeare');
  85. equal(data.length, 123);
  86. Backbone.emulateJSON = false;
  87. });
  88. test("sync: read model", 3, function() {
  89. library.first().save({id: '2-the-tempest', author: 'Tim Shakespeare'});
  90. library.first().fetch();
  91. equal(this.ajaxSettings.url, '/library/2-the-tempest');
  92. equal(this.ajaxSettings.type, 'GET');
  93. ok(_.isEmpty(this.ajaxSettings.data));
  94. });
  95. test("sync: destroy", 3, function() {
  96. library.first().save({id: '2-the-tempest', author: 'Tim Shakespeare'});
  97. library.first().destroy({wait: true});
  98. equal(this.ajaxSettings.url, '/library/2-the-tempest');
  99. equal(this.ajaxSettings.type, 'DELETE');
  100. equal(this.ajaxSettings.data, null);
  101. });
  102. test("sync: destroy with emulateHTTP", 3, function() {
  103. library.first().save({id: '2-the-tempest', author: 'Tim Shakespeare'});
  104. Backbone.emulateHTTP = Backbone.emulateJSON = true;
  105. library.first().destroy();
  106. equal(this.ajaxSettings.url, '/library/2-the-tempest');
  107. equal(this.ajaxSettings.type, 'POST');
  108. equal(JSON.stringify(this.ajaxSettings.data), '{"_method":"DELETE"}');
  109. Backbone.emulateHTTP = Backbone.emulateJSON = false;
  110. });
  111. test("sync: urlError", 2, function() {
  112. var model = new Backbone.Model();
  113. raises(function() {
  114. model.fetch();
  115. });
  116. model.fetch({url: '/one/two'});
  117. equal(this.ajaxSettings.url, '/one/two');
  118. });
  119. test("#1052 - `options` is optional.", 0, function() {
  120. var model = new Backbone.Model();
  121. model.url = '/test';
  122. Backbone.sync('create', model);
  123. });
  124. test("Backbone.ajax", 1, function() {
  125. Backbone.ajax = function(settings){
  126. strictEqual(settings.url, '/test');
  127. };
  128. var model = new Backbone.Model();
  129. model.url = '/test';
  130. Backbone.sync('create', model);
  131. });
  132. test("Call provided error callback on error.", 1, function() {
  133. var model = new Backbone.Model;
  134. model.url = '/test';
  135. Backbone.sync('read', model, {
  136. error: function() { ok(true); }
  137. });
  138. this.ajaxSettings.error();
  139. });
  140. });