-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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..)