PageRenderTime 55ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/test/specs/SafeCollectionSpec.js

https://github.com/Fer0x/Backbone.Safe
JavaScript | 151 lines | 71 code | 22 blank | 58 comment | 0 complexity | caaad062e178b8f4865ef56238edb178 MD5 | raw file
Possible License(s): MIT
  1. describe("Backbone Safe", function () {
  2. var SafeCollection;
  3. var items = mockData.result;
  4. // instance
  5. var col;
  6. describe("Collection Suite", function(){
  7. // ensure the safe is created for the first time
  8. localStorage.removeItem("safe-col-tester");
  9. beforeEach(function() {
  10. SafeCollection = Backbone.Collection.extend({
  11. safe: "safe-col-tester"
  12. });
  13. col = new SafeCollection();
  14. });
  15. it("should have a safe attribute", function(){
  16. expect( col.safe ).toBeDefined();
  17. });
  18. it("should have a safe attribute which is defined as a collection", function(){
  19. expect( col.safe.isCollection ).toBeDefined();
  20. expect( col.safe.isCollection ).toBeTruthy();
  21. });
  22. it("should have a uid attribute", function(){
  23. expect( col.safe.uid ).toBeDefined();
  24. });
  25. });
  26. describe("Collection Suite with json", function(){
  27. localStorage.removeItem("safe-col-with-data");
  28. beforeEach(function(){
  29. SafeCollection = Backbone.Collection.extend({
  30. safe: "safe-col-with-data"
  31. });
  32. });
  33. it("should create a collection with a safe from data", function(){
  34. col = new SafeCollection(items);
  35. expect( col ).toBeDefined();
  36. expect( col.safe ).toBeDefined();
  37. });
  38. it("should save data to storage and retreive it", function(){
  39. col = new SafeCollection();
  40. col.safe.destroy();
  41. col.safe.create();
  42. col.set(items);
  43. expect( col.toJSON() ).toEqual( col.safe.getData() );
  44. });
  45. it("should create a new collection and load the relevant data from local-storage", function(){
  46. col = new SafeCollection();
  47. expect( col.toJSON() ).toEqual( col.safe.getData() );
  48. });
  49. it("should fetch data from local-storage using 'fetch'", function(){
  50. col = new SafeCollection();
  51. col.remove(col.models, { silent: true });
  52. col.fetch({ from: 'safe' });
  53. expect( col.toJSON() ).toEqual( col.safe.getData() );
  54. });
  55. it("should fetch data from a given url using 'fetch' and ignore safe", function(){
  56. // create a spy for a custom 'fetch'
  57. spyOn($, 'ajax').andCallFake(function(options) {
  58. options.success(mockData.result);
  59. });
  60. col = new SafeCollection();
  61. col.url = "src/mockData.json";
  62. col.fetch({
  63. success: function(col){
  64. // expect( col.get('id') ).toBeDefined();
  65. expect( col.toJSON() ).toEqual( col.safe.getData() );
  66. }
  67. });
  68. });
  69. // it("should clear the localStorage when 'destroy' is called", function() {
  70. // // create a spy for a custom 'destroy'
  71. // spyOn(Backbone.Model.prototype, 'destroy').andCallFake(function(options){
  72. // this.clear();
  73. // this.trigger('destroy');
  74. // });
  75. // model = new SafeCollection();
  76. // model.set(mockData);
  77. // expect( model.get('id') ).toBeDefined();
  78. // model.destroy();
  79. // expect( model.id ).toBeUndefined();
  80. // expect( model.safe.getData() ).toBeNull();
  81. // });
  82. it("should return the JSON from the 'toJSON' method", function(){
  83. col = new SafeCollection();
  84. col.set(mockData.result);
  85. expect( col.toJSON() ).toEqual( mockData.result );
  86. });
  87. /*
  88. it("should save to storage when the collection changes", function(){
  89. var randomId = Math.round(Math.random()*1000) + 10;
  90. model = new SafeCollection();
  91. model.set(mockData);
  92. model.set('id', randomId);
  93. expect( model.get('id') ).not.toBe( mockData.id );
  94. expect( model.get('id') ).toEqual( randomId );
  95. expect( model.get('id') ).toEqual( model.safe.getData().id );
  96. });
  97. it("should use constructor's 'initialize' method if config doesn't have", function(){
  98. var newId = 555;
  99. var AnotherSafeCollection = Backbone.Model.extend({
  100. safe: "another-safe-tester-2",
  101. initialize: function() {
  102. this.set('id', newId);
  103. }
  104. });
  105. model = new AnotherSafeCollection();
  106. expect( model.attributes.id ).toBeDefined();
  107. expect( model.get('id') ).toEqual( newId );
  108. });
  109. it("should use constructor's 'initialize' when extending another Model", function(){
  110. var newId = 555;
  111. var AnotherSafeCollection = Backbone.Model.extend({
  112. safe: "another-safe-tester",
  113. initialize: function() {
  114. this.set('id', newId);
  115. }
  116. });
  117. var ExtendedModel = AnotherSafeCollection.extend({});
  118. model = new ExtendedModel();
  119. expect( model.attributes.id ).toBeDefined();
  120. expect( model.get('id') ).toEqual( newId );
  121. });
  122. */
  123. });
  124. });