Skip to content

Commit

Permalink
test(Observable): add tests for important lift() features
Browse files Browse the repository at this point in the history
Add tests for Observable to verify that the most important codebase-wide features brought by the
lift architecture are satisfied.

Refers to discussion in #60.
  • Loading branch information
staltz authored and benlesh committed Jan 27, 2016
1 parent 7004c26 commit 90df9e2
Showing 1 changed file with 102 additions and 0 deletions.
102 changes: 102 additions & 0 deletions spec/Observable-spec.js
Expand Up @@ -295,3 +295,105 @@ describe('Observable.create', function () {
expect(called).toBe(true);
});
});

describe('Observable.lift', function () {
it('should be overrideable in a custom Observable type that composes', function (done) {
function MyCustomObservable() {
Observable.apply(this, arguments);
}
MyCustomObservable.prototype = Object.create(Observable.prototype);
MyCustomObservable.prototype.constructor = MyCustomObservable;
MyCustomObservable.prototype.lift = function (operator) {
var obs = new MyCustomObservable();
obs.source = this;
obs.operator = operator;
return obs;
};

var result = new MyCustomObservable(function (observer) {
observer.next(1);
observer.next(2);
observer.next(3);
observer.complete();
}).map(function (x) { return 10 * x; });

expect(result instanceof MyCustomObservable).toBe(true);

var expected = [10, 20, 30];

result.subscribe(
function (x) {
expect(x).toBe(expected.shift());
},
done.fail,
done);
});

it('should allow injecting behaviors into all subscribers in an operator ' +
'chain when overriden', function (done) {
// The custom Observable
function LogObservable() {
Observable.apply(this, arguments);
}
LogObservable.prototype = Object.create(Observable.prototype);
LogObservable.prototype.constructor = LogObservable;
LogObservable.prototype.lift = function (operator) {
var obs = new LogObservable();
obs.source = this;
obs.operator = new LogOperator(operator);
return obs;
};

// The custom Operator
function LogOperator(childOperator) {
this.childOperator = childOperator;
}
LogOperator.prototype.call = function (subscriber) {
return this.childOperator.call(new LogSubscriber(subscriber));
};

// The custom Subscriber
var log = [];
function LogSubscriber() {
Subscriber.apply(this, arguments);
}
LogSubscriber.prototype = Object.create(Subscriber.prototype);
LogSubscriber.prototype.constructor = LogSubscriber;
LogSubscriber.prototype.next = function (x) {
log.push('next ' + x);
this.destination.next(x);
};

// Use the LogObservable
var result = new LogObservable(function (observer) {
observer.next(1);
observer.next(2);
observer.next(3);
observer.complete();
})
.map(function (x) { return 10 * x; })
.filter(function (x) { return x > 15 })
.count();

expect(result instanceof LogObservable).toBe(true);

var expected = [2];

result.subscribe(
function (x) {
expect(x).toBe(expected.shift());
},
done.fail,
function () {
expect(log).toEqual([
'next 10', // map
'next 20', // map
'next 20', // filter
'next 30', // map
'next 30', // filter
'next 2' // count
]);
done();
});
});
});

0 comments on commit 90df9e2

Please sign in to comment.