diff --git a/src/PickoneGenerator.js b/src/PickoneGenerator.js index 44af504..0c098f3 100644 --- a/src/PickoneGenerator.js +++ b/src/PickoneGenerator.js @@ -15,7 +15,11 @@ class PickoneGenerator extends Generator { } shrink(item) { - if (this.lastUnwrappedValue === item && this.lastValue.shrink) { + if ( + this.lastUnwrappedValue === item && + this.lastValue && + this.lastValue.shrink + ) { return this.lastValue.shrink(item); } else { return new ConstantGenerator(item); @@ -24,7 +28,9 @@ class PickoneGenerator extends Generator { expand(item) { const expandableItem = - this.lastUnwrappedValue === item && this.lastValue.expand; + this.lastUnwrappedValue === item && + this.lastValue && + this.lastValue.expand; return new WeightedGenerator([ [this, 10], diff --git a/src/PickoneGenerator.spec.js b/src/PickoneGenerator.spec.js index 9f67178..586ecbe 100644 --- a/src/PickoneGenerator.spec.js +++ b/src/PickoneGenerator.spec.js @@ -38,12 +38,20 @@ describe("PickoneGenerator", () => { ConstantGenerator ); }); + + it("does not break when the generator emits falsy values", () => { + new PickoneGenerator([null]).shrink().first(); + }); }); describe("expand", () => { it("returns a new generator that is more likely to pick the given item", () => { expect(generator.expand(5), "to yield items", [7, 5, 7, 5, 5, 5, 4, 0]); }); + + it("does not break when the generator emits falsy values", () => { + new PickoneGenerator([null]).expand().first(); + }); }); describe("when the items contains generators", () => {