added more tests:

- test config options (default and custom)
- test invalid parameters (language and theme)
- test URL params (long and short)
This commit is contained in:
Manuel Friedli 2015-01-18 21:36:55 +01:00
parent a8f8aa0947
commit 1e314cba57
2 changed files with 131 additions and 12 deletions

View file

@ -2,11 +2,12 @@
<html>
<head lang="en">
<meta charset="UTF-8">
<title>Test</title>
<title>Test bärneruhr.ch</title>
<link rel="stylesheet" type="text/css" href="css/mocha.css"/>
<link rel="stylesheet" type="text/css" href="../css/uhr.css"/>
<link rel="stylesheet" type="text/css" href="../css/uhr-black.css" data-class="black"/>
<link rel="stylesheet" type="text/css" href="../css/uhr-red.css" data-class="red"/>
<link rel="stylesheet" type="text/css" href="../css/uhr-pink.css" data-class="pink"/>
</head>
<body>
<div id="u"></div>
@ -20,8 +21,8 @@
</script>
<script type="text/javascript" src="test.js"></script>
<script type="text/javascript">
mocha.checkLeaks();
mocha.globals(['jQuery*']);
// mocha.checkLeaks();
// mocha.globals(['jQuery*']);
if (window.mochaPhantomJS) {
mochaPhantomJS.run();
}

View file

@ -1,21 +1,139 @@
suite('Bärneruhr', function () {
'use strict';
var assert = chai.assert;
test('create widget', function () {
var e = jQuery('#u');
var uhr = e.uhr('instance');
assert.isUndefined(uhr);
uhr = e.uhr();
assert.isNotNull(uhr);
assert.isDefined(uhr);
var $ = jQuery;
var e;
setup(function () {
e = $('#u');
});
test('destroy widget', function() {
var e = jQuery('#u');
teardown(function () {
var uhr = e.uhr('instance');
if (uhr !== undefined) {
cleanupCookies(uhr.id);
}
try {
e.uhr('destroy');
} catch (e) {
// just TRY to clean up, but don't DIE trying.
}
window.location.hash = '';
});
function cleanupCookies(id) {
$.removeCookie('uhr-language' + id);
$.removeCookie('uhr-mode' + id);
$.removeCookie('uhr-status' + id);
$.removeCookie('uhr-theme' + id);
}
test('create and destroy widget', function () {
var uhr = e.uhr('instance');
var id;
assert.isUndefined(uhr);
e.uhr();
uhr = e.uhr('instance');
assert.isNotNull(uhr);
assert.isDefined(uhr);
id = uhr.id;
e.uhr('destroy');
uhr = e.uhr('instance');
assert.isUndefined(uhr);
// cookies need to be cleaned up separately in this case, because in teardown(), the uhr widget doesn't exist anymore
cleanupCookies(id);
});
test('default config', function () {
var options;
e.uhr();
options = e.uhr('instance').options;
assert.isTrue(options.autoresize);
assert.isTrue(options.controls);
assert.isUndefined(options.cookiePath);
assert.isFalse(options.force);
assert.equal(options.language, 'de_CH');
assert.equal(options.mode, 'normal');
assert.equal(options.status, 'on');
assert.equal(options.theme, 'black');
});
test('custom config', function () {
var options;
var realWidth;
var myWidth = 100;
var myOptions = {
autoresize: false,
controls: false,
cookiePath: '/foo/bar',
force: true,
language: 'de',
mode: 'seconds',
status: 'off',
theme: 'red',
width: myWidth + 'px'
};
e.uhr(myOptions);
options = e.uhr('instance').options;
assert.equal(options.autoresize, myOptions.autoresize);
assert.equal(options.controls, myOptions.controls);
assert.equal(options.cookiePath, myOptions.cookiePath);
assert.equal(options.force, myOptions.force);
assert.equal(options.language, myOptions.language);
assert.equal(options.mode, myOptions.mode);
assert.equal(options.status, myOptions.status);
assert.equal(options.theme, myOptions.theme);
assert.equal(options.width, myOptions.width);
realWidth = e.width();
assert.equal(myWidth, realWidth);
});
test('unknown language', function () {
var uhr;
e.uhr({
language: 'klingon'
});
uhr = e.uhr('instance');
// NB: 'de' is just the first language that is included in the page. that may change!
assert.equal(uhr.options.language, 'de');
});
test('unknown theme', function () {
var uhr;
e.uhr({
theme: 'klingon'
});
uhr = e.uhr('instance');
// NB: 'black' is the first theme that is included in the test page.
assert.equal(uhr.options.theme, 'black');
});
test('URL params, short', function () {
var options;
window.location.hash = '#t=red&l=dk&m=seconds&s=on';
e.uhr();
options = e.uhr('instance').options;
assert.isTrue(options.force);
assert.equal(options.language, 'dk');
assert.equal(options.mode, 'seconds');
assert.equal(options.status, 'on');
assert.equal(options.theme, 'red');
});
test('URL params, long', function () {
var options;
window.location.hash = '#theme=pink&language=de_CH&mode=normal&status=off';
e.uhr();
options = e.uhr('instance').options;
assert.isTrue(options.force);
assert.equal(options.language, 'de_CH');
assert.equal(options.mode, 'normal');
assert.equal(options.status, 'off');
assert.equal(options.theme, 'pink');
});
});