/server/app/webroot/vendor/backbone.localStorage/spec/localStorage_spec.js

https://bitbucket.org/albertoprb/fleetster-challenge · JavaScript · 338 lines · 233 code · 100 blank · 5 comment · 0 complexity · 91f0e50dad966e6c2a8b24645b5bb0ec MD5 · raw file

  1. describe("Backbone.localStorage", function(){
  2. var attributes = {
  3. string: "String",
  4. string2: "String 2",
  5. number: 1337
  6. };
  7. describe("on a Collection", function(){
  8. var Model = Backbone.Model.extend({
  9. defaults: attributes
  10. });
  11. var Collection = Backbone.Collection.extend({
  12. model: Model,
  13. localStorage: new Backbone.LocalStorage("collectionStore")
  14. });
  15. var collection = new Collection();
  16. // Clean up before starting
  17. before(function(){
  18. window.localStorage.clear();
  19. });
  20. before(function(){
  21. collection.fetch();
  22. });
  23. it("should use `localSync`", function(){
  24. assert.equal(Backbone.getSyncMethod(collection), Backbone.localSync);
  25. });
  26. it("should initially be empty", function(){
  27. assert.equal(collection.length, 0);
  28. });
  29. describe("create", function(){
  30. var model;
  31. before(function(){
  32. model = collection.create({});
  33. });
  34. it("should have 1 model", function(){
  35. assert.equal(collection.length, 1);
  36. });
  37. it("should have a populated model", function(){
  38. var withId = _.clone(attributes);
  39. withId.id = model.id;
  40. assert.deepEqual(model.toJSON(), withId);
  41. });
  42. it("should have assigned an `id` to the model", function(){
  43. assert.isDefined(model.id);
  44. });
  45. });
  46. describe("get (by `id`)", function(){
  47. var model;
  48. before(function(){
  49. model = collection.create({});
  50. });
  51. it("should find the model with its `id`", function(){
  52. assert.equal(collection.get(model.id), model);
  53. });
  54. });
  55. describe("instances", function(){
  56. describe("save", function(){
  57. var model, model2;
  58. before(function(){
  59. model = collection.create({});
  60. model.save({string: "String 0"});
  61. collection.fetch()
  62. });
  63. it("should persist the changes", function(){
  64. assert.equal(model.get("string"), "String 0");
  65. });
  66. describe("with a new `id`", function(){
  67. before(function(){
  68. model2 = collection.create({});
  69. model2.save({id: 1});
  70. collection.fetch();
  71. });
  72. it("should have a new `id`", function(){
  73. assert.equal(model2.id, 1);
  74. });
  75. it("should have kept its old properties", function(){
  76. var withId = _.clone(attributes);
  77. withId.id = 1;
  78. assert.deepEqual(model2.toJSON(), withId);
  79. });
  80. });
  81. });
  82. describe("destroy", function(){
  83. var beforeFetchLength, afterFetchLength;
  84. before(function(){
  85. // Make sure there's at least items in there
  86. // ... can't rely on previous tests
  87. _(5).times(function(){
  88. collection.create()
  89. });
  90. });
  91. before(function(){
  92. _.each(collection.toArray(), function(model){
  93. model.destroy();
  94. });
  95. beforeFetchLength = collection.length;
  96. });
  97. before(function(){
  98. collection.fetch();
  99. afterFetchLength = collection.length;
  100. });
  101. it("should have removed all items from the collection", function(){
  102. assert.equal(beforeFetchLength, 0);
  103. });
  104. it("should have removed all items from the store", function(){
  105. assert.equal(afterFetchLength, 0);
  106. });
  107. });
  108. describe("with a different `idAttribute`", function(){
  109. var Model2 = Backbone.Model.extend({
  110. defaults: attributes,
  111. idAttribute: "_id"
  112. });
  113. var Collection2 = Backbone.Collection.extend({
  114. model: Model2,
  115. localStorage: new Backbone.LocalStorage("collection2Store")
  116. });
  117. var collection2 = new Collection2();
  118. before(function(){
  119. collection2.create();
  120. });
  121. it("should have used the custom `idAttribute`", function(){
  122. assert.equal(collection2.first().id, collection2.first().get("_id"));
  123. });
  124. });
  125. });
  126. });
  127. describe("on a Model", function(){
  128. var Model = Backbone.Model.extend({
  129. defaults: attributes,
  130. localStorage: new Backbone.LocalStorage("modelStore")
  131. });
  132. var model = new Model();
  133. before(function(){
  134. window.localStorage.clear();
  135. });
  136. it("should use `localSync`", function(){
  137. assert.equal(Backbone.getSyncMethod(model), Backbone.localSync);
  138. });
  139. describe("save", function(){
  140. before(function(){
  141. model.save();
  142. model.fetch();
  143. });
  144. it("should be saved in the store", function(){
  145. assert.isDefined(model.id);
  146. });
  147. describe("with new attributes", function(){
  148. before(function(){
  149. model.save({number: 42});
  150. model.fetch();
  151. });
  152. it("should persist the changes", function(){
  153. assert.deepEqual(model.toJSON(), _.extend(_.clone(attributes), {id: model.id, number: 42}));
  154. });
  155. });
  156. });
  157. describe("destroy", function(){
  158. before(function(){
  159. model.destroy();
  160. });
  161. it("should have removed the instance from the store", function(){
  162. assert.lengthOf(Model.prototype.localStorage.findAll(), 0);
  163. });
  164. });
  165. });
  166. describe("Error handling", function(){
  167. var Model = Backbone.Model.extend({
  168. defaults: attributes,
  169. localStorage: new Backbone.LocalStorage("modelStore")
  170. });
  171. before(function(){
  172. window.localStorage.clear();
  173. });
  174. describe("private browsing", function(){
  175. var model = new Model()
  176. , oldSetItem = window.localStorage.setItem
  177. before(function(){
  178. window.localStorage.setItem = function(){
  179. var error = new Error();
  180. error.code = DOMException.QUOTA_EXCEEDED_ERR;
  181. throw error;
  182. };
  183. });
  184. var error;
  185. before(function(){
  186. model.save(attributes, {
  187. error: function(model, err){
  188. error = err;
  189. }
  190. })
  191. });
  192. it("should return the error in the error callback", function(){
  193. assert.equal(error, "Private browsing is unsupported");
  194. });
  195. after(function(){
  196. window.localStorage.setItem = oldSetItem;
  197. })
  198. });
  199. });
  200. });
  201. describe("Without Backbone.localStorage", function(){
  202. describe("on a Collection", function(){
  203. var Collection = Backbone.Collection.extend()
  204. , collection = new Collection();
  205. it("should use `ajaxSync`", function(){
  206. assert.equal(Backbone.getSyncMethod(collection), Backbone.ajaxSync);
  207. });
  208. });
  209. describe("on a Model", function(){
  210. var Model = Backbone.Model.extend()
  211. , model = new Model();
  212. it("should use `ajaxSync`", function(){
  213. assert.equal(Backbone.getSyncMethod(model), Backbone.ajaxSync);
  214. });
  215. });
  216. });
  217. // For some reason this is not ran when viewed in a browser
  218. // but it is ran when using `mocha-phantomjs`.
  219. describe("AMD", function(){
  220. require.config({
  221. paths: {
  222. jquery: "support/jquery",
  223. underscore: "support/underscore",
  224. backbone: "support/backbone",
  225. localstorage: "../backbone.localStorage"
  226. }
  227. });
  228. var LocalStorage;
  229. before(function(done){
  230. require(["localstorage"], function(LS){
  231. LocalStorage = LS;
  232. done()
  233. });
  234. });
  235. it("should be the same as the non-amd usage", function(){
  236. assert.equal(Backbone.LocalStorage, LocalStorage)
  237. });
  238. });