-
Notifications
You must be signed in to change notification settings - Fork 113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds events class and specs #1194
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
const noOp = function() {}; | ||
|
||
class Events { | ||
constructor() { | ||
this.handlers = {}; | ||
} | ||
|
||
on(eventName, handler = noOp, context = null) { | ||
if (!eventName) return; | ||
|
||
const handlersForEvent = this.handlers[eventName] || []; | ||
|
||
if (!handlersForEvent.filter(obj => obj.handler === handler).length) { | ||
handlersForEvent.push({ handler, context }); | ||
} | ||
|
||
this.handlers[eventName] = handlersForEvent; | ||
} | ||
|
||
off(eventName, handler) { | ||
if (!eventName) { | ||
Object.keys(this.handlers).forEach((name) => { | ||
this.handlers[name].length = 0; | ||
}); | ||
} else if (!handler || typeof handler !== 'function') { | ||
this.handlers[eventName].length = 0; | ||
} else { | ||
const handlers = this.handlers[eventName] || []; | ||
this.handlers[eventName] = handlers.filter(obj => obj.handler !== handler); | ||
} | ||
} | ||
|
||
emit(eventName, ...rest) { | ||
const handlers = this.handlers[eventName] || []; | ||
|
||
handlers.forEach(({ handler, context }) => handler.apply(context, rest)); | ||
} | ||
} | ||
|
||
export default Events; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
const Events = require('../../../../app/assets/javascripts/app/utils/events').default; | ||
/*eslint-disable */ | ||
const sinon = require('sinon'); | ||
/*eslint-enable */ | ||
|
||
describe('Events', () => { | ||
const myEvent = 'super.event'; | ||
const dummyHandler = sinon.stub(); | ||
let events; | ||
|
||
beforeEach(() => { | ||
events = new Events(); | ||
}); | ||
|
||
it('maintains a map of handler objects', () => { | ||
expect(events.handlers).not.to.be.undefined(); | ||
expect(events.handlers).to.be.an('object'); | ||
}); | ||
|
||
describe('#on', () => { | ||
it('does nothing if an event name is not supplied', () => { | ||
const initialHandlerLen = Object.keys(events.handlers).length; | ||
events.on(); | ||
|
||
expect(Object.keys(events.handlers).length).to.equal(initialHandlerLen); | ||
}); | ||
|
||
it('adds handler object to event key when name and function supplied', () => { | ||
events.on(myEvent, dummyHandler); | ||
|
||
const clickHandlers = events.handlers[myEvent]; | ||
|
||
expect(clickHandlers.length).to.equal(1); | ||
expect(clickHandlers[0]).to.be.an('object'); | ||
expect(clickHandlers[0].handler).to.equal(dummyHandler); | ||
}); | ||
|
||
it('stores the context of the handler when supplied', () => { | ||
class FakeClass { contructor() { this.thing = 'fake'; } } | ||
|
||
const fake = new FakeClass(); | ||
|
||
events.on(myEvent, dummyHandler, fake); | ||
|
||
expect(events.handlers[myEvent][0].context instanceof FakeClass).to.be.true(); | ||
}); | ||
}); | ||
|
||
describe('#off', () => { | ||
it('deletes all handlers if no event name is supplied', () => { | ||
events.on(myEvent, dummyHandler); | ||
events.off(); | ||
|
||
expect(events.handlers[myEvent].length).to.equal(0); | ||
}); | ||
|
||
it('deletes all events registered for a given even when one is supplied', () => { | ||
const otherEvent = 'other.event'; | ||
|
||
events.on(myEvent, dummyHandler); | ||
events.on(otherEvent, function() {}); | ||
|
||
events.off(myEvent); | ||
|
||
expect(events.handlers[otherEvent].length).to.equal(1); | ||
}); | ||
|
||
it('deletes the specific handler supplied', () => { | ||
const anotherStub = sinon.stub(); | ||
|
||
events.on(myEvent, dummyHandler); | ||
events.on(myEvent, anotherStub); | ||
|
||
events.off(myEvent, dummyHandler); | ||
|
||
expect(events.handlers[myEvent].length).to.equal(1); | ||
expect(events.handlers[myEvent][0].handler).to.equal(anotherStub); | ||
}); | ||
}); | ||
|
||
describe('#emit', () => { | ||
it('calls each handler registered to an event', () => { | ||
events.on(myEvent, dummyHandler); | ||
events.emit(myEvent); | ||
|
||
expect(dummyHandler.calledOnce).to.be.true(); | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sinon looks cool, do you think we should start moving in this direction for javascript tests? I tend to rely on feature specs but I know that's probably not the right way if we continue to add more javascript.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I think we definitely want to be unit testing our javascript where possible. Self contained classes that don't have explicit dependencies on the DOM or browser APIs are great candidates for that.