Jop de Klein avatar Jop de Klein committed 66ef273

Implemented webstorageBackend.remove and snap.remove

Comments (0)

Files changed (8)

examples/mpi/index.html

             .snap-error {
                 border: 3px dashed red;
             }
+            
+            .myElement {
+                color: black;
+            }
         </style>
         
     </head>
     
     <body>
-        <div style="color: blue">Test Page</div>
+        <div class="myElement">Test Page</div>
     </body>
     
 </html>
         <script type="text/javascript">
             window.onload = function () {
                 snap.setSnapshotSettings({
-                    'styleIncludes': ['color', 'backgroundColor', 'paddingLeft']
+                    'styleIncludes': [/color/, 'backgroundColor', 'paddingLeft']
                 });
             };
         </script>
         <div style="padding: 10px; height: 2em">
             <div style="color: blue">hello world</div>
         </div>
-        <div style="color: brown">hello world</div>
+        <div style="color: blue">hello world</div>
         <div style="color: black">hello world</div>
         <div style="color: green">hello world</div>
         <div style="color: blue">hello world</div>

src/js/snap.backend.js

                 success = true;
             } catch (e) {
                 // something went wrong
+                throw 'SnapBackendSaveError';
             }
             
             return success;
         };
         
         that.load = function (uid) {
-            return localStorage.getItem(uid);
+            var snapShot = localStorage.getItem(uid);
+            if (!snapShot) {
+                throw 'SnapSnapshotNotFound';
+            }
+            
+            return snapShot;
+        };
+        
+        that.remove = function (uid) {
+            localStorage.removeItem(uid);
         };
         
         return that;
         var currentSnapshot = ns.createSnapshot();
         var comparator = ns.nodeComparator();
         
+        
+        // console.dir(latestSnapshot.getContents());
+        // console.dir(currentSnapshot.getContents());
+        
         var comparison = comparator.compare(
             latestSnapshot.getContents(), 
             currentSnapshot.getContents()
         }
     };
     
+    ns.removeSnapshot = function () {
+        var backend = ns.webStorageBackend();
+        backend.remove(ns.getSnapshotSetting('id'));
+    };
+    
     // dummy implementation
-    ns.compareSnapshotFromCache = function (root, cache) {
-        var oldSnapshot = cache;
-        var newSnapshot = ns.createSnapshot();
-        
-        var comparator = ns.nodeComparator();
-        var isSame = comparator.compare(
-            oldSnapshot.getContents(), 
-            newSnapshot.getContents()
-        );
-        
-        return isSame;
-    };
     
     ns.getElements = function () {
         var iterator = snap.iterator({
             'root': ns.getSnapshotSetting('root')
         });
-        var elements = iterator.getElements();
-        
-        return elements;
+        return iterator.getElements();
     };
     
 }(snap));

src/js/snap.node.js

         snap.utils.extendSettings(settings, snap.getSnapshotSettings(), options);
         
         if (settings.styleExcludes.length && settings.styleIncludes.length) {
-            snap.console.warn('Both styleIncludes and styleExcludes are specified, ignoring styleExcludes');
+            // snap.console.warn('Both styleIncludes and styleExcludes are specified, ignoring styleExcludes');
             settings.styleExcludes = [];
         }
         

test/unit/test.snap.backend.js

 });
 */
 
-test('save (HTML5 Web Storage)', function () {
+test('WebstorageBackend save', function () {
     var backend = snap.webStorageBackend();
     
-    var str = 'abc';
-    var uid = '123';
+    var uid = 'foo';
+    var str = 'bar';
     ok('save sucessful', backend.save(uid, str));
 });
 
-test('load (HTML5 Web Storage)', function () {
+test('WebstorageBackend load', function () {
     var backend = snap.webStorageBackend();
-    var uid = '123';
+    var uid = 'foo';
     
     var str = backend.load(uid);
-    equals(str, 'abc');
+    equals(str, 'bar');
 });
 
-// TODO: clear test data
+test('WebstorageBackend load not found', function () {
+    var backend = snap.webStorageBackend();
+    var uid = '_non_existing_element';
+    
+    var f = function () {
+        backend.load(uid);
+    };
+    
+    raises(f, 'SnapSnapshotNotFound', 'Snapshot not found raises exception');
+    
+});
 
+test('WebstorageBackend remove', function () {
+    var backend = snap.webStorageBackend();
+    var uid = 'foo';
+
+    backend.remove(uid);
+    
+    var f = function () {
+        backend.load(uid);
+    };
+    
+    raises(f, 'SnapSnapshotNotFound', 'Snapshot is removed');
+    
+});
+
+

test/unit/test.snap.js

     var elements = snap.getElements();
     equals(elements.length, 4);
     
+    equals(comparison.differences[0].difference['background-color'][0], 'transparent');
+    equals(comparison.differences[0].difference['background-color'][1], 'rgb(0, 0, 255)');
+    equals(undefined, comparison.differences[1]);
+    
     snap.setSnapshotSetting('root', oldRoot);
-});
-
+    
+    // remove test snapshot
+    snap.removeSnapshot();
+});

test/unit/test.snap.node.js

 });
 
 test('styles include setting (global regexp)', function () {
-    snap.setSnapshotSetting('styleExcludes', [/color/]);
+    snap.setSnapshotSetting('styleIncludes', [/color/]);
     
     var root = document.getElementById('test_root');
     var representation = snap.node(root);
     
-    equals(representation.computedStyle['background-color'], undefined);
-    equals(representation.computedStyle['color'], undefined);
-    equals(representation.computedStyle['border-left-color'], undefined);
-    
+    equals(representation.computedStyle['background-color'], 'rgb(0, 0, 255)');
+    equals(representation.computedStyle['color'], 'rgb(0, 0, 0)');
+    equals(representation.computedStyle['border-left-color'], 'rgb(0, 0, 0)');
 });
 
 test('filteredStyle caching (global)', function () {
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.