Skip to content
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

Specs for MicroEvent #15

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 115 additions & 0 deletions spec/MicroEventSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
describe("MicroEvent", function() {
var micro_event;
var event_funcs;

beforeEach(function() {
micro_event = new MicroEvent();

event_funcs = {
foo: function() {},
bar: function() {},
baz: function() {}
};
spyOn(event_funcs, 'foo');
spyOn(event_funcs, 'bar');
spyOn(event_funcs, 'baz');

});

it("binds a single function to an event", function() {
micro_event.bind('event_one', event_funcs.foo);
micro_event.trigger('event_one');

expect(event_funcs.foo).toHaveBeenCalled();
expect(event_funcs.foo.calls.length).toEqual(1);
});

it("passes arguments to a bound function", function() {
micro_event.bind('event_one', event_funcs.foo);
micro_event.trigger('event_one', 1, 2);

expect(event_funcs.foo).toHaveBeenCalledWith(1, 2);
});

it("binds several functions to one event", function() {
micro_event.bind('event_one', event_funcs.foo);
micro_event.bind('event_one', event_funcs.bar);
micro_event.bind('event_one', event_funcs.baz);

micro_event.trigger('event_one');

expect(event_funcs.foo).toHaveBeenCalled();
expect(event_funcs.foo.calls.length).toEqual(1);

expect(event_funcs.bar).toHaveBeenCalled();
expect(event_funcs.bar.calls.length).toEqual(1);

expect(event_funcs.baz).toHaveBeenCalled();
expect(event_funcs.baz.calls.length).toEqual(1);
});

it("passes arguments to all functions bound to the same event", function() {
micro_event.bind('event_one', event_funcs.foo);
micro_event.bind('event_one', event_funcs.bar);
micro_event.bind('event_one', event_funcs.baz);

micro_event.trigger('event_one', 1, 2);

expect(event_funcs.foo).toHaveBeenCalledWith(1, 2);
expect(event_funcs.bar).toHaveBeenCalledWith(1, 2);
expect(event_funcs.baz).toHaveBeenCalledWith(1, 2);
});

it("binds functions to several different events", function() {
micro_event.bind('event_one', event_funcs.foo);
micro_event.bind('event_two', event_funcs.bar);
micro_event.bind('event_three', event_funcs.baz);

micro_event.trigger('event_one');
micro_event.trigger('event_two');
micro_event.trigger('event_three');

expect(event_funcs.foo).toHaveBeenCalled();
expect(event_funcs.foo.calls.length).toEqual(1);

expect(event_funcs.bar).toHaveBeenCalled();
expect(event_funcs.bar.calls.length).toEqual(1);

expect(event_funcs.baz).toHaveBeenCalled();
expect(event_funcs.baz.calls.length).toEqual(1);
});

it("unbinds a function from an event", function() {
micro_event.bind('event_one', event_funcs.foo);
micro_event.bind('event_one', event_funcs.bar);

micro_event.unbind('event_one', event_funcs.foo);

micro_event.trigger('event_one');

expect(event_funcs.foo).not.toHaveBeenCalled();

expect(event_funcs.bar).toHaveBeenCalled();
expect(event_funcs.bar.calls.length).toEqual(1);
});

it("keeps working when triggering unknown event", function() {
micro_event.bind('event_one', event_funcs.foo);
micro_event.bind('event_two', event_funcs.bar);

micro_event.trigger('unknown_event');

expect(event_funcs.foo).not.toHaveBeenCalled();
expect(event_funcs.bar).not.toHaveBeenCalled();
});

it("keeps working when unbinding unknown event", function() {
micro_event.bind('event_one', event_funcs.foo);
micro_event.unbind('unknown_event');

micro_event.trigger('event_one');

expect(event_funcs.foo).toHaveBeenCalled();
});
});

2 changes: 2 additions & 0 deletions spec/SpecRunner.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
<script type="text/javascript" src="../js/plugins/star.js"></script>
<script type="text/javascript" src="../js/plugins/refresh.js"></script>
<script type="text/javascript" src="../js/plugins/local-storage.js"></script>
<script type="text/javascript" src="../js/libs/microevent.js"></script>

<!-- include spec files here... -->
<script type="text/javascript" src="PluginFactorySpec.js"></script>
<script type="text/javascript" src="MicroEventSpec.js"></script>

<script type="text/javascript">
(function() {
Expand Down