PageRenderTime 70ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/test/sync.js

https://github.com/chandlerkent/backbone
JavaScript | 127 lines | 108 code | 17 blank | 2 comment | 0 complexity | c33e3e017a5c384db747627fce5e405d MD5 | raw file
  1. $(document).ready(function() {
  2. module("Backbone.sync");
  3. // Variable to catch the last request.
  4. window.lastRequest = null;
  5. // Stub out jQuery.ajax...
  6. $.ajax = function(obj) {
  7. lastRequest = obj;
  8. };
  9. var Library = Backbone.Collection.extend({
  10. url : function() { return '/library'; }
  11. });
  12. var library = new Library();
  13. var attrs = {
  14. title : "The Tempest",
  15. author : "Bill Shakespeare",
  16. length : 123
  17. };
  18. test("sync: read", function() {
  19. Backbone.sync = originalSync;
  20. library.fetch();
  21. equals(lastRequest.url, '/library');
  22. equals(lastRequest.type, 'GET');
  23. equals(lastRequest.dataType, 'json');
  24. ok(_.isEmpty(lastRequest.data));
  25. });
  26. test("sync: passing data", function() {
  27. library.fetch({data: {a: 'a', one: 1}});
  28. equals(lastRequest.url, '/library');
  29. equals(lastRequest.data.a, 'a');
  30. equals(lastRequest.data.one, 1);
  31. });
  32. test("sync: create", function() {
  33. library.add(library.create(attrs));
  34. equals(lastRequest.url, '/library');
  35. equals(lastRequest.type, 'POST');
  36. equals(lastRequest.dataType, 'json');
  37. var data = JSON.parse(lastRequest.data);
  38. equals(data.title, 'The Tempest');
  39. equals(data.author, 'Bill Shakespeare');
  40. equals(data.length, 123);
  41. });
  42. test("sync: update", function() {
  43. library.first().save({id: '1-the-tempest', author: 'William Shakespeare'});
  44. equals(lastRequest.url, '/library/1-the-tempest');
  45. equals(lastRequest.type, 'PUT');
  46. equals(lastRequest.dataType, 'json');
  47. var data = JSON.parse(lastRequest.data);
  48. equals(data.id, '1-the-tempest');
  49. equals(data.title, 'The Tempest');
  50. equals(data.author, 'William Shakespeare');
  51. equals(data.length, 123);
  52. });
  53. test("sync: update with emulateHTTP and emulateJSON", function() {
  54. Backbone.emulateHTTP = Backbone.emulateJSON = true;
  55. library.first().save({id: '2-the-tempest', author: 'Tim Shakespeare'});
  56. equals(lastRequest.url, '/library/2-the-tempest');
  57. equals(lastRequest.type, 'POST');
  58. equals(lastRequest.dataType, 'json');
  59. equals(lastRequest.data._method, 'PUT');
  60. var data = JSON.parse(lastRequest.data.model);
  61. equals(data.id, '2-the-tempest');
  62. equals(data.author, 'Tim Shakespeare');
  63. equals(data.length, 123);
  64. Backbone.emulateHTTP = Backbone.emulateJSON = false;
  65. });
  66. test("sync: update with just emulateHTTP", function() {
  67. Backbone.emulateHTTP = true;
  68. library.first().save({id: '2-the-tempest', author: 'Tim Shakespeare'});
  69. equals(lastRequest.url, '/library/2-the-tempest');
  70. equals(lastRequest.type, 'POST');
  71. equals(lastRequest.contentType, 'application/json');
  72. var data = JSON.parse(lastRequest.data);
  73. equals(data.id, '2-the-tempest');
  74. equals(data.author, 'Tim Shakespeare');
  75. equals(data.length, 123);
  76. Backbone.emulateHTTP = false;
  77. });
  78. test("sync: update with just emulateJSON", function() {
  79. Backbone.emulateJSON = true;
  80. library.first().save({id: '2-the-tempest', author: 'Tim Shakespeare'});
  81. equals(lastRequest.url, '/library/2-the-tempest');
  82. equals(lastRequest.type, 'PUT');
  83. equals(lastRequest.contentType, 'application/x-www-form-urlencoded');
  84. var data = JSON.parse(lastRequest.data.model);
  85. equals(data.id, '2-the-tempest');
  86. equals(data.author, 'Tim Shakespeare');
  87. equals(data.length, 123);
  88. Backbone.emulateJSON = false;
  89. });
  90. test("sync: read model", function() {
  91. library.first().fetch();
  92. equals(lastRequest.url, '/library/2-the-tempest');
  93. equals(lastRequest.type, 'GET');
  94. ok(_.isEmpty(lastRequest.data));
  95. });
  96. test("sync: destroy", function() {
  97. library.first().destroy();
  98. equals(lastRequest.url, '/library/2-the-tempest');
  99. equals(lastRequest.type, 'DELETE');
  100. equals(lastRequest.data, null);
  101. });
  102. test("sync: destroy with emulateHTTP", function() {
  103. Backbone.emulateHTTP = Backbone.emulateJSON = true;
  104. library.first().destroy();
  105. equals(lastRequest.url, '/library/2-the-tempest');
  106. equals(lastRequest.type, 'POST');
  107. equals(JSON.stringify(lastRequest.data), '{"_method":"DELETE"}');
  108. Backbone.emulateHTTP = Backbone.emulateJSON = false;
  109. });
  110. });