Skip to content

Commit

Permalink
[#2525] Add tests for jQuery.fn.slug()
Browse files Browse the repository at this point in the history
  • Loading branch information
aron committed Jul 2, 2012
1 parent 8d085d3 commit 7976b08
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
13 changes: 12 additions & 1 deletion ckan/public/base/test/index.html
Expand Up @@ -7,23 +7,34 @@
</head>
<body>
<div id="mocha"></div>
<div id="fixture" style="position: absolute; top: -9999px; left: -9999px"></div>

<!-- Test Runner -->
<script src="./vendor/mocha.js"></script>
<script src="./vendor/chai.js"></script>
<script>
mocha.setup('bdd');
assert = chai.assert;
var assert = chai.assert;
</script>

<!-- Source -->
<script src="../javascript/vendor/jquery.js"></script>
<script src="../javascript/plugins/jquery.url-helpers.js"></script>
<script src="../javascript/plugins/jquery.slug.js"></script>

<!-- Suite -->
<script src="./spec/plugins/jquery.url-helpers.spec.js"></script>
<script src="./spec/plugins/jquery.slug.spec.js"></script>

<script>
beforeEach(function () {
this.fixture = jQuery('#fixture').empty();
});

after(function () {
this.fixture.empty();
});

mocha.run().globals(['ckan']);
</script>
</body>
Expand Down
34 changes: 34 additions & 0 deletions ckan/public/base/test/spec/plugins/jquery.slug.spec.js
@@ -0,0 +1,34 @@
/*globals beforeEach describe it assert jQuery*/
describe('jQuery.slug()', function () {
beforeEach(function () {
this.input = jQuery('<input />').slug();
this.fixture.append(this.input);
});

it('should slugify and append the pressed key', function () {
var e = jQuery.Event('keypress', {charCode: 97 /* a */});
this.input.trigger(e);

assert(this.input.val(), 'a', 'append an "a"');

e = jQuery.Event('keypress', {charCode: 38 /* & */});
this.input.trigger(e);

assert(this.input.val(), 'a-', 'append an "-"');
});

it('should do nothing if a non character key is pressed', function () {
var e = jQuery.Event('keypress', {charCode: 0});
this.input.val('some other string').trigger(e);

assert(this.input.val(), 'some other string');
});

it('should slugify the input contents on "blur" and "change" events', function () {
this.input.val('apples & pears').trigger(jQuery.Event('blur'));
assert(this.input.val(), 'apples-pears', 'on blur');

this.input.val('apples & pears').trigger(jQuery.Event('change'));
assert(this.input.val(), 'apples-pears', 'on change');
});
});

0 comments on commit 7976b08

Please sign in to comment.