PageRenderTime 40ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/node_modules/ender/test/node_modules/backbone/test/sync.js

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