Skip to content

Instantly share code, notes, and snippets.

@opiethehokie
Created February 4, 2014 14:38
Show Gist options
  • Select an option

  • Save opiethehokie/8804702 to your computer and use it in GitHub Desktop.

Select an option

Save opiethehokie/8804702 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>QUnit basic example</title>
<link rel="stylesheet" href="resources/qunit.css">
</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture"></div>
<script src="resources/jquery-2.0.0.js"></script>
<script src="resources/qunit.js"></script>
<script src="resources/json2.js"></script>
<script src="resources/jquery.mockjax.js"></script>
<script src="testable_js.js"></script>
<script>
test( "a basic test example", function() {
equal( farenheitToCelcius(32), 0, "farenheit to celcius conversion" );
});
test( "a basic test example 2", function() {
equal( celciusTofarenheit(-40), -40, "celcius to farenheit conversion" );
});
module( "example group of tests with setup and teardown", {
setup: function() {
ok( true, "one extra assert per test" );
}, teardown: function() {
ok( true, "and one extra assert after each test" );
}
});
test( "a basic test example 3", function() {
ok( true, "this test is fine" );
ok( true, "this test is fine" );
ok( true, "this test is fine" );
});
module( "with qunit tests are atomic" );
test( "Appends a div", function() {
appendDiv("qunit-fixture", "hello!");
equal( $( "div", $( "#qunit-fixture" ) ).length, 1, "div added successfully!" );
});
test( "Appends a span", function() {
appendSpan("qunit-fixture", "hello again!");
equal( $( "span", $( "#qunit-fixture" ) ).length, 1, "div added successfully!" );
});
module( "testing synchronous callbacks" );
test( "multiplication", function() {
var result = calc( 2, function( x ) {
ok( true, "calc() calls operation function" );
return x * x;
});
equal( result, 4, "2 square equals 4" );
});
test( "subtraction", function() {
var result = calc( 55, function( x ) {
ok( true, "calc() calls operation function" );
return x - 10;
});
equal( result, 45, "55 minux 10 equals 45" );
});
module( "real asynchronous callback" );
asyncTest("real ajax", function() {
// I think there's a bug, I get an error trying to call ok() in one of my callbacks
expect(0);
$.mockjaxClear()
workingAjax(
function(data) { console.log(data); },
function(jqxhr, textStatus, error) { console.log(error); }
);
// fail after .5 seconds if start isn't called
setTimeout(function() {
start();
}, 500);
});
asyncTest( "mocked asynchronous callback success", function() {
expect(1); // verify we have one callback
$.mockjaxClear()
$.mockjax({
url: '/rest/notimplemented',
responseTime: 250,
status: 200 // HTTP success code
});
nonWorkingAjax(
function() { ok(true); },
function() { ok(false); }
);
// fail after .5 seconds if start isn't called
setTimeout(function() {
start();
}, 500);
});
asyncTest( "mocked asynchronous callback failure", function() {
expect(1); // verify we have one callback
$.mockjaxClear()
$.mockjax({
url: '/rest/notimplemented',
responseTime: 250,
status: 400 // HTTP error code
});
nonWorkingAjax(
function() { ok(false); },
function() { ok(true); }
);
// fail after .5 seconds if start isn't called
setTimeout(function() {
start();
}, 500);
});
module( "testing user behavior" );
test( "keylogger api behavior", function() {
var event,
$doc = $( document ),
keys = KeyLogger( $doc );
// trigger event
event = $.Event( "keydown" );
event.keyCode = 9;
$doc.trigger( event );
// verify expected behavior
equal( keys.log.length, 1, "a key was logged" );
equal( keys.log[ 0 ], 9, "correct key was logged" );
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment