Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display an error message for impossible values of minZoom and maxZoom #4324

Merged
merged 9 commits into from
Mar 1, 2017
8 changes: 6 additions & 2 deletions src/geo/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ class Transform {
this.tileSize = 512; // constant

this._renderWorldCopies = renderWorldCopies === undefined ? true : renderWorldCopies;
this._minZoom = minZoom || 0;
this._maxZoom = maxZoom || 22;
this.minZoom = minZoom || 0;
this.maxZoom = maxZoom || 22;

this.latRange = [-85.05113, 85.05113];

Expand All @@ -43,13 +43,17 @@ class Transform {
if (this._minZoom === zoom) return;
this._minZoom = zoom;
this.zoom = Math.max(this.zoom, zoom);
if (this._maxZoom != null && this._minZoom > this._maxZoom)
return this.fire('error', new Error('maxZoom must always be greater than minZoom'));
}

get maxZoom() { return this._maxZoom; }
set maxZoom(zoom) {
if (this._maxZoom === zoom) return;
this._maxZoom = zoom;
this.zoom = Math.min(this.zoom, zoom);
if (this._minZoom != null && this._minZoom > this._maxZoom)
return this.fire('error', new Error('maxZoom must always be greater than minZoom'));
}

get worldSize() {
Expand Down
25 changes: 25 additions & 0 deletions test/unit/geo/transform.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,31 @@ test('transform', (t) => {
t.end();
});

t.test('fire on maxZoom smaller than minZoom at init', (t) => {
t.throws(() => {
new Transform(20, 10);
}, Error, 'maxZoom must always be greater than minZoom');
t.end();
});

t.test('fire on maxZoom smaller than minZoom at set maxZoom', (t) => {
t.throws(() => {
const transform = new Transform(10, 15);

transform.maxZoom = 5;
}, Error, 'maxZoom must always be greater than minZoom');
t.end();
});

t.test('fire on maxZoom smaller than minZoom at set minZoom', (t) => {
t.throws(() => {
const transform = new Transform(10, 15);

transform.minZoom = 20;
}, Error, 'maxZoom must always be greater than minZoom');
t.end();
});

t.test('clamps pitch', (t) => {
const transform = new Transform();

Expand Down