Repository: yagoferrer/map-tools
Pull request #270 (2015-04-28 05:40:57)

Adds tests for: #269


Patch:
diff --git a/README.md b/README.md
index 55b28f6..42e0f87 100644
--- a/README.md
+++ b/README.md
@@ -261,9 +261,6 @@ You can test this query at this [Live example](http://map-tools.io/examples/data
 Allows you to Tag a specific Marker to later search or update based on that tag value.
 
 You can set a `tags` property to a Marker
-
-[Live example](http://map-tools.io/examples/tags.html)
-
 ```javascript
 map.addMarker({
   lat: 42.5000,
diff --git a/lib/map-tools/addMap.js b/lib/map-tools/addMap.js
index 90a30d8..7146f30 100644
--- a/lib/map-tools/addMap.js
+++ b/lib/map-tools/addMap.js
@@ -117,8 +117,8 @@ module.exports = function (global, that) {
       };
 
       // Set Global Structure
-      global.mapTools.maps[id].markers = global.mapTools.maps[id].markers || {all: {}, tags: {}};
-      global.mapTools.maps[id].json = global.mapTools.maps[id].json || {all: {}};
+      global.mapTools.maps[id].markers = global.mapTools.maps[id].markers || {all: {}, tags: {}, dataChanged: false};
+      global.mapTools.maps[id].json = global.mapTools.maps[id].json || {all: {}, dataChanged: false};
 
       that.markers = global.mapTools.maps[id].markers;
       that.json = global.mapTools.maps[id].json;
diff --git a/lib/map-tools/addMarker.js b/lib/map-tools/addMarker.js
index ff706d7..5a2c9cb 100644
--- a/lib/map-tools/addMarker.js
+++ b/lib/map-tools/addMarker.js
@@ -148,12 +148,14 @@ module.exports = function (global, that) {
           }
         }
 
+        that.markers.dataChanged = true;
         return markers;
       }
 
       return [];
     }
 
+    that.markers.dataChanged = true;
     return addMarker(args, options);
   }
 
diff --git a/lib/map-tools/filter.js b/lib/map-tools/filter.js
index 03aa8ff..28e74ff 100644
--- a/lib/map-tools/filter.js
+++ b/lib/map-tools/filter.js
@@ -18,7 +18,13 @@ module.exports = function(global, that, type) {
       if (dimensionSet.hasOwnProperty(i)) {
         dimension = dimensionSet[i];
 
-        dimension.filterAll();
+        if (that[type].dataChanged === true) {
+          dimension.dispose();
+          addFilter(i);
+        } else {
+          dimension.filterAll();
+        }
+
       }
     }
   }
@@ -52,9 +58,10 @@ module.exports = function(global, that, type) {
       if (dimension === 'tags') {
         return filterByTag(query);
       }
-
     }
 
+    clearAll(that[type].filter);
+
     // Add Crossfilter Dimension if it does not exist.
     if (!that[type].filter[dimension]) {
       addFilter(dimension);
@@ -71,17 +78,14 @@ module.exports = function(global, that, type) {
         query = null;
       }
 
-      clearAll(that[type].filter);
-
       var result = that[type].filter[dimension].filter(query)[order](limit);
 
-
+      that[type].dataChanged = false;
 
       if (limit === 1) {
         return result[0];
       }
 
-
       return result;
 
   }
diff --git a/lib/map-tools/resetMarker.js b/lib/map-tools/resetMarker.js
index 63f8875..a8382e7 100644
--- a/lib/map-tools/resetMarker.js
+++ b/lib/map-tools/resetMarker.js
@@ -23,13 +23,19 @@ module.exports = function(global, that) {
   function resetMarker(args, options) {
     var type = Object.prototype.toString.call(args);
 
+    var result;
+
     if (type === '[object Object]') {
-      return reset(findMarker(args), options);
+      result = reset(findMarker(args), options);
     }
 
     if (type === '[object Array]') {
-      return resetBulk(args, options);
+      result = resetBulk(args, options);
     }
+
+    that.markers.dataChanged = true;
+
+    return result;
   }
 
   function formatOptions(marker, options) {
diff --git a/lib/map-tools/updateMarker.js b/lib/map-tools/updateMarker.js
index bcc0425..5201127 100644
--- a/lib/map-tools/updateMarker.js
+++ b/lib/map-tools/updateMarker.js
@@ -38,6 +38,8 @@ module.exports = function (global, that) {
       countVisible();
     }
 
+    that.markers.dataChanged = true;
+
     return result;
   }
 


Test patch:
diff --git a/test/functional/findMarker.Spec.js b/test/functional/findMarker.Spec.js
new file mode 100644
index 0000000..2878038
--- /dev/null
+++ b/test/functional/findMarker.Spec.js
@@ -0,0 +1,36 @@
+describe('Given the findMarker()', function () {
+
+  var map;
+
+  beforeEach(function () {
+    if (mapTools.maps && mapTools.maps.mymap) {
+      delete mapTools.maps.mymap;
+    }
+
+    map = new mapTools({async: false, id: 'mymap', lat: 41.3833, lng: 2.1833});
+  });
+
+  describe('When finding a Marker by a property and then updating the data', function () {
+
+
+    it('should still find the Marker with the data updates', function () {
+
+      var marker = map.addMarker({lat: 41, lng: 1, visible: false, uid: 'f1'});
+      var result1 = map.findMarker({visible: false});
+      expect(result1[0].uid).to.eql('f1');
+
+      // Data changes!
+      map.updateMarker(marker, {visible: true});
+
+      // When running the same query: it should not find the Marker.
+      var result2 = map.findMarker({visible: false});
+
+      expect(result2.length).to.eql(0);
+
+      var result3 = map.findMarker({visible: true});
+      expect(result3[0].uid).to.eql('f1');
+    });
+
+  });
+
+});
diff --git a/test/unit/resetMarker.Spec.js b/test/unit/resetMarker.Spec.js
index ff18fd5..8fe946a 100644
--- a/test/unit/resetMarker.Spec.js
+++ b/test/unit/resetMarker.Spec.js
@@ -17,7 +17,7 @@ describe('Given the resetMarker() Module', function () {
     };
 
     g.google = window.google;
-     var that = {id: 'mymap'};
+     var that = {id: 'mymap', markers: {dataChanged: false}};
      resetMarker = require('map-tools/resetMarker')(g, that);
   });