Skip to content

Commit

Permalink
adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaly-t committed Jul 30, 2019
1 parent f4e37a9 commit ee21cd1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {Subscription} from './sub';
/**
* Subscription Context Interface.
*/
export interface ISubContext<T, D = any> {
export interface ISubContext<T = any, D = any> {
readonly event: SubEvent<T>;
readonly cb: SubFunction<T>;

Expand Down
32 changes: 31 additions & 1 deletion test/event.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import {expect, chai} from './';
import {SubEvent} from '../src';
import {ISubContext, SubEvent} from '../src';

const dummy = () => {
};

describe('SubEvent', () => {
it('must invoke subscription functions', () => {
Expand Down Expand Up @@ -46,6 +49,33 @@ describe('SubEvent', () => {
expect(s2).to.not.have.been.called;
});
});
it('must call onSubscribe during subscription', () => {
let called = false;
const onSubscribe = (ctx: ISubContext) => {
called = true;
};
const a = new SubEvent({onSubscribe});
expect(called).to.be.false;
a.subscribe(dummy);
expect(called).to.be.true;
});
it('must call onCancel during cancellation', () => {
let data, called = false;
const onSubscribe = (ctx: ISubContext) => {
ctx.data = 123;
};
const onCancel = (ctx: ISubContext) => {
data = ctx.data;
called = true;
};
const a = new SubEvent({onSubscribe, onCancel});
const sub = a.subscribe(dummy);
expect(called).to.be.false;
sub.cancel();
expect(called).to.be.true;
expect(data).to.eql(123);
});

describe('emitSafe', () => {
const err = new Error('Ops!');
it('must handle errors from synchronous subscribers', done => {
Expand Down

0 comments on commit ee21cd1

Please sign in to comment.