Skip to content

Commit

Permalink
Merge branch 'test-subscriber-observer-instance' of https://github.co…
Browse files Browse the repository at this point in the history
…m/oscarlorentzon/rxjs into oscarlorentzon-test-subscriber-observer-instance
  • Loading branch information
trxcllnt committed Jan 16, 2017
2 parents 31cf2bf + 30b309d commit 6a40d7b
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 0 deletions.
74 changes: 74 additions & 0 deletions spec/Observable-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,80 @@ describe('Observable', () => {
source.subscribe();
});

it('should work when subscribe is called with no arguments and other subscription with no arguments completes', (done: MochaDone) => {
let nextCalled = false;
const source = new Observable((subscriber: Rx.Subscriber<string>) => {
const id = setTimeout(() => {
subscriber.next('foo');
subscriber.complete();
nextCalled = true;
}, 1);
return () => {
clearTimeout(id);
};
});

source.subscribe();

Observable.empty().subscribe();

setTimeout(() => {
let err;
let errHappened = false;
try {
expect(nextCalled).to.be.true;
} catch (e) {
err = e;
errHappened = true;
} finally {
if (!errHappened) {
done();
} else {
done(err);
}
}
}, 100);
});

it('should work when subscribe is called with observer and other subscription with same observer instance completes', (done: MochaDone) => {
let nextCalled = false;
const source = new Observable((subscriber: Rx.Subscriber<string>) => {
const id = setTimeout(() => {
subscriber.next('foo');
subscriber.complete();
nextCalled = true;
}, 1);
return () => {
clearTimeout(id);
};
});

let observer = {
next: function () { /*noop*/ }
};

source.subscribe(observer);

Observable.empty().subscribe(observer);

setTimeout(() => {
let err;
let errHappened = false;
try {
expect(nextCalled).to.be.true;
} catch (e) {
err = e;
errHappened = true;
} finally {
if (!errHappened) {
done();
} else {
done(err);
}
}
}, 100);
});

it('should run unsubscription logic when an error is sent synchronously and subscribe is called with no arguments', () => {
let unsubscribeCalled = false;
const source = new Observable((subscriber: Rx.Subscriber<string>) => {
Expand Down
14 changes: 14 additions & 0 deletions spec/Subscriber-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,18 @@ describe('Subscriber', () => {
expect(times).to.equal(2);
expect(completeCalled).to.be.false;
});

it('should not be closed when other subscriber with same observer instance completes', () => {
let observer = {
next: function () { /*noop*/ }
};

const sub1 = new Subscriber(observer);
const sub2 = new Subscriber(observer);

sub2.complete();

expect(sub1.closed).to.be.false;
expect(sub2.closed).to.be.true;
});
});
28 changes: 28 additions & 0 deletions spec/util/toSubscriber-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {expect} from 'chai';
import {toSubscriber} from '../../dist/cjs/util/toSubscriber';

describe('toSubscriber', () => {
it('should not be closed when other subscriber created with no arguments completes', () => {
let sub1 = toSubscriber();
let sub2 = toSubscriber();

sub2.complete();

expect(sub1.closed).to.be.false;
expect(sub2.closed).to.be.true;
});

it('should not be closed when other subscriber created with same observer instance completes', () => {
let observer = {
next: function () { /*noop*/ }
};

let sub1 = toSubscriber(observer);
let sub2 = toSubscriber(observer);

sub2.complete();

expect(sub1.closed).to.be.false;
expect(sub2.closed).to.be.true;
});
});

0 comments on commit 6a40d7b

Please sign in to comment.