PageRenderTime 881ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/backbone/test/sync.js

https://gitlab.com/pravi/lodash
JavaScript | 239 lines | 210 code | 29 blank | 0 comment | 0 complexity | e58f93dccfe50a8f2dc17d7b6f76b2f8 MD5 | raw file
  1. (function(QUnit) {
  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. QUnit.module('Backbone.sync', {
  12. beforeEach: function(assert) {
  13. library = new Library;
  14. library.create(attrs, {wait: false});
  15. },
  16. afterEach: function(assert) {
  17. Backbone.emulateHTTP = false;
  18. }
  19. });
  20. QUnit.test('read', function(assert) {
  21. assert.expect(4);
  22. library.fetch();
  23. assert.equal(this.ajaxSettings.url, '/library');
  24. assert.equal(this.ajaxSettings.type, 'GET');
  25. assert.equal(this.ajaxSettings.dataType, 'json');
  26. assert.ok(_.isEmpty(this.ajaxSettings.data));
  27. });
  28. QUnit.test('passing data', function(assert) {
  29. assert.expect(3);
  30. library.fetch({data: {a: 'a', one: 1}});
  31. assert.equal(this.ajaxSettings.url, '/library');
  32. assert.equal(this.ajaxSettings.data.a, 'a');
  33. assert.equal(this.ajaxSettings.data.one, 1);
  34. });
  35. QUnit.test('create', function(assert) {
  36. assert.expect(6);
  37. assert.equal(this.ajaxSettings.url, '/library');
  38. assert.equal(this.ajaxSettings.type, 'POST');
  39. assert.equal(this.ajaxSettings.dataType, 'json');
  40. var data = JSON.parse(this.ajaxSettings.data);
  41. assert.equal(data.title, 'The Tempest');
  42. assert.equal(data.author, 'Bill Shakespeare');
  43. assert.equal(data.length, 123);
  44. });
  45. QUnit.test('update', function(assert) {
  46. assert.expect(7);
  47. library.first().save({id: '1-the-tempest', author: 'William Shakespeare'});
  48. assert.equal(this.ajaxSettings.url, '/library/1-the-tempest');
  49. assert.equal(this.ajaxSettings.type, 'PUT');
  50. assert.equal(this.ajaxSettings.dataType, 'json');
  51. var data = JSON.parse(this.ajaxSettings.data);
  52. assert.equal(data.id, '1-the-tempest');
  53. assert.equal(data.title, 'The Tempest');
  54. assert.equal(data.author, 'William Shakespeare');
  55. assert.equal(data.length, 123);
  56. });
  57. QUnit.test('update with emulateHTTP and emulateJSON', function(assert) {
  58. assert.expect(7);
  59. library.first().save({id: '2-the-tempest', author: 'Tim Shakespeare'}, {
  60. emulateHTTP: true,
  61. emulateJSON: true
  62. });
  63. assert.equal(this.ajaxSettings.url, '/library/2-the-tempest');
  64. assert.equal(this.ajaxSettings.type, 'POST');
  65. assert.equal(this.ajaxSettings.dataType, 'json');
  66. assert.equal(this.ajaxSettings.data._method, 'PUT');
  67. var data = JSON.parse(this.ajaxSettings.data.model);
  68. assert.equal(data.id, '2-the-tempest');
  69. assert.equal(data.author, 'Tim Shakespeare');
  70. assert.equal(data.length, 123);
  71. });
  72. QUnit.test('update with just emulateHTTP', function(assert) {
  73. assert.expect(6);
  74. library.first().save({id: '2-the-tempest', author: 'Tim Shakespeare'}, {
  75. emulateHTTP: true
  76. });
  77. assert.equal(this.ajaxSettings.url, '/library/2-the-tempest');
  78. assert.equal(this.ajaxSettings.type, 'POST');
  79. assert.equal(this.ajaxSettings.contentType, 'application/json');
  80. var data = JSON.parse(this.ajaxSettings.data);
  81. assert.equal(data.id, '2-the-tempest');
  82. assert.equal(data.author, 'Tim Shakespeare');
  83. assert.equal(data.length, 123);
  84. });
  85. QUnit.test('update with just emulateJSON', function(assert) {
  86. assert.expect(6);
  87. library.first().save({id: '2-the-tempest', author: 'Tim Shakespeare'}, {
  88. emulateJSON: true
  89. });
  90. assert.equal(this.ajaxSettings.url, '/library/2-the-tempest');
  91. assert.equal(this.ajaxSettings.type, 'PUT');
  92. assert.equal(this.ajaxSettings.contentType, 'application/x-www-form-urlencoded');
  93. var data = JSON.parse(this.ajaxSettings.data.model);
  94. assert.equal(data.id, '2-the-tempest');
  95. assert.equal(data.author, 'Tim Shakespeare');
  96. assert.equal(data.length, 123);
  97. });
  98. QUnit.test('read model', function(assert) {
  99. assert.expect(3);
  100. library.first().save({id: '2-the-tempest', author: 'Tim Shakespeare'});
  101. library.first().fetch();
  102. assert.equal(this.ajaxSettings.url, '/library/2-the-tempest');
  103. assert.equal(this.ajaxSettings.type, 'GET');
  104. assert.ok(_.isEmpty(this.ajaxSettings.data));
  105. });
  106. QUnit.test('destroy', function(assert) {
  107. assert.expect(3);
  108. library.first().save({id: '2-the-tempest', author: 'Tim Shakespeare'});
  109. library.first().destroy({wait: true});
  110. assert.equal(this.ajaxSettings.url, '/library/2-the-tempest');
  111. assert.equal(this.ajaxSettings.type, 'DELETE');
  112. assert.equal(this.ajaxSettings.data, null);
  113. });
  114. QUnit.test('destroy with emulateHTTP', function(assert) {
  115. assert.expect(3);
  116. library.first().save({id: '2-the-tempest', author: 'Tim Shakespeare'});
  117. library.first().destroy({
  118. emulateHTTP: true,
  119. emulateJSON: true
  120. });
  121. assert.equal(this.ajaxSettings.url, '/library/2-the-tempest');
  122. assert.equal(this.ajaxSettings.type, 'POST');
  123. assert.equal(JSON.stringify(this.ajaxSettings.data), '{"_method":"DELETE"}');
  124. });
  125. QUnit.test('urlError', function(assert) {
  126. assert.expect(2);
  127. var model = new Backbone.Model();
  128. assert.raises(function() {
  129. model.fetch();
  130. });
  131. model.fetch({url: '/one/two'});
  132. assert.equal(this.ajaxSettings.url, '/one/two');
  133. });
  134. QUnit.test('#1052 - `options` is optional.', function(assert) {
  135. assert.expect(0);
  136. var model = new Backbone.Model();
  137. model.url = '/test';
  138. Backbone.sync('create', model);
  139. });
  140. QUnit.test('Backbone.ajax', function(assert) {
  141. assert.expect(1);
  142. Backbone.ajax = function(settings) {
  143. assert.strictEqual(settings.url, '/test');
  144. };
  145. var model = new Backbone.Model();
  146. model.url = '/test';
  147. Backbone.sync('create', model);
  148. });
  149. QUnit.test('Call provided error callback on error.', function(assert) {
  150. assert.expect(1);
  151. var model = new Backbone.Model;
  152. model.url = '/test';
  153. Backbone.sync('read', model, {
  154. error: function() { assert.ok(true); }
  155. });
  156. this.ajaxSettings.error();
  157. });
  158. QUnit.test('Use Backbone.emulateHTTP as default.', function(assert) {
  159. assert.expect(2);
  160. var model = new Backbone.Model;
  161. model.url = '/test';
  162. Backbone.emulateHTTP = true;
  163. model.sync('create', model);
  164. assert.strictEqual(this.ajaxSettings.emulateHTTP, true);
  165. Backbone.emulateHTTP = false;
  166. model.sync('create', model);
  167. assert.strictEqual(this.ajaxSettings.emulateHTTP, false);
  168. });
  169. QUnit.test('Use Backbone.emulateJSON as default.', function(assert) {
  170. assert.expect(2);
  171. var model = new Backbone.Model;
  172. model.url = '/test';
  173. Backbone.emulateJSON = true;
  174. model.sync('create', model);
  175. assert.strictEqual(this.ajaxSettings.emulateJSON, true);
  176. Backbone.emulateJSON = false;
  177. model.sync('create', model);
  178. assert.strictEqual(this.ajaxSettings.emulateJSON, false);
  179. });
  180. QUnit.test('#1756 - Call user provided beforeSend function.', function(assert) {
  181. assert.expect(4);
  182. Backbone.emulateHTTP = true;
  183. var model = new Backbone.Model;
  184. model.url = '/test';
  185. var xhr = {
  186. setRequestHeader: function(header, value) {
  187. assert.strictEqual(header, 'X-HTTP-Method-Override');
  188. assert.strictEqual(value, 'DELETE');
  189. }
  190. };
  191. model.sync('delete', model, {
  192. beforeSend: function(_xhr) {
  193. assert.ok(_xhr === xhr);
  194. return false;
  195. }
  196. });
  197. assert.strictEqual(this.ajaxSettings.beforeSend(xhr), false);
  198. });
  199. QUnit.test('#2928 - Pass along `textStatus` and `errorThrown`.', function(assert) {
  200. assert.expect(2);
  201. var model = new Backbone.Model;
  202. model.url = '/test';
  203. model.on('error', function(m, xhr, options) {
  204. assert.strictEqual(options.textStatus, 'textStatus');
  205. assert.strictEqual(options.errorThrown, 'errorThrown');
  206. });
  207. model.fetch();
  208. this.ajaxSettings.error({}, 'textStatus', 'errorThrown');
  209. });
  210. })(QUnit);