Skip to content

Commit

Permalink
fix: make event object immutable
Browse files Browse the repository at this point in the history
  • Loading branch information
sibiraj-s committed Nov 11, 2019
1 parent 9e97dfe commit b691adf
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
2 changes: 2 additions & 0 deletions capillaries.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ function Capillaries() {
this.unbindAll = function unbindAll() {
events = {};
};

Object.freeze(this);
}

export default Capillaries;
22 changes: 20 additions & 2 deletions capillaries.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ it('should unbind correct events', () => {
expect(callbackFnR).toBeCalled();
});

it('should not unbind events when off method is invoked with invalid type', () => {
it('should not unbind events when `off` method is invoked with invalid type', () => {
const callbackFnQ = jest.fn();
const callbackFnR = jest.fn();

Expand All @@ -103,7 +103,7 @@ it('should not unbind events when off method is invoked with invalid type', () =
expect(callbackFnR).toBeCalled();
});

it('should unbind all when unbindAll is invoked', () => {
it('should unbind all when `unbindAll` is invoked', () => {
const evt = new Capillaries();

const callbackFnQ = jest.fn();
Expand All @@ -130,3 +130,21 @@ it('should unbind all when unbindAll is invoked', () => {
expect(callbackFnS).not.toBeCalled();
expect(callbackFnT).not.toBeCalled();
});

it('should be immutable', () => {
const func = jest.fn();
const callbackFnQ = jest.fn();

expect(() => { event.on = func; }).toThrow(TypeError);
expect(() => { event.off = func; }).toThrow(TypeError);
expect(() => { event.emit = func; }).toThrow(TypeError);
expect(() => { event.unbindAll = func; }).toThrow(TypeError);
expect(() => { event.newFunc = func; }).toThrow(TypeError);

event.on('q', callbackFnQ);
event.emit('q', payload);

expect(func).not.toBeCalled();
expect(event.newFunc).toBe(undefined);
expect(callbackFnQ).toBeCalled();
});

0 comments on commit b691adf

Please sign in to comment.