From 6cefc81a1c932abf370614b76f9f84518cc3595b Mon Sep 17 00:00:00 2001 From: Andre Staltz Date: Fri, 22 Jul 2016 19:44:33 +0300 Subject: [PATCH] fix(debug): support usage with no argument given Debug operator should use console.log if no argument if given, but it was crashing with an error. Closes issue #87. --- package.json | 1 + src/core.ts | 2 +- tests/operator/debug.ts | 33 +++++++++++++++++++++++++++++++++ typings.json | 4 +++- 4 files changed, 38 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 549d1af..b21bf49 100644 --- a/package.json +++ b/package.json @@ -55,6 +55,7 @@ "markdox": "^0.1.10", "mkdirp": "^0.5.1", "mocha": "^2.4.5", + "sinon": "^1.16.0", "strip-comments": "^0.4.4", "ts-node": "^0.9.0", "tslint": "^3.6.0", diff --git a/src/core.ts b/src/core.ts index 0e4ce5d..7bfdb20 100644 --- a/src/core.ts +++ b/src/core.ts @@ -373,7 +373,7 @@ export class DebugOperator implements Operator { this.l = ''; if (typeof arg === 'string') { this.l = arg; - } else { + } else if (typeof arg === 'function') { this.s = arg; } } diff --git a/tests/operator/debug.ts b/tests/operator/debug.ts index 46e65d9..abebe60 100644 --- a/tests/operator/debug.ts +++ b/tests/operator/debug.ts @@ -2,8 +2,18 @@ /// import xs, {Stream, MemoryStream} from '../../src/index'; import * as assert from 'assert'; +import * as sinon from 'sinon'; +var sandbox: sinon.SinonSandbox; describe('Stream.prototype.debug', () => { + beforeEach(function () { + sandbox = sinon.sandbox.create(); + }); + + afterEach(function () { + sandbox.restore(); + }); + it('should allow inspecting the operator chain', (done) => { const expected = [0, 1, 2]; const stream = xs.periodic(50).take(3).debug(x => { @@ -23,6 +33,29 @@ describe('Stream.prototype.debug', () => { stream.addListener(listener); }); + it('should use console.log if no argument given', (done) => { + let stub = sandbox.stub(console, 'log'); + + const expected = [0, 1, 2]; + const stream = xs.periodic(50).take(3).debug(); + + assert.doesNotThrow(() => { + stream.addListener({ + next: (x: number) => { + assert.equal(x, expected.shift()); + }, + error: (err) => done(err), + complete: () => { + assert.strictEqual(stub.callCount, 3); + assert.strictEqual(stub.firstCall.args[0], 0); + assert.strictEqual(stub.secondCall.args[0], 1); + assert.strictEqual(stub.thirdCall.args[0], 2); + done(); + }, + }); + }); + }); + it('should return a Stream if input stream is a Stream', (done) => { const input = xs.of(1, 2, 3); assert.strictEqual(input instanceof Stream, true); diff --git a/typings.json b/typings.json index 925986b..813c146 100644 --- a/typings.json +++ b/typings.json @@ -1,7 +1,9 @@ { "name": "xstream", "dependencies": {}, - "devDependencies": {}, + "devDependencies": { + "sinon": "registry:npm/sinon#1.16.0+20160427193336" + }, "globalDependencies": { "es6-promise": "registry:dt/es6-promise#0.0.0+20160423074304" },