// Here is a simple function for opening a dialog box.
// This is the "system under test" (SUT)
function displayMessage(message, title) {
// Setup the KendoWindow as a dialog box
var dialog = $("#message").kendoWindow({
width: "300px",
height: "200px",
title: title,
modal: true,
visible: false,
actions: ["Close"]
});
// Inject the message argument into the window
$("#message").html(message);
// Center and open the window
dialog.data("kendoWindow")
.center()
.open();
}
// Sample usage - setting up a button that will invoke the SUT
$("#showMessage").click(function () {
console.log("button clicked");
displayMessage("here's the message", "and this is the title");
});
// A few variables that will help us setup the tests
var sandbox; // this will be a SinonJS sandbox - a container for mocks, stubs, and spies.
var kendoWindowSpy // this will be a spy for the KendoWindow;
var jQueryHtmlSpy // and this will become a spy for changing the HTML inside the window;
// Prepare a QUnit module for testing - this is the test fixture
module("displayMessage tests",
{
setup: function () {
sandbox = sinon.sandbox.create();
// Using mocks with jQuery and Kendo is very challenging, since you have to
// specify all behavior and returned objects. Instead, SinonJS's spies functionality
// is the right choice - it allows us to look at what was called, without
// having specify all expected behavior. In .Net testing, I prefer mocks
// so that we can define all expected behavior. But so far I've simply
// found that to be impractical in Javascript tests.
jQueryHtmlSpy = sandbox.spy(jQuery.fn, "html");
kendoWindowSpy = sandbox.spy(jQuery.fn, "kendoWindow");
},
teardown: function () {
// Restore the actual jQuery/Kendo functionality by clearing the sandbox
sandbox.restore();
// Re-close the dialog box
$("#message").data("kendoWindow").close();
}
});
test("displayMessage opens a Kendo window and replaces the inner contents", function () {
// ** Prepare input **
var title = "some title";
var message = "<p>The detailed message</p><p>with HTML in it</p>";
// ** Call the system under test **
// That is, call the function "displayMessage"
displayMessage(message, title);
// ** Evaluate the results **
// Were the spies used?
ok(jQueryHtmlSpy.calledOnce, "HTML should be injected");
ok(kendoWindowSpy.calledOnce, "dialog window should be opened");
// Now let's dig into the details a bit... first making sure the desire message was injected
ok(jQueryHtmlSpy.calledWithExactly(message), "desired message not passed");
// Next, let's look at the arguments passed to the KendoWindow. I don't want to
// look at all the arguments - that would feel like overspecification to me.
// So let's just test that the JSON-formatted arguments include the modal setting
// and that the input title is injected as desired.
var args = kendoWindowSpy.firstCall.args[0];
//var actual = JSON.parse(args);
ok(args.modal, "modal property");
equal(args.title, title, "title property");
});
<button id="showMessage">Show Message</button>
<div id="message">this text will be replaced with a dialog box containing new text</div>
<hr />
<!-- Standard setup for Qunit is -->
<h1 id="qunit-header">QUnit Test Results</h1>
<h2 id="qunit-banner"></h2>
<div id="qunit-testrunner-toolbar"></div>
<h2 id="qunit-userAgent"></h2>
<div id="qunit"></div>
<div id="qunit-fixture"></div>
External resources loaded into this fiddle: