<html>
<head>
<meta charset="utf-8">
<title>Getting started with QUnit</title>
<link rel="stylesheet" href="//code.jquery.com/qunit/qunit-1.14.0.css">
</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture"></div>
<script src="//code.jquery.com/qunit/qunit-1.14.0.js"></script>
<script>
// Code inlined in the HTML
// Tests here
var App = {
max: function () {
var max = -Infinity;
for (var i = 0; i < arguments.length; i++) {
if (arguments[i] > max) {
max = arguments[i];
}
}
return max;
},
isOdd: function (number) {
if (typeof number !== 'number') {
throw Error('The given argument is not a number');
}
return number % 2 !== 0;
},
sortObj: function (array) {
array.sort(function (a, b) {
var date1 = new Date(a.timestamp).getTime();
var date2 = new Date(b.timestamp).getTime();
if (date1 < date2) {
return -1;
} else if (date1 === date2) {
return 0;
} else {
return 1;
}
});
}
};
</script>
</body>
</html>
// Code inlined in the HTML
// Tests here
QUnit.test('max', function (assert) {
expect(4);
assert.strictEqual(App.max(), -Infinity, 'No parameters');
assert.strictEqual(App.max(3, 1, 2), 3, 'All positive numbers');
assert.strictEqual(App.max(-10, 5, 3, 99), 99, 'Positive and negative numbers');
assert.strictEqual(App.max(-14, -22, -5), -5, 'All positive numbers');
});
QUnit.test('isOdd', function (assert) {
expect(5);
assert.ok(App.isOdd(5), '5 is odd');
assert.ok(!App.isOdd(2), '5 is not odd');
assert.ok(!App.isOdd(0), '0 is not odd');
assert.throws(function () {
App.isOdd(null);
},
/The given argument is not a number/,
'Passing null raises an Error');
assert.throws(function () {
App.isOdd([]);
},
new Error('The given argument is not a number'),
'Passing an array raises an Error');
});
QUnit.test('sortObj', function (assert) {
expect(3);
var timestamp = Date.now();
var array = [{
id: 1,
timestamp: timestamp
}, {
id: 3,
timestamp: timestamp + 1000
}, {
id: 11,
timestamp: timestamp - 1000
}];
App.sortObj(array);
assert.propEqual(array, [{
id: 11,
timestamp: timestamp - 1000
}, {
id: 1,
timestamp: timestamp
}, {
id: 3,
timestamp: timestamp + 1000
}]);
assert.notPropEqual(App.sortObj(array), array, 'sortObj() does not return an array');
assert.strictEqual(App.sortObj(array), undefined, 'sortObj() returns undefined');
});
Output
You can jump to the latest bin by adding /latest
to your URL
Keyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |