spec-js/observables/fromEventPattern-spec.js
- "use strict";
- var chai_1 = require('chai');
- var sinon = require('sinon');
- var Rx = require('../../dist/package/Rx');
- var noop_1 = require('../../dist/package/util/noop');
- var Observable = Rx.Observable;
- /** @test {fromEventPattern} */
- describe('Observable.fromEventPattern', function () {
- asDiagram('fromEventPattern(addHandler, removeHandler)')('should create an observable from the handler API', function () {
- function addHandler(h) {
- Observable.timer(50, 20, rxTestScheduler)
- .mapTo('ev')
- .take(2)
- .concat(Observable.never())
- .subscribe(h);
- }
- var e1 = Observable.fromEventPattern(addHandler);
- var expected = '-----x-x---';
- expectObservable(e1).toBe(expected, { x: 'ev' });
- });
- it('should call addHandler on subscription', function () {
- var addHandler = sinon.spy();
- Observable.fromEventPattern(addHandler, noop_1.noop).subscribe(noop_1.noop);
- var call = addHandler.getCall(0);
- chai_1.expect(addHandler).calledOnce;
- chai_1.expect(call.args[0]).to.be.a('function');
- });
- it('should call removeHandler on unsubscription', function () {
- var removeHandler = sinon.spy();
- Observable.fromEventPattern(noop_1.noop, removeHandler).subscribe(noop_1.noop).unsubscribe();
- var call = removeHandler.getCall(0);
- chai_1.expect(removeHandler).calledOnce;
- chai_1.expect(call.args[0]).to.be.a('function');
- });
- it('should work without optional removeHandler', function () {
- var addHandler = sinon.spy();
- Observable.fromEventPattern(addHandler).subscribe(noop_1.noop);
- chai_1.expect(addHandler).calledOnce;
- });
- it('should deliver return value of addHandler to removeHandler as signal', function () {
- var expected = { signal: true };
- var addHandler = function () { return expected; };
- var removeHandler = sinon.spy();
- Observable.fromEventPattern(addHandler, removeHandler).subscribe(noop_1.noop).unsubscribe();
- var call = removeHandler.getCall(0);
- chai_1.expect(call).calledWith(sinon.match.any, expected);
- });
- it('should send errors in addHandler down the error path', function (done) {
- Observable.fromEventPattern(function (h) {
- throw 'bad';
- }, noop_1.noop).subscribe(function () { return done(new Error('should not be called')); }, function (err) {
- chai_1.expect(err).to.equal('bad');
- done();
- }, function () { return done(new Error('should not be called')); });
- });
- it('should accept a selector that maps outgoing values', function (done) {
- var target;
- var trigger = function () {
- var args = [];
- for (var _i = 0; _i < arguments.length; _i++) {
- args[_i - 0] = arguments[_i];
- }
- if (target) {
- target.apply(null, arguments);
- }
- };
- var addHandler = function (handler) {
- target = handler;
- };
- var removeHandler = function (handler) {
- target = null;
- };
- var selector = function (a, b) {
- return a + b + '!';
- };
- Observable.fromEventPattern(addHandler, removeHandler, selector).take(1)
- .subscribe(function (x) {
- chai_1.expect(x).to.equal('testme!');
- }, function (err) {
- done(new Error('should not be called'));
- }, function () {
- done();
- });
- trigger('test', 'me');
- });
- it('should send errors in the selector down the error path', function (done) {
- var target;
- var trigger = function (value) {
- if (target) {
- target(value);
- }
- };
- var addHandler = function (handler) {
- target = handler;
- };
- var removeHandler = function (handler) {
- target = null;
- };
- var selector = function (x) {
- throw 'bad';
- };
- Observable.fromEventPattern(addHandler, removeHandler, selector)
- .subscribe(function (x) {
- done(new Error('should not be called'));
- }, function (err) {
- chai_1.expect(err).to.equal('bad');
- done();
- }, function () {
- done(new Error('should not be called'));
- });
- trigger('test');
- });
- });
- //# sourceMappingURL=fromEventPattern-spec.js.map