Skip to content

Commit

Permalink
Unit tests for Leaflet#4257, attributionControl layeradd/layerremove
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanSanchez committed Feb 18, 2016
1 parent e0c17bf commit 6639782
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
50 changes: 50 additions & 0 deletions spec/suites/control/Control.AttributionSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,54 @@ describe("Control.Attribution", function () {
});
});

describe('on layer add/remove', function () {
it('changes text', function () {
var fooLayer = new L.Layer();
var barLayer = new L.Layer();
var bazLayer = new L.Layer();
fooLayer.getAttribution = function () { return 'foo'; };
barLayer.getAttribution = function () { return 'bar'; };
bazLayer.getAttribution = function () { return 'baz'; };

expect(container.innerHTML).to.eql('prefix');
map.addLayer(fooLayer);
expect(container.innerHTML).to.eql('prefix | foo');
map.addLayer(barLayer);
expect(container.innerHTML).to.eql('prefix | foo, bar');
map.addLayer(bazLayer);
expect(container.innerHTML).to.eql('prefix | foo, bar, baz');

map.removeLayer(fooLayer);
expect(container.innerHTML).to.eql('prefix | bar, baz');
map.removeLayer(barLayer);
expect(container.innerHTML).to.eql('prefix | baz');
map.removeLayer(bazLayer);
expect(container.innerHTML).to.eql('prefix');
});

it('keeps count of duplicated attributions', function () {
var fooLayer = new L.Layer();
var fo2Layer = new L.Layer();
var fo3Layer = new L.Layer();
fooLayer.getAttribution = function () { return 'foo'; };
fo2Layer.getAttribution = function () { return 'foo'; };
fo3Layer.getAttribution = function () { return 'foo'; };

expect(container.innerHTML).to.eql('prefix');
map.addLayer(fooLayer);
expect(container.innerHTML).to.eql('prefix | foo');
map.addLayer(fo2Layer);
expect(container.innerHTML).to.eql('prefix | foo');
map.addLayer(fo3Layer);
expect(container.innerHTML).to.eql('prefix | foo');

map.removeLayer(fooLayer);
expect(container.innerHTML).to.eql('prefix | foo');
map.removeLayer(fo2Layer);
expect(container.innerHTML).to.eql('prefix | foo');
map.removeLayer(fo3Layer);
expect(container.innerHTML).to.eql('prefix');
});
});

});
4 changes: 4 additions & 0 deletions src/layer/Layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ L.Map.include({
layer.beforeAdd(this);
}

if (layer.getAttribution && this.attributionControl) {
this.attributionControl.addAttribution(layer.getAttribution());
}

this.whenReady(layer._layerAdd, layer);

return this;
Expand Down

0 comments on commit 6639782

Please sign in to comment.