Skip to content

Commit

Permalink
Merge 6fb7c42 into d6dcfe1
Browse files Browse the repository at this point in the history
  • Loading branch information
Tetsuharu OHZEKI committed Jul 19, 2016
2 parents d6dcfe1 + 6fb7c42 commit 75bba09
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 4 deletions.
79 changes: 79 additions & 0 deletions spec/Subscription-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,83 @@ describe('Subscription', () => {
done();
});
});

describe('Subscription.add()', () => {
it('Should returns the self if the self is passed', () => {
const sub = new Subscription();
const ret = sub.add(sub);

expect(ret).to.equal(sub);
});

it('Should returns Subscription.EMPTY if it is passed', () => {
const sub = new Subscription();
const ret = sub.add(Subscription.EMPTY);

expect(ret).to.equal(Subscription.EMPTY);
});

it('Should returns Subscription.EMPTY if it is called with `void` value', () => {
const sub = new Subscription();
const ret = sub.add(undefined);
expect(ret).to.equal(Subscription.EMPTY);
});

it('Should returns a new Subscription created with teardown function if it is passed a function', () => {
const sub = new Subscription();

let isCalled = false;
const ret = sub.add(function() {
isCalled = true;
});
ret.unsubscribe();

expect(isCalled).to.equal(true);
});

it('Should returns the passed one if passed a unsubscribed AnonymousSubscription', () => {
const sub = new Subscription();
const arg = {
isUnsubscribed: true,
unsubscribe: () => undefined,
};
const ret = sub.add(arg);

expect(ret).to.equal(arg);
});

it('Should returns the passed one if passed a AnonymousSubscription having not function `unsubscribe` member', () => {
const sub = new Subscription();
const arg = {
isUnsubscribed: true,
unsubscribe: undefined as any,
};
const ret = sub.add(arg as any);

expect(ret).to.equal(arg);
});

it('Should returns the passed one if the self has been unsubscribed', () => {
const main = new Subscription();
main.unsubscribe();

const child = new Subscription();
const ret = main.add(child);

expect(ret).to.equal(child);
});

it('Should unsubscribe the passed one if the self has been unsubscribed', () => {
const main = new Subscription();
main.unsubscribe();

let isCalled = false;
const child = new Subscription(() => {
isCalled = true;
});
main.add(child);

expect(isCalled).to.equal(true);
});
});
});
10 changes: 6 additions & 4 deletions src/Subscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,12 @@ export class Subscription implements ISubscription {
* list.
*/
add(teardown: TeardownLogic): Subscription {
if (!teardown || (
teardown === this) || (
teardown === Subscription.EMPTY)) {
return;
if (!teardown || (teardown === Subscription.EMPTY)) {
return Subscription.EMPTY;
}

if (teardown === this) {
return this;
}

let sub = (<Subscription> teardown);
Expand Down

0 comments on commit 75bba09

Please sign in to comment.