Skip to content

Commit

Permalink
add more test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewharvey committed Dec 16, 2016
1 parent 496b3d5 commit 9718cb7
Showing 1 changed file with 130 additions and 0 deletions.
130 changes: 130 additions & 0 deletions test/js/ui/control/geolocate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,133 @@ test('GeolocateControl no watching map centered on geolocation', (t) => {
geolocation.send({latitude: 10, longitude: 20, accuracy: 30});
});
});

test('GeolocateControl watching map updates recenter on location with marker', (t) => {
const map = createMap();
const geolocate = new GeolocateControl({
watchPosition: true,
showMarker: true,
markerPaintProperties: {
'circle-radius': 10,
'circle-color': '#000',
'circle-stroke-color': '#fff',
'circle-stroke-width': 2
},
markerStalePaintProperties: {
'circle-color': '#f00',
}
});
map.addControl(geolocate);

const click = new window.Event('click');

geolocate.on('ready', () => {
map.once('moveend', () => {
t.deepEqual(map.getCenter(), { lat: 10, lng: 20 }, 'map centered on location after 1st update');
t.ok(map.getLayer('_geolocate-control-marker'), 'has marker layer');
t.equals(map.getPaintProperty('_geolocate-control-marker', 'circle-color'), '#000', 'markerPaintProperty circle-color');
map.once('moveend', () => {
t.deepEqual(map.getCenter(), { lat: 40, lng: 50 }, 'map centered on location after 2nd update');
geolocate.once('error', () => {
t.equals(map.getPaintProperty('_geolocate-control-marker', 'circle-color'), '#f00', 'markerStalePaintProperty circle-color');
t.end();
});
geolocation.changeError({code: 1, message: 'message'});
});
geolocation.change({latitude: 40, longitude: 50, accuracy: 60});
});
geolocate._geolocateButton.dispatchEvent(click);
geolocation.send({latitude: 10, longitude: 20, accuracy: 30});
});
});

test('GeolocateControl watching map background event', (t) => {
const map = createMap();
const geolocate = new GeolocateControl({
watchPosition: true
});
map.addControl(geolocate);

const click = new window.Event('click');

// when the geolocate control is ready
geolocate.once('ready', () => {
map.once('moveend', () => {
geolocate.once('background', () => {
t.end();
});

// manually pan the map away from the geolocation position which should trigger the 'background' event above
map.jumpTo({
center: [10, 5]
});
});
// click the button to activate it into the enabled watch state
geolocate._geolocateButton.dispatchEvent(click);
// send through a location update which should reposition the map and trigger the 'moveend' event above
geolocation.send({latitude: 10, longitude: 20, accuracy: 30});
});
});

test('GeolocateControl watching map background state', (t) => {
const map = createMap();
const geolocate = new GeolocateControl({
watchPosition: true
});
map.addControl(geolocate);

const click = new window.Event('click');

// when the geolocate control is ready
geolocate.once('ready', () => {
map.once('moveend', () => {
map.once('moveend', () => {
geolocate.once('geolocate', () => {
t.deepEquals(map.getCenter(), {lng: 10, lat: 5}, 'camera not changed after geolocation update in background state');
t.end();
});
// update the geolocation position, since we are in background state when 'geolocate' is triggered above, the camera shouldn't have changed
geolocation.change({latitude: 0, longitude: 0, accuracy: 10});
});

// manually pan the map away from the geolocation position which should trigger the 'moveend' event above
map.jumpTo({
center: [10, 5]
});
});
// click the button to activate it into the enabled watch state
geolocate._geolocateButton.dispatchEvent(click);
// send through a location update which should reposition the map and trigger the 'moveend' event above
geolocation.send({latitude: 10, longitude: 20, accuracy: 30});
});
});

test('GeolocateControl active_lock event', (t) => {
console.log('start final test');
const map = createMap();
const geolocate = new GeolocateControl({
watchPosition: true
});
map.addControl(geolocate);

const click = new window.Event('click');

geolocate.once('ready', () => {
geolocate.once('active_lock', () => {
geolocate.once('background', () => {
geolocate.once('active_lock', () => {
t.end();
});
// click the geolocate control button again which should transition back to active_lock state
geolocate._geolocateButton.dispatchEvent(click);
});

// manually pan the map away from the geolocation position which should trigger the 'background' event above
map.jumpTo({
center: [10, 5]
});
});
geolocate._geolocateButton.dispatchEvent(click);
geolocation.send({latitude: 10, longitude: 20, accuracy: 30, timestamp: 40});
});
});

0 comments on commit 9718cb7

Please sign in to comment.