diff --git a/src/layer.js b/src/layer.js index 65e478829f8..f713cf6e167 100644 --- a/src/layer.js +++ b/src/layer.js @@ -92,6 +92,8 @@ export default class Layer { assert(props.data[Symbol.iterator], 'data prop must have an iterator'); } + this.props = props; + this.checkProp(props.data, 'data'); this.checkProp(props.id, 'id'); this.checkProp(props.width, 'width'); @@ -103,7 +105,6 @@ export default class Layer { this.checkProp(props.longitude, 'longitude'); this.checkProp(props.zoom, 'zoom'); - this.props = props; this.count = counter++; } /* eslint-enable max-statements */ @@ -455,8 +456,8 @@ export default class Layer { } checkProp(property, propertyName) { - if (!property) { - throw new Error(`Property ${propertyName} undefined in layer ${this.id}`); + if (property === undefined || property === null) { + throw new Error(`Property ${propertyName} undefined in layer ${this.props.id}`); } } diff --git a/test/layer-spec.js b/test/layer-spec.js index 10d8a93f67c..c913a4f7c8a 100644 --- a/test/layer-spec.js +++ b/test/layer-spec.js @@ -16,6 +16,23 @@ const LAYER_PROPS = { zoom: 1, data: [] }; +const LAYER_PROPS_ZEROES = { + id: 'testLayer', + width: 0, + height: 0, + latitude: 0, + longitude: 0, + zoom: 0, + data: [] +}; +const LAYER_PROPS_MISSING = { + id: 'testLayer', + width: 1, + // height: 1, + longitude: 1, + zoom: 1, + data: [] +} test('Layer#constructor', t => { const layer = new Layer(LAYER_PROPS); @@ -23,6 +40,21 @@ test('Layer#constructor', t => { t.end(); }); +test('Layer#constructor with zeroes', t => { + const layer = new Layer(LAYER_PROPS_ZEROES); + t.ok(layer, 'Layer created'); + t.end(); +}); + +test('Layer#constructor with missing props', t => { + t.throws( + () => new Layer(LAYER_PROPS_MISSING), + /Property height undefined in layer testLayer/, + 'Expected missing props to throw an error' + ); + t.end(); +}); + test('Layer#getNumInstances', t => { for (const dataVariant of dataVariants) { const layer = new Layer({