Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Ahora tenemos la capacidad para espiar eventos generados por cualquie…

…r elemento
  • Loading branch information...
commit 3c2e93b49c170aab391c9c6abbfe9a0121822fa2 1 parent b351e82
@ggalmazor authored
View
8 spec/PlusMinusWidgetSpecs.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');
View
2  spec/runners/unitSpecs.html
@@ -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
45 spec/util/EventsHelper.js
@@ -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
7 src/Widgets/PlusMinus.js
@@ -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) );
Please sign in to comment.
Something went wrong with that request. Please try again.