Skip to content

Commit

Permalink
test(Subscription): Update subscription tests (ReactiveX#4912)
Browse files Browse the repository at this point in the history
* test(Subscription): Update subscription tests

* Cleaned up wording
* Moved positive cases to the top
* Grouped unsubscribe() tests together

* Fix the linter
  • Loading branch information
NothingEverHappens authored and benlesh committed Jul 13, 2019
1 parent 292be56 commit af20916
Showing 1 changed file with 81 additions and 79 deletions.
160 changes: 81 additions & 79 deletions spec/Subscription-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,101 +3,28 @@ import { Observable, UnsubscriptionError, Subscription, merge } from 'rxjs';

/** @test {Subscription} */
describe('Subscription', () => {
it('should not leak', done => {
const tearDowns: number[] = [];

const source1 = new Observable(() => {
return () => {
tearDowns.push(1);
};
});

const source2 = new Observable(() => {
return () => {
tearDowns.push(2);
throw new Error('oops, I am a bad unsubscribe!');
};
});

const source3 = new Observable(() => {
return () => {
tearDowns.push(3);
};
});

const subscription = merge(source1, source2, source3).subscribe();

setTimeout(() => {
expect(() => {
subscription.unsubscribe();
}).to.throw(UnsubscriptionError);
expect(tearDowns).to.deep.equal([1, 2, 3]);
done();
});
});

it('should not leak when adding a bad custom subscription to a subscription', done => {
const tearDowns: number[] = [];

const sub = new Subscription();

const source1 = new Observable(() => {
return () => {
tearDowns.push(1);
};
});

const source2 = new Observable(() => {
return () => {
tearDowns.push(2);
sub.add(<any>({
unsubscribe: () => {
expect(sub.closed).to.be.true;
throw new Error('Who is your daddy, and what does he do?');
}
}));
};
});

const source3 = new Observable(() => {
return () => {
tearDowns.push(3);
};
});

sub.add(merge(source1, source2, source3).subscribe());

setTimeout(() => {
expect(() => {
sub.unsubscribe();
}).to.throw(UnsubscriptionError);
expect(tearDowns).to.deep.equal([1, 2, 3]);
done();
});
});

describe('Subscription.add()', () => {
it('Should returns the self if the self is passed', () => {
it('Should return 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', () => {
it('Should return 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', () => {
it('Should return 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', () => {
it('Should return a new Subscription created with teardown function if it is passed a function', () => {
const sub = new Subscription();

let isCalled = false;
Expand All @@ -124,7 +51,7 @@ describe('Subscription', () => {
expect(sub._subscriptions.length).to.equal(0);
});

it('Should returns the passed one if passed a AnonymousSubscription having not function `unsubscribe` member', () => {
it('Should return the passed one if passed a AnonymousSubscription having not function `unsubscribe` member', () => {
const sub = new Subscription();
const arg = {
isUnsubscribed: false,
Expand All @@ -135,7 +62,7 @@ describe('Subscription', () => {
expect(ret).to.equal(arg);
});

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

Expand All @@ -158,4 +85,79 @@ describe('Subscription', () => {
expect(isCalled).to.equal(true);
});
});

describe('Subscription.unsubscribe()', () => {
it('Should unsubscribe from all subscriptions, when some of them throw', done => {
const tearDowns: number[] = [];

const source1 = new Observable(() => {
return () => {
tearDowns.push(1);
};
});

const source2 = new Observable(() => {
return () => {
tearDowns.push(2);
throw new Error('oops, I am a bad unsubscribe!');
};
});

const source3 = new Observable(() => {
return () => {
tearDowns.push(3);
};
});

const subscription = merge(source1, source2, source3).subscribe();

setTimeout(() => {
expect(() => {
subscription.unsubscribe();
}).to.throw(UnsubscriptionError);
expect(tearDowns).to.deep.equal([1, 2, 3]);
done();
});
});

it('Should unsubscribe from all subscriptions, when adding a bad custom subscription to a subscription', done => {
const tearDowns: number[] = [];

const sub = new Subscription();

const source1 = new Observable(() => {
return () => {
tearDowns.push(1);
};
});

const source2 = new Observable(() => {
return () => {
tearDowns.push(2);
sub.add(<any>({
unsubscribe: () => {
expect(sub.closed).to.be.true;
throw new Error('Who is your daddy, and what does he do?');
}
}));
};
});

const source3 = new Observable(() => {
return () => {
tearDowns.push(3);
};
});

sub.add(merge(source1, source2, source3).subscribe());

setTimeout(() => {
expect(() => {
sub.unsubscribe();
}).to.throw(UnsubscriptionError);
expect(tearDowns).to.deep.equal([1, 2, 3]);
done();
});
});
});
});

0 comments on commit af20916

Please sign in to comment.