Skip to content

Commit

Permalink
More tests, better example in README
Browse files Browse the repository at this point in the history
  • Loading branch information
jieter committed Mar 16, 2015
1 parent f676f79 commit c20061c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
9 changes: 6 additions & 3 deletions README.md
Expand Up @@ -11,10 +11,13 @@ var cloneLayer = require('leaflet-clonelayer');
var layer = L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
}).addTo(map1);
console.log(L.stamp(layer)); // 1

var clonedLayer = cloneLayer(layer);
// Now we can safely add it to another map
cloneLayer.addTo(map2);
var cloned = cloneLayer(layer);
console.log(L.stamp(cloned)); // 2

// Different _leaflet_id, so now we can safely add it to another map
cloned.addTo(map2);
```

## Used in
Expand Down
5 changes: 4 additions & 1 deletion index.js
Expand Up @@ -18,7 +18,10 @@ function cloneLayer(layer) {
}

// Vector layers
if (layer instanceof L.Polygon || layer instanceof L.Rectangle) {
if (layer instanceof L.Rectangle) {
return L.rectangle(layer.getBounds(), options);
}
if (layer instanceof L.Polygon) {
return L.polygon(layer.getLatLngs(), options);
}
if (layer instanceof L.Polyline) {
Expand Down
44 changes: 42 additions & 2 deletions test/spec.js
Expand Up @@ -11,10 +11,16 @@ if (typeof window === 'undefined') {
}


/*
* Clones `layer` and
* - tests if the cloned layer is instanceof `instance`
* - checks if _leaflet_id's are not the same.
* - checks if options are the same.
*/
function testCloneLayer(instance, layer) {
var cloned = cloneLayer(layer);

it('should be a L.TileLayer', function () {
it('should clone to specified instance', function () {
cloned.should.be.an.instanceof(instance);
});
it('should not have the same _leaflet_id', function () {
Expand All @@ -23,6 +29,8 @@ function testCloneLayer(instance, layer) {
it('should have the same options', function () {
layer.options.should.deep.equal(cloned.options);
});
/* eslint no-console: 0 */
console.log(L.stamp(layer), L.stamp(cloned));
return cloned;
}

Expand Down Expand Up @@ -68,7 +76,37 @@ describe('leaflet-cloneLayer', function () {
var cloned = testCloneLayer(L.Polyline, layer);

it('should have the same latlngs', function () {
cloned._latlngs.should.be.deep.equal(layer._latlngs);
cloned.getLatLngs().should.be.deep.equal(layer.getLatLngs());
});
});

describe('L.Polygon', function () {
var latlngs = [[52, 4], [52, 5], [51, 5]];
var options = {
color: '#f00',
fillColor: '#0f0'
};

var layer = L.polygon(latlngs, options);
var cloned = testCloneLayer(L.Polygon, layer);

it('should have the same latlngs', function () {
cloned.getLatLngs().should.be.deep.equal(layer.getLatLngs());
});
});

describe('L.Rectangle', function () {
var bounds = [[4, 51], [5, 52]];
var options = {
color: '#f00',
fillColor: '#0f0'
};

var layer = L.rectangle(bounds, options);
var cloned = testCloneLayer(L.Rectangle, layer);

it('should have the same bounds', function () {
cloned.getBounds().should.be.deep.equal(layer.getBounds());
});
});

Expand Down Expand Up @@ -99,4 +137,6 @@ describe('leaflet-cloneLayer', function () {
cloned.toGeoJSON().should.deep.equal(geojson);
});
});


});

0 comments on commit c20061c

Please sign in to comment.