/tests/test.js

https://github.com/raybaxter/Backbone.localStorage · JavaScript · 183 lines · 148 code · 34 blank · 1 comment · 1 complexity · e13edf68a1359ea0c3b0cb0bebff87ac MD5 · raw file

  1. QUnit.done = function(results){
  2. if (results.failed)
  3. console.log("failed")
  4. else
  5. console.log("success")
  6. };
  7. $(document).ready(function() {
  8. var Library = Backbone.Collection.extend({
  9. localStorage: new Backbone.LocalStorage("libraryStore")
  10. // is the problem with my library that is has no model reference?
  11. });
  12. var attrs = {
  13. title : 'The Tempest',
  14. author : 'Bill Shakespeare',
  15. length : 123
  16. };
  17. var library = null;
  18. module("localStorage on collections", {
  19. setup: function() {
  20. window.localStorage.clear();
  21. library = new Library();
  22. }
  23. });
  24. test("should be empty initially", function() {
  25. equals(library.length, 0, 'empty initially');
  26. library.fetch();
  27. equals(library.length, 0, 'empty read');
  28. });
  29. test("should create item", function() {
  30. library.create(attrs);
  31. equals(library.length, 1, 'one item added');
  32. equals(library.first().get('title'), 'The Tempest', 'title was read');
  33. equals(library.first().get('author'), 'Bill Shakespeare', 'author was read');
  34. equals(library.first().get('length'), 123, 'length was read');
  35. });
  36. test("should discard unsaved changes on fetch", function() {
  37. library.create(attrs);
  38. library.first().set({ 'title': "Wombat's Fun Adventure" });
  39. equals(library.first().get('title'), "Wombat's Fun Adventure", 'title changed, but not saved');
  40. library.fetch();
  41. equals(library.first().get('title'), 'The Tempest', 'title was read');
  42. });
  43. test("should persist changes", function(){
  44. library.create(attrs);
  45. equals(library.first().get('author'), 'Bill Shakespeare', 'author was read');
  46. library.first().save({ author: 'William Shakespeare' });
  47. library.fetch();
  48. equals(library.first().get('author'), 'William Shakespeare', 'verify author update');
  49. });
  50. test("should store model id inside collection", function() {
  51. var book = library.create(attrs);
  52. equals(library.get(book.id), book, 'book has been read by id from collection');
  53. });
  54. test("should allow to change id", function() {
  55. library.create(attrs);
  56. library.first().save({id: '1-the-tempest', author: 'William Shakespeare'});
  57. equals(library.first().get('id'), '1-the-tempest', 'verify ID update');
  58. equals(library.first().get('title'), 'The Tempest', 'verify title is still there');
  59. equals(library.first().get('author'), 'William Shakespeare', 'verify author update');
  60. equals(library.first().get('length'), 123, 'verify length is still there');
  61. library.fetch();
  62. equals(library.length, 2, 'should not auto remove first object when changing ID');
  63. });
  64. test("should remove from collection", function() {
  65. _(23).times(function(index) {
  66. library.create({id: index});
  67. });
  68. _(library.toArray()).chain().clone().each(function(book) {
  69. book.destroy();
  70. });
  71. equals(library.length, 0, 'item was destroyed and library is empty');
  72. library.fetch()
  73. equals(library.length, 0, 'item was destroyed and library is empty even after fetch');
  74. });
  75. test("should not try to load items from localstorage if they are not there anymore", function() {
  76. library.create(attrs);
  77. localStorage.clear();
  78. library.fetch();
  79. equals(0, library.length);
  80. });
  81. test("should load from session store without server request", function() {
  82. library.create(attrs);
  83. secondLibrary = new Library();
  84. secondLibrary.fetch();
  85. equals(1, secondLibrary.length);
  86. });
  87. test("should cope with arbitrary idAttributes", function() {
  88. var Model = Backbone.Model.extend({
  89. idAttribute: '_id'
  90. });
  91. var Collection = Backbone.Collection.extend({
  92. model: Model,
  93. localStorage: new Store('strangeID')
  94. });
  95. var collection = new Collection();
  96. collection.create({});
  97. equals(collection.first().id, collection.first().get('_id'));
  98. });
  99. module("localStorage on models", {
  100. setup: function() {
  101. window.localStorage.clear();
  102. book = new Book();
  103. }
  104. });
  105. var Book = Backbone.Model.extend({
  106. defaults: {
  107. title : 'The Tempest',
  108. author : 'Bill Shakespeare',
  109. length : 123
  110. },
  111. localStorage : new Backbone.LocalStorage('TheTempest')
  112. });
  113. var book = null;
  114. test("should overwrite unsaved changes when fetching", function() {
  115. book.save()
  116. book.set({ 'title': "Wombat's Fun Adventure" });
  117. book.fetch();
  118. equals(book.get('title'), 'The Tempest', 'model created');
  119. });
  120. test("should persist changes", function(){
  121. book.save({ author: 'William Shakespeare'});
  122. book.fetch();
  123. equals(book.get('author'), 'William Shakespeare', 'author successfully updated');
  124. equals(book.get('length'), 123, 'verify length is still there');
  125. });
  126. test("should remove book when destroying", function() {
  127. book.save({author: 'fnord'})
  128. equals(Book.prototype.localStorage.findAll().length, 1, 'book removed');
  129. book.destroy()
  130. equals(Book.prototype.localStorage.findAll().length, 0, 'book removed');
  131. });
  132. test("Book should use local sync", function()
  133. {
  134. var method = Backbone.getSyncMethod(book);
  135. equals(method, Backbone.localSync);
  136. });
  137. var MyRemoteModel = Backbone.Model.extend();
  138. var remoteModel = new MyRemoteModel();
  139. test("remoteModel should use ajax sync", function()
  140. {
  141. var method = Backbone.getSyncMethod(remoteModel);
  142. equals(method, Backbone.ajaxSync);
  143. });
  144. test("Backbone.sync should return a value when ajax is used.", function ()
  145. {
  146. var returnValue = remoteModel.fetch({url: '/'});
  147. notEqual(returnValue, undefined);
  148. });
  149. });