Commits

Jop de Klein committed d79dfba

Refactored snap to support .save() and .compare

Comments (0)

Files changed (5)

     
 Comparing against latest snapshot
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-    ``snap.compareWithLatest()``
+    ``snap.compare()``
 
 
 Reporting
         
         <script type="text/javascript">
             window.onload = function () {
-                snap.boot();
-                
                 snap.setSnapshotSettings({
                     'styleIncludes': ['color', 'backgroundColor', 'paddingLeft']
                 });
     };
     
     ns.settings = {
-        'id': '',
+        'id': 'snap.latest',
         'styleExcludes': [
             /^\d+$/,
             /^-moz/i,
     
     ns.setSnapshotSettings = function (options) {
         ns.utils.extendSettings(ns.settings, options);
-
         return ns.settings;
     };
     
     ns.getSnapshotSetting = function (setting) {
         return ns.utils.cloneObject(ns.settings[setting]);
     };
+        
+    ns.save = function (root) {
+        var backend = ns.webStorageBackend();
+        var snapshot = ns.createSnapshot(root);
+        
+        return backend.save(ns.getSnapshotSetting('id'), snapshot.serialize());
+    };
     
     ns.createSnapshot = function (root) {
         var iterator = snap.iterator({
             'root': root
         });
         var nodes = iterator.getNodes();
+        var snapshot = snap.snapshot();
         
-        var snapshot = snap.snapshot();
         snapshot.create(nodes);
         
         return snapshot;
     };
     
-    ns.saveSnapshot = function (snapshot) {
-        var backend = ns.webStorageBackend();
-        return backend.save('snap.latest', snapshot.serialize());
-    };
-    
-    ns.compareWithLatest = function (root) {
-        var backend = ns.webStorageBackend();
-        var latestSnapshot = ns.getLatest();
-        
+    ns.compare = function (root) {
+        var latestSnapshot = ns.loadSnapshot();
         var currentSnapshot = ns.createSnapshot(root);
         var comparator = ns.nodeComparator();
         
         return comparison;
     };
     
-    ns.getLatest = function () {
+    ns.loadSnapshot = function () {
         var backend = ns.webStorageBackend();
         
         return ns.snapshot().loadFromString(
-            backend.load('snap.latest')
+            backend.load(ns.getSnapshotSetting('id'))
         );    
     };
     
         });
         
         // TODO: store elements in snapshot to improve performance
-        var comparison = ns.compareWithLatest(root);
+        var comparison = ns.compare(root);
         
         if (!comparison.equals) {
             var elements = iterator.getElements();

test/unit/test.snap.js

     ok('snap bootup', snap.boot());
 });
 
-test('create snapshot', function () {
-    var root = document.getElementById('test_root');
-    var snapshot = snap.createSnapshot(root);
-    ok('Saving snapshot', snap.saveSnapshot(snapshot));
-    // ok(__cache = snap.createSnapshot(root).serialize());
-});
-
 test('get- and setSnapshotSettings', function () {
     var root = document.getElementById('test_root');
     
     equals(newSettings.id, snap.getSnapshotSettings().id);
     
     var snapshot = snap.createSnapshot(root);
-    console.dir(oldSettings);
-    
+
     // restore settings
     snap.setSnapshotSettings(oldSettings);
-    // ok(__cache = snap.createSnapshot(root).serialize());
 });
 
 test('get- and setSnapshotSetting', function () {
     equals(snap.getSnapshotSetting('id'), 'foo');
 });
 
+test('save snapshot', function () {
+    snap.setSnapshotSetting('id', 'snap.test');
+    var root = document.getElementById('test_root');
+    // var snapshot = snap.createSnapshot(root);
+    ok('Saving snapshot', snap.save(root));
+    // FIXME: Delete test snapshot
+    // ok(__cache = snap.createSnapshot(root).serialize());
+});
+
 test('compare with latest snapshot', function () {
-    var root = document.getElementById('test_root')
-    ok(snap.compareWithLatest(root), 'Comparing against same latest');
+    snap.setSnapshotSetting('id', 'snap.test');
+    var root = document.getElementById('test_root');
+    ok(snap.compare(root), 'Comparing against same latest');
 
     root.style.backgroundColor = 'blue';
-    var comparison = snap.compareWithLatest(root);
+    var comparison = snap.compare(root);
     equals(comparison.equals, false, 'Comparing against different latest');
     
     var elements = snap.getElements(root);
     equals(elements.length, 4);
     // same(elements[0], root.getElementsByTagName('*')[0]);
-});
+});
+

test/unit/test.snap.node.js

 
     var computedStyle = _getComputedStyle(root);
     
-    console.dir(snap.getSnapshotSettings());
-
     equals(representation.computedStyle['font-size'], computedStyle['fontSize']);
 });