Skip to content

Commit

Permalink
enforce and test valid range of pitch min/max
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewharvey committed Oct 11, 2019
1 parent 487e08d commit 00b4b69
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/ui/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ type MapOptions = {
const defaultMinZoom = 0;
const defaultMaxZoom = 22;

// the default values, but also the valid range
const defaultMinPitch = 0;
const defaultMaxPitch = 60;

Expand Down Expand Up @@ -344,6 +345,14 @@ class Map extends Camera {
throw new Error(`maxPitch must be greater than or equal to minPitch`);
}

if (options.minPitch != null && options.minPitch < defaultMinPitch) {
throw new Error(`minPitch must be greater than or equal to ${defaultMinPitch}`);
}

if (options.maxPitch != null && options.maxPitch > defaultMaxPitch) {
throw new Error(`maxPitch must be less than or equal to ${defaultMaxPitch}`);
}

const transform = new Transform(options.minZoom, options.maxZoom, options.minPitch, options.maxPitch, options.renderWorldCopies);
super(transform, options);

Expand Down Expand Up @@ -675,6 +684,10 @@ class Map extends Camera {

minPitch = minPitch === null || minPitch === undefined ? defaultMinPitch : minPitch;

if (minPitch < defaultMinPitch) {
throw new Error(`minPitch must be greater than or equal to ${defaultMinPitch}`);
}

if (minPitch >= defaultMinPitch && minPitch <= this.transform.maxPitch) {
this.transform.minPitch = minPitch;
this._update();
Expand Down Expand Up @@ -706,6 +719,10 @@ class Map extends Camera {

maxPitch = maxPitch === null || maxPitch === undefined ? defaultMaxPitch : maxPitch;

if (maxPitch > defaultMaxPitch) {
throw new Error(`maxPitch must be less than or equal to ${defaultMaxPitch}`);
}

if (maxPitch >= this.transform.minPitch) {
this.transform.maxPitch = maxPitch;
this._update();
Expand Down
14 changes: 14 additions & 0 deletions test/unit/ui/map.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,20 @@ test('Map', (t) => {
t.end();
});

t.test('throw on maxPitch greater than valid maxPitch at init', (t) => {
t.throws(() => {
createMap(t, {maxPitch: 90});
}, new Error(`maxPitch must be less than or equal to 60`));
t.end();
});

t.test('throw on minPitch less than valid minPitch at init', (t) => {
t.throws(() => {
createMap(t, {minPitch: -10});
}, new Error(`minPitch must be greater than or equal to 0`));
t.end();
});

t.test('#remove', (t) => {
const map = createMap(t);
t.equal(map.getContainer().childNodes.length, 3);
Expand Down

0 comments on commit 00b4b69

Please sign in to comment.