Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,11 @@ MapboxGeocoder.prototype = {
*/
addTo: function(container){

function addToExistingContainer (container) {
const el = this.onAdd(); //returns the input elements, which are then added to the requested html container
function addToExistingContainer (geocoder, container) {
if (!document.body.contains(container)) {
throw new Error("Element provided to #addTo() exists, but is not in the DOM")
}
const el = geocoder.onAdd(); //returns the input elements, which are then added to the requested html container
container.appendChild(el);
}

Expand All @@ -122,7 +125,7 @@ MapboxGeocoder.prototype = {
}
// if the container is an HTMLElement, then set the parent to be that element
else if (container instanceof HTMLElement) {
addToExistingContainer(container).bind(this);
addToExistingContainer(this, container);
}
// if the container is a string, treat it as a CSS query
else if (typeof container == 'string'){
Expand All @@ -135,7 +138,7 @@ MapboxGeocoder.prototype = {
throw new Error("Geocoder can only be added to a single html element")
}

addToExistingContainer(parent[0]);
addToExistingContainer(this, parent[0]);
}else{
throw new Error("Error: addTo must be a mapbox-gl-js map, an html element, or a CSS selector query for a single html element")
}
Expand Down
52 changes: 35 additions & 17 deletions test/test.ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ test('Geocoder#inputControl', function(tt) {
tt.end();
});

test('Geocoder--no map', function(tt) {
test('Geocoder#addTo(String) -- no map', function(tt) {
var container, geocoder;

var changeEvent = document.createEvent('HTMLEvents');
Expand Down Expand Up @@ -431,7 +431,6 @@ test('Geocoder--no map', function(tt) {
t.end();
});


tt.test('input works without a map', function(t) {
setup({
types: 'place'
Expand Down Expand Up @@ -482,7 +481,39 @@ test('Geocoder--no map', function(tt) {
});
});

test('Geocoder#addTo', function(tt) {

test('Geocoder#addTo(HTMLElement) -- no map', function(tt) {
var container, geocoder;

var changeEvent = document.createEvent('HTMLEvents');
changeEvent.initEvent('change', true, false);

var clickEvent = document.createEvent('HTMLEvents');
clickEvent.initEvent('click', true, false);

function setup(opts) {
opts = opts || {};
opts.accessToken = mapboxgl.accessToken;
opts.enableEventLogging = false;
container = document.createElement('div');
container.className = "notAMap"
document.body.appendChild(container)
geocoder = new MapboxGeocoder(opts);
geocoder.addTo(".notAMap")
}

tt.test('result was added to container', (t)=>{
setup();
const geocoderRef = document.getElementsByClassName("mapboxgl-ctrl-geocoder");
t.ok(Object.keys(geocoderRef).length, "A geocoder exists in the document");
const containerChildRef = container.getElementsByClassName("mapboxgl-ctrl-geocoder");
t.ok(Object.keys(containerChildRef).length, "A geocoder exists as a child to the specified element");
container.remove();
t.end();
});
});

test('Geocoder#addTo(mapboxgl.Map)', function(tt) {
var container, geocoder;

tt.test('add to an existing map', (t)=>{
Expand Down Expand Up @@ -515,26 +546,13 @@ test('Geocoder#addTo', function(tt) {
opts.accessToken = mapboxgl.accessToken;
opts.enableEventLogging = false;
container = document.createElement('div');
container.className = "notAMapEither"
document.body.appendChild(container)
geocoder = new MapboxGeocoder(opts);
geocoder.addTo(document.querySelectorAll(".notAMapEither"));
geocoder.addTo(container);
t.ok(Object.keys(container.getElementsByClassName("mapboxgl-ctrl-geocoder--input")).length, 'geocoder exists when added to an html element')
t.end();
});

tt.test('throws if added to an unknown element', (t)=>{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, I'm not sure what this unit test is for. @scottsfarley93 added it in 7f53ecf#diff-23a1ea5628fe499863040c05a9d338aaR440 but as far as I can tell it's checking to make sure the element is a map, but in that PR at #295 support was added to allow adding the geocoder to a non-map element for a mapless geocoder.

const opts = {}
opts.accessToken = mapboxgl.accessToken;
opts.enableEventLogging = false;
container = document.createElement('div');
container.className = "notAMap"
document.body.appendChild(container)
geocoder = new MapboxGeocoder(opts);
t.throws(()=>{geocoder.addTo(container)}, 'addTo throws if added to an unknown element');
t.end();
});

tt.test('throws if the element cannot be found', (t)=>{
const opts = {}
opts.accessToken = mapboxgl.accessToken;
Expand Down