Comparing version 1.1.0 to 1.2.0
@@ -39,15 +39,15 @@ (function (chaiJSkit) { | ||
function missingAction(action, controller) { | ||
function controllerIsMissingAction(controller, action) { | ||
return !controller.elements[action]; | ||
} | ||
function missingElement(element, action, controller) { | ||
return !controller.elements[action][element]; | ||
function controllerIsMissingElement(controller, element) { | ||
return !controller[element]; | ||
} | ||
function hasNoEventHandlers(element, action, controller) { | ||
function controllerHasNoEventHandlersForElementOnAction(controller, element, action) { | ||
return !isArray(controller.elements[action][element]); | ||
} | ||
function hasNoRegisteredEvents(element, eventName, handler, action, controller) { | ||
function handlerIsNotRegisteredToEventOnElementForAction(handler, eventName, element, action, controller) { | ||
return !any(tail(controller.elements[action][element]), function(eventHandlers) { | ||
@@ -65,38 +65,73 @@ return eventHandlers[eventName] === handler; | ||
utils.flag(this, "element", element); | ||
var elementKey = element.replace(/^\$/, ""); | ||
var controller = this._obj; | ||
var pass = false; | ||
var assertMessage = "expected #{this} to register `" + handler + "` on `" + eventName + "` of the `$" + element + "` element, for the `" + action + "` action"; | ||
var refuteMessage = "expected #{this} to NOT register `" + handler + "` on `" + eventName + "` of the `$" + element + "` element, for the `" + action + "` action"; | ||
var testResult = { pass: true, reason: "" }; | ||
var assertMessage = "expected #{this} to register `" + handler + "` on `" + eventName + "` of the `" + element + "` element, for the `" + action + "` action"; | ||
var refuteMessage = "expected #{this} to NOT register `" + handler + "` on `" + eventName + "` of the `" + element + "` element, for the `" + action + "` action"; | ||
if (missingAction(action, controller)) return assertWithReason.call(this,pass, assertMessage, refuteMessage, "there is no `" + action + "` action"); | ||
if (missingElement(element, action, controller)) return assertWithReason.call(this,pass, assertMessage, refuteMessage, "there is no `$" + element + "` element"); | ||
if (hasNoEventHandlers(element, action, controller)) return assertWithReason.call(this,pass, assertMessage, refuteMessage, "the `" + element + "` element has no event handlers"); | ||
if (hasNoRegisteredEvents(element, eventName, handler, action, controller)) return assertWithReason.call(this,pass, assertMessage, refuteMessage, "the `" + handler + "` function is not registered to the `" + eventName + "` event on `$" + element + "`"); | ||
if (sinon) { | ||
sinon.stub(controller, handler); | ||
if (sinon && controller[handler]) { | ||
var stub = sinon.stub(controller, handler); | ||
controller.registerEvents(action); | ||
controller["$" + element].trigger(eventName); | ||
return assertWithReason.call(this, controller[handler].called, assertMessage, refuteMessage, "the `" + handler + "` function did not fire when `$" + element + "` triggered the `" + eventName + "` event"); | ||
controller[element].trigger(eventName); | ||
testResult = { | ||
pass: stub.called, | ||
reason: "the `" + handler + "` function did not fire when `" + element + "` triggered the `" + eventName + "` event" | ||
}; | ||
} | ||
}); | ||
chai.Assertion.addMethod("registerDynamicEvent", function(action, element, eventName, handler) { | ||
if (!sinon) throw new Error("registerDynamicEvent requires sinon-chai https://github.com/domenic/sinon-chai"); | ||
if (controllerIsMissingElement(controller, element)) { | ||
testResult = { | ||
pass: false, | ||
reason: "there is no `$" + element + "` element" | ||
}; | ||
} | ||
var assertMessage = "expected #{this} to register `" + handler + "` on `" + eventName + "` of the `$" + element + "` element, for the `" + action + "` action dynamically"; | ||
var refuteMessage = "expected #{this} to NOT register `" + handler + "` on `" + eventName + "` of the `$" + element + "` element, for the `" + action + "` action dynamically"; | ||
var controller = this._obj; | ||
if (controllerHasNoEventHandlersForElementOnAction(controller, elementKey, action)) { | ||
testResult = { | ||
pass: false, | ||
reason: "the `" + element + "` element has no event handlers" | ||
}; | ||
} | ||
sinon.stub(controller, handler); | ||
controller.registerEvents(action); | ||
controller["$" + element].trigger(eventName); | ||
this.assert(controller[handler].called, assertMessage, refuteMessage); | ||
if (!testResult.pass && handlerIsNotRegisteredToEventOnElementForAction(handler, eventName, elementKey, action, controller)) { | ||
testResult = { | ||
pass: false, | ||
reason: "the `" + handler + "` function is not registered to the `" + eventName + "` event on `" + element + "`" | ||
}; | ||
} | ||
assertWithReason.call(this, testResult.pass, assertMessage, refuteMessage, testResult.reason); | ||
}); | ||
chai.Assertion.addMethod("cacheElement", function(action, element, selector) { | ||
var testResult = { pass: true, reason: "" }; | ||
var elementKey = element.replace(/^\$/, ""); | ||
var assertMessage = "expected #{this} to cache the `" + selector + "` element on the `" + action + "` action"; | ||
var refuteMessage = "expected #{this} NOT to cache the `" + selector + "` element on the `" + action + "` action"; | ||
var subject = this._obj; | ||
if (!contains(keys(subject.elements[action]), element)) return assertWithReason.call(this, pass, assertMessage, refuteMessage, "there is no cache key `" + element + "`"); | ||
if (!first(flatten([subject.elements[action][element]])) === selector) return assertWithReason.call(this, pass, assertMessage, refuteMessage, "`" + selector + "` is not the selector for the cache key `" + element + "`"); | ||
if (!subject["$" + element].length) return assertWithReason.call(this, pass, assertMessage, refuteMessage, "`" + selector + "` is not in the DOM"); | ||
if (!subject[element]) { | ||
testResult = { | ||
pass: false, | ||
reason: "there is no cache key `" + element + "`" | ||
}; | ||
} | ||
if (!first(flatten([subject.elements[action][elementKey]])) === selector) { | ||
testResult = { | ||
pass: false, | ||
reason: "`" + selector + "` is not the selector for the cache key `" + element + "`" | ||
}; | ||
} | ||
if (!subject[element].length) { | ||
testResult = { | ||
pass: false, | ||
reason: "`" + selector + "` is not in the DOM" | ||
}; | ||
} | ||
assertWithReason.call(this, testResult.pass, assertMessage, refuteMessage, testResult.reason); | ||
}); | ||
})); |
{ | ||
"name": "chai-jskit", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "A chai plugin for easily testing JSkit applications.", | ||
"main": "chai-jskit.js", | ||
"scripts": { | ||
"test": "mocha" | ||
"test": "karma start --single-run" | ||
}, | ||
@@ -33,3 +33,3 @@ "repository": { | ||
"jquery": "^2.1.4", | ||
"jskit": "5.1.0-rc2", | ||
"jskit": "5.1.1", | ||
"karma": "^0.13.15", | ||
@@ -36,0 +36,0 @@ "karma-chai": "^0.1.0", |
describe("Chai-JSkit", function() { | ||
var subject; | ||
var app; | ||
beforeEach(function() { | ||
$("body").append("<div id='fixtures'/>") | ||
$("body").append("<div id='fixtures'/>"); | ||
var fixtures = $("#fixtures"); | ||
@@ -10,4 +9,4 @@ fixtures.append("<div class='test' />"); | ||
fixtures.append("<div class='dynamic' />"); | ||
app = JSkit.createApplication(); | ||
subject = app.createController("TestController", { | ||
subject = JSkit.createApplication().createController("TestController", { | ||
actions: [ | ||
@@ -26,3 +25,3 @@ "index", | ||
dynamic: [".dynamic", function(on) { | ||
on("click", this.handleDynamicBound); | ||
on("click", this.handleTodoListClick); | ||
}], | ||
@@ -34,5 +33,4 @@ todoList: [".todo-list", { click: "handleTodoListClick" }] | ||
form: function() {}, | ||
handleTodoListClick: function() {}, | ||
handleDynamicBound: function() {} | ||
}) | ||
handleTodoListClick: function() {} | ||
}); | ||
}); | ||
@@ -50,7 +48,7 @@ | ||
it("refutes the existence of an invalid named action", function() { | ||
expect(subject).to.not.have.action("nonexistent"); | ||
expect(subject).not.to.have.action("nonexistent"); | ||
}); | ||
it("refutes the existence of an invalid mapped action", function() { | ||
expect(subject).to.not.have.action("nonexistent", "mapped"); | ||
expect(subject).not.to.have.action("nonexistent", "mapped"); | ||
}); | ||
@@ -61,13 +59,11 @@ }); | ||
it("asserts the registration of event handlers", function() { | ||
expect(subject).to.registerEvent("index", "todoList", "click", "handleTodoListClick"); | ||
expect(subject).to.registerEvent("index", "$todoList", "click", "handleTodoListClick"); | ||
}); | ||
it("refutes the registration of event handlers", function() { | ||
expect(subject).to.not.registerEvent("nonexistent"); | ||
expect(subject).not.to.registerEvent("index", "$nonexistent", "click", "missingFunction"); | ||
}); | ||
}); | ||
describe("#registerDynamicEvent", function() { | ||
it("asserts the registration of dynamic event handlers", function() { | ||
expect(subject).to.registerDynamicEvent("index", "dynamic", "click", "handleDynamicBound"); | ||
expect(subject).to.registerEvent("index", "$dynamic", "click", "handleTodoListClick"); | ||
}); | ||
@@ -78,5 +74,5 @@ }); | ||
it("asserts the controller caches an element", function() { | ||
expect(subject).to.cacheElement("index", "todoList", ".todo-list"); | ||
expect(subject).to.cacheElement("index", "$todoList", ".todo-list"); | ||
}); | ||
}); | ||
}); |
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
10367
7
207
1
82