PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/test/sync.js

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