diff --git a/reference-implementation/test/byte-length-queuing-strategy.js b/reference-implementation/test/byte-length-queuing-strategy.js index 94fe65947..845a90efd 100644 --- a/reference-implementation/test/byte-length-queuing-strategy.js +++ b/reference-implementation/test/byte-length-queuing-strategy.js @@ -15,6 +15,46 @@ test('Can construct a ByteLengthQueuingStrategy with any value as its high water t.end(); }); +test('ByteLengthQueuingStrategy constructor behaves as expected with wrong arguments', t => { + const highWaterMark = 1; + const highWaterMarkObjectGetter = { + get highWaterMark() { return highWaterMark; }, + }; + const error = new Error("wow!"); + const highWaterMarkObjectGetterThrowing = { + get highWaterMark() { throw error; }, + }; + t.throws(() => new ByteLengthQueuingStrategy(), /TypeError/, 'construction fails with undefined'); + t.throws(() => new ByteLengthQueuingStrategy(null), /TypeError/, 'construction fails with null'); + t.doesNotThrow(() => new ByteLengthQueuingStrategy('potato'), 'construction succeeds with a random non-object type'); + t.doesNotThrow(() => new ByteLengthQueuingStrategy({}), 'construction succeeds with an object without hwm property'); + t.doesNotThrow(() => new ByteLengthQueuingStrategy(highWaterMarkObjectGetter), 'construction succeeds with an object with a hwm getter'); + t.throws(() => new ByteLengthQueuingStrategy(highWaterMarkObjectGetterThrowing), /wow/, 'construction fails with the error thrown by the getter'); + + t.end(); +}); + +test('ByteLengthQueuingStrategy size behaves as expected with wrong arguments', t => { + const size = 1024; + const chunk = { byteLength: size }; + const chunkGetter = { + get byteLength() { return size; }, + } + const error = new Error("wow!"); + const chunkGetterThrowing = { + get byteLength() { throw error; }, + } + t.throws(() => ByteLengthQueuingStrategy.prototype.size(), /TypeError/, 'size fails with undefined'); + t.throws(() => ByteLengthQueuingStrategy.prototype.size(null), /TypeError/, 'size fails with null'); + t.equal(ByteLengthQueuingStrategy.prototype.size('potato'), undefined, 'size succeeds with undefined with a random non-object type'); + t.equal(ByteLengthQueuingStrategy.prototype.size({}), undefined, 'size succeeds with undefined with an object without hwm property'); + t.equal(ByteLengthQueuingStrategy.prototype.size(chunk), size, 'size succeeds with the right amount with an object with a hwm'); + t.equal(ByteLengthQueuingStrategy.prototype.size(chunkGetter), size, 'size succeeds with the right amount with an object with a hwm getter'); + t.throws(() => ByteLengthQueuingStrategy.prototype.size(chunkGetterThrowing), /wow/, 'size fails with the error thrown by the getter'); + + t.end(); +}); + test('ByteLengthQueuingStrategy instances have the correct properties', t => { const strategy = new ByteLengthQueuingStrategy({ highWaterMark: 4 }); diff --git a/reference-implementation/test/count-queuing-strategy.js b/reference-implementation/test/count-queuing-strategy.js index ff2d027b5..13c9efa49 100644 --- a/reference-implementation/test/count-queuing-strategy.js +++ b/reference-implementation/test/count-queuing-strategy.js @@ -15,6 +15,46 @@ test('Can construct a CountQueuingStrategy with any value as its high water mark t.end(); }); +test('CountQueuingStrategy constructor behaves as expected with wrong arguments', t => { + const highWaterMark = 1; + const highWaterMarkObjectGetter = { + get highWaterMark() { return highWaterMark; }, + }; + const error = new Error("wow!"); + const highWaterMarkObjectGetterThrowing = { + get highWaterMark() { throw error; }, + }; + t.throws(() => new CountQueuingStrategy(), /TypeError/, 'construction fails with undefined'); + t.throws(() => new CountQueuingStrategy(null), /TypeError/, 'construction fails with null'); + t.doesNotThrow(() => new CountQueuingStrategy('potato'), 'construction succeeds with a random non-object type'); + t.doesNotThrow(() => new CountQueuingStrategy({}), 'construction succeeds with an object without hwm property'); + t.doesNotThrow(() => new CountQueuingStrategy(highWaterMarkObjectGetter), 'construction succeeds with an object with a hwm getter'); + t.throws(() => new CountQueuingStrategy(highWaterMarkObjectGetterThrowing), /wow/, 'construction fails with the error thrown by the getter'); + + t.end(); +}); + +test('CountQueuingStrategy size behaves as expected with wrong arguments', t => { + const size = 1024; + const chunk = { byteLength: size }; + const chunkGetter = { + get byteLength() { return size; }, + } + const error = new Error("wow!"); + const chunkGetterThrowing = { + get byteLength() { throw error; }, + } + t.equal(CountQueuingStrategy.prototype.size(), 1, 'size returns 1 with undefined'); + t.equal(CountQueuingStrategy.prototype.size(null), 1, 'size returns 1 with null'); + t.equal(CountQueuingStrategy.prototype.size('potato'), 1, 'size returns 1 with non-object type'); + t.equal(CountQueuingStrategy.prototype.size({}), 1, 'size returns 1 with empty object'); + t.equal(CountQueuingStrategy.prototype.size(chunk), 1, 'size returns 1 with a chunk'); + t.equal(CountQueuingStrategy.prototype.size(chunkGetter), 1, 'size returns 1 with chunk getter'); + t.equal(CountQueuingStrategy.prototype.size(chunkGetterThrowing), 1, 'size returns 1 with chunk getter that throws'); + + t.end(); +}); + test('CountQueuingStrategy instances have the correct properties', t => { const strategy = new CountQueuingStrategy({ highWaterMark: 4 });