PageRenderTime 74ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/app/vendor/backbone.localstorage/spec/localStorage_spec.js

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