Skip to content

Commit

Permalink
test(Subject): add tests for subjects used as Observer
Browse files Browse the repository at this point in the history
Add tests for Subject, BehaviorSubject and ReplaySubject to check whether they can be used as plain
observers when given to Observable.subscribe().

Related to issue #825.
  • Loading branch information
staltz authored and kwonoj committed Nov 30, 2015
1 parent a687695 commit 3c72bca
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
16 changes: 16 additions & 0 deletions spec/subject-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -471,4 +471,20 @@ describe('Subject', function () {
expect(output).toEqual([1,2,3,4,5]);
expect(outputComplete).toBe(true);
});

it('should be an Observer which can be given to Observable.subscribe', function (done) {
var source = Observable.of(1, 2, 3, 4, 5);
var subject = new Subject();
var expected = [1, 2, 3, 4, 5];

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

source.subscribe(subject);
});
});
17 changes: 17 additions & 0 deletions spec/subjects/behavior-subject-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var Rx = require('../../dist/cjs/Rx');

var BehaviorSubject = Rx.BehaviorSubject;
var nextTick = Rx.Scheduler.nextTick;
var Observable = Rx.Observable;

describe('BehaviorSubject', function () {
it('should extend Subject', function (done) {
Expand Down Expand Up @@ -115,4 +116,20 @@ describe('BehaviorSubject', function () {
)).toBe(sourceTemplate);
expectObservable(subscriber1).toBe(expected1);
});

it('should be an Observer which can be given to Observable.subscribe', function (done) {
var source = Observable.of(1, 2, 3, 4, 5);
var subject = new BehaviorSubject(0);
var expected = [0, 1, 2, 3, 4, 5];

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

source.subscribe(subject);
});
});
17 changes: 17 additions & 0 deletions spec/subjects/replay-subject-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var Rx = require('../../dist/cjs/Rx');

var ReplaySubject = Rx.ReplaySubject;
var nextTick = Rx.Scheduler.nextTick;
var Observable = Rx.Observable;

describe('ReplaySubject', function () {
it('should extend Subject', function (done) {
Expand Down Expand Up @@ -206,4 +207,20 @@ describe('ReplaySubject', function () {
expectObservable(subscriber1).toBe(expected1);
});
});

it('should be an Observer which can be given to Observable.subscribe', function (done) {
var source = Observable.of(1, 2, 3, 4, 5);
var subject = new ReplaySubject(3);
var expected = [3, 4, 5];

source.subscribe(subject);

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

0 comments on commit 3c72bca

Please sign in to comment.