-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
48 additions
and
1 deletion.
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,27 @@ | ||
/** | ||
* Creates an observable that immediately terminates with the provided error | ||
* | ||
* @example | ||
* error('error message').subscribe({ error: console.log }) // logs 'error message' | ||
* | ||
* @example | ||
* // rejecting a certain case | ||
* stream | ||
* .filter(specificCase) | ||
* .flatMap(value => Observable.error(value)) | ||
* .subscribe(observer) | ||
* | ||
* @param {*} e Error value | ||
* @return {Observable} New Observable | ||
*/ | ||
function error(e) { | ||
const Constructor = this || Observable; | ||
|
||
return new Constructor(observer => { | ||
observer.error(e); | ||
}); | ||
} | ||
|
||
error._name = 'error'; | ||
|
||
export default error; |
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
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,18 @@ | ||
import { expect } from 'chai'; | ||
import sinon from 'sinon'; | ||
import error from '../src/error'; | ||
import apiCheck from './common/apiCheck'; | ||
|
||
describe('(Creator) error', () => { | ||
apiCheck(error); | ||
|
||
it('should return an Observable', () => { | ||
expect(error(Promise.resolve())).to.be.an.instanceOf(Observable); | ||
}); | ||
|
||
it('should trigger an Observable error event with the provided value', () => { | ||
const observer = { error: sinon.spy() }; | ||
error('error').subscribe(observer); | ||
expect(observer.error).to.have.been.calledWith('error'); | ||
}); | ||
}); |