Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(bindCallback): rename fromCallback, add tests, and fix/refactor #881

Merged
merged 3 commits into from
Dec 4, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
246 changes: 246 additions & 0 deletions spec/observables/bindCallback-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
/* globals describe, it, expect, rxTestScheduler */
var Rx = require('../../dist/cjs/Rx');
var Observable = Rx.Observable;

describe('Observable.bindCallback', function () {
describe('when not scheduled', function () {
it('should emit one value from a callback', function () {
function callback(datum, cb) {
cb(datum);
}
var boundCallback = Observable.bindCallback(callback);
var results = [];

boundCallback(42)
.subscribe(function (x) {
results.push(x);
}, null, function () {
results.push('done');
});

expect(results).toEqual([42, 'done']);
});

it('should emit one value chosen by a selector', function () {
function callback(datum, cb) {
cb(datum);
}
var boundCallback = Observable.bindCallback(callback, function (datum) { return datum; });
var results = [];

boundCallback(42)
.subscribe(function (x) {
results.push(x);
}, null, function () {
results.push('done');
});

expect(results).toEqual([42, 'done']);
});

it('should emit an error when the selector throws', function () {
function callback(cb) {
cb(42);
}
var boundCallback = Observable.bindCallback(callback, function (err) { throw new Error('Yikes!'); });
var results = [];

boundCallback()
.subscribe(function () {
throw 'should not next';
}, function (err) {
results.push(err);
}, function () {
throw 'should not complete';
});

expect(results).toEqual([new Error('Yikes!')]);
});

it('should not emit, throw or complete if immediately unsubscribed', function (done) {
var nextSpy = jasmine.createSpy('next');
var throwSpy = jasmine.createSpy('throw');
var completeSpy = jasmine.createSpy('complete');
var timeout;
function callback(datum, cb) {
// Need to cb async in order for the unsub to trigger
timeout = setTimeout(function () {
cb(datum);
});
}
var subscription = Observable.bindCallback(callback)(42)
.subscribe(nextSpy, throwSpy, completeSpy);
subscription.unsubscribe();

setTimeout(function () {
expect(nextSpy).not.toHaveBeenCalled();
expect(throwSpy).not.toHaveBeenCalled();
expect(completeSpy).not.toHaveBeenCalled();

clearTimeout(timeout);
done();
});
});
});

describe('when scheduled', function () {
it('should emit one value from a callback', function () {
function callback(datum, cb) {
cb(datum);
}
var boundCallback = Observable.bindCallback(callback, null, rxTestScheduler);
var results = [];

boundCallback(42)
.subscribe(function (x) {
results.push(x);
}, null, function () {
results.push('done');
});

rxTestScheduler.flush();

expect(results).toEqual([42, 'done']);
});

it('should error if callback throws', function () {
function callback(datum, cb) {
throw new Error('haha no callback for you');
}
var boundCallback = Observable.bindCallback(callback, null, rxTestScheduler);
var results = [];

boundCallback(42)
.subscribe(function (x) {
throw 'should not next';
}, function (err) {
results.push(err);
}, function () {
throw 'should not complete';
});

rxTestScheduler.flush();

expect(results).toEqual([new Error('haha no callback for you')]);
});

it('should error if selector throws', function () {
function callback(datum, cb) {
cb(datum);
}
function selector() {
throw new Error('what? a selector? I don\'t think so');
}
var boundCallback = Observable.bindCallback(callback, selector, rxTestScheduler);
var results = [];

boundCallback(42)
.subscribe(function (x) {
throw 'should not next';
}, function (err) {
results.push(err);
}, function () {
throw 'should not complete';
});

rxTestScheduler.flush();

expect(results).toEqual([new Error('what? a selector? I don\'t think so')]);
});

it('should use a selector', function () {
function callback(datum, cb) {
cb(datum);
}
function selector(x) {
return x + '!!!';
}
var boundCallback = Observable.bindCallback(callback, selector, rxTestScheduler);
var results = [];

boundCallback(42)
.subscribe(function (x) {
results.push(x);
}, null, function () {
results.push('done');
});

rxTestScheduler.flush();

expect(results).toEqual(['42!!!', 'done']);
});
});

it('should pass multiple inner arguments as an array', function () {
function callback(datum, cb) {
cb(datum, 1, 2, 3);
}
var boundCallback = Observable.bindCallback(callback, null, rxTestScheduler);
var results = [];

boundCallback(42)
.subscribe(function (x) {
results.push(x);
}, null, function () {
results.push('done');
});

rxTestScheduler.flush();

expect(results).toEqual([[42, 1, 2, 3], 'done']);
});

it('should pass multiple inner arguments to the selector if there is one', function () {
function callback(datum, cb) {
cb(datum, 1, 2, 3);
}
function selector(a, b, c, d) {
expect([a, b, c, d]).toEqual([42, 1, 2, 3]);
return a + b + c + d;
}
var boundCallback = Observable.bindCallback(callback, selector, rxTestScheduler);
var results = [];

boundCallback(42)
.subscribe(function (x) {
results.push(x);
}, null, function () {
results.push('done');
});

rxTestScheduler.flush();

expect(results).toEqual([48, 'done']);
});

it('should cache value for next subscription and not call callbackFunc again', function () {
var calls = 0;
function callback(datum, cb) {
calls++;
cb(datum);
}
var boundCallback = Observable.bindCallback(callback, null, rxTestScheduler);
var results1 = [];
var results2 = [];

var source = boundCallback(42);

source.subscribe(function (x) {
results1.push(x);
}, null, function () {
results1.push('done');
});

source.subscribe(function (x) {
results2.push(x);
}, null, function () {
results2.push('done');
});

rxTestScheduler.flush();

expect(calls).toBe(1);
expect(results1).toEqual([42, 'done']);
expect(results2).toEqual([42, 'done']);
});
});
97 changes: 0 additions & 97 deletions spec/observables/from-callback-spec.js

This file was deleted.

38 changes: 38 additions & 0 deletions spec/observables/jasmine-is-weird-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* globals describe, it, expect, rxTestScheduler */
var Rx = require('../../dist/cjs/Rx');
var Observable = Rx.Observable;

/**
* I'm starting this file to collect tests that when put in other files break jasmine for
* no apparent reason. It seems like maybe we should move off of jasmine, but moving >1700 tests
* sounds really gross, so I don't want to do that...
*/
describe('jasmine is weird', function () {
describe('bindCallback', function () {
// HACK: If you move this test under the bindCallback-spec.js file, it will arbitrarily
// break one bufferWhen-spec.js test.
it('should not even call the callbackFn if immediately unsubscribed', function () {
var calls = 0;
function callback(datum, cb) {
calls++;
cb(datum);
}
var boundCallback = Observable.bindCallback(callback, null, rxTestScheduler);
var results1 = [];

var source = boundCallback(42);

var subscription = source.subscribe(function (x) {
results1.push(x);
}, null, function () {
results1.push('done');
});

subscription.unsubscribe();

rxTestScheduler.flush();

expect(calls).toBe(0);
});
});
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Jasmine BDD style looks fairly similar to Mocha BDD. We could at some point try migrating from Jasmine to Mocha. At least we wouldn't need to rewrite/adapt every single test, it would be more a matter of fixing test-helper.js and the assertion tools for Mocha.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious if any other uses jasmine would experience similar before making decisions of migrating to another frameworks. + if mocha does have any known issues might need to consider (course earlier would be better since there are already more than > 1000 test cases, though effort wouldn't be huge still require some..)

Loading