Skip to content

Commit

Permalink
Fix Layer#checkProp to only error on undefined/null (#39)
Browse files Browse the repository at this point in the history
* Fix Layer#checkProp to only error on undefined/null

* Fix nits
  • Loading branch information
austinhyde authored and ibgreen committed Jul 1, 2016
1 parent 4dc34fa commit bc02fc9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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 */
Expand Down Expand Up @@ -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}`);
}
}

Expand Down
32 changes: 32 additions & 0 deletions test/layer-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,45 @@ 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);
t.ok(layer, 'Layer created');
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({
Expand Down

0 comments on commit bc02fc9

Please sign in to comment.