Permalink
Browse files

Ahora tenemos la capacidad para espiar eventos generados por cualquie…

…r elemento
  • Loading branch information...
1 parent b351e82 commit 3c2e93b49c170aab391c9c6abbfe9a0121822fa2 Guille committed Mar 19, 2012
Showing with 52 additions and 10 deletions.
  1. +4 −4 spec/PlusMinusWidgetSpecs.js
  2. +2 −0 spec/runners/unitSpecs.html
  3. +45 −0 spec/util/EventsHelper.js
  4. +1 −6 src/Widgets/PlusMinus.js
@@ -14,14 +14,14 @@ describe("el plus minus widget", function () {
expect($('button.minus', element).length).toBe(1);
});
- it("lanza un evento onClick con el número y la operación cuando se clickan los botones inyectados", function () {
+ it("lanza un evento 'plus' con el número cuando se clicka el botón plus", function () {
var widget = Widgets.PlusMinus.widgetize(element);
- spyOn(widget, 'onClick');
+ spyOnEvent(widget, 'plus');
$('button.plus', element).click();
- expect(widget.onClick).toHaveBeenCalledWith(number, 'plus');
+ expect(widget).toHaveTriggered('plus');
});
describe('El controlador', function () {
- it("solicita el nuevo número al servidor cada vez que se clicka un botón del widget", function () {
+ xit("solicita el nuevo número al servidor cada vez que se clicka un botón del widget", function () {
var widget = Widgets.PlusMinus.widgetize(element);
spyOn($, 'getJSON');
@@ -14,6 +14,8 @@
window.__DEV__ = dev;
};
</script>
+ <!-- Helpers & Util -->
+ <script type="text/javascript" src="../util/EventsHelper.js"></script>
<!-- Source -->
<script type="text/javascript" src="../../src/DOM/DOMBuilder.js"></script>
View
@@ -0,0 +1,45 @@
+var jasmineExtensions = jasmineExtensions || {};
+
+(function (extensions, undefined) {
+ var jQueryEventSpy = function (element, eventName) {
+ this.element = element;
+ this.eventName = eventName;
+ this.triggered = false;
+ };
+
+ jQueryEventSpy.prototype.hit = function () {
+ this.triggered = true;
+ };
+ jQueryEventSpy.prototype.matches = function (element, eventName) {
+ return this.element == element && this.eventName == eventName;
+ };
+ jQueryEventSpy.prototype.hasBeenTriggered = function () {
+ return this.triggered;
+ };
+
+ extensions.jQueryEventSpy = jQueryEventSpy;
+ extensions.jQueryEventSpies = [];
+ extensions.spyOnEvent = function (element, eventName) {
+ var spy = new jQueryEventSpy(element, eventName);
+ $(element).bind(eventName, spy.hit.bind(spy));
+ extensions.jQueryEventSpies.push(spy);
+ };
+
+}(jasmineExtensions));
+
+var spyOnEvent = jasmineExtensions.spyOnEvent;
+
+beforeEach(function () {
+ this.addMatchers({
+ toHaveTriggered:function (eventName) {
+ var length = jasmineExtensions.jQueryEventSpies.length;
+ for (var i = 0; i < length; i++) {
+ var candidate = jasmineExtensions.jQueryEventSpies[i];
+ if (candidate.matches(this.actual, eventName) && candidate.hasBeenTriggered()) {
+ return true;
+ }
+ }
+ return false;
+ }
+ });
+});
View
@@ -1,5 +1,3 @@
-
-
window.Widgets = window.Widgets || {};
(function (context, undefined) {
@@ -38,13 +36,10 @@ window.Widgets = window.Widgets || {};
}
PlusMinusWidget.prototype.click = function (operation) {
- this.onClick(this.number, operation);
+ $(this).trigger(operation, {number:this.number});
};
- PlusMinusWidget.prototype.onClick = function () {
- } // To be handled
-
context.PlusMinus = PlusMinusWidget;
}(window.Widgets) );

0 comments on commit 3c2e93b

Please sign in to comment.