PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/test/sync.js

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