Skip to content

Commit

Permalink
increase coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
dciccale committed Aug 5, 2015
1 parent f2e832a commit 19b4eeb
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 4 deletions.
4 changes: 0 additions & 4 deletions src/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,6 @@ Kimbo.define('events', function (_) {
var elementId = _getElementId(element);
var handleObj, handlers, name, i;

if (!elementId) {
return;
}

handlers = _getHandlers(elementId, type);

// Return if no handlers for the current event type
Expand Down
70 changes: 70 additions & 0 deletions test/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,62 @@ describe('ajax', function () {
xhr && xhr.restore && xhr.restore();
});

describe('ajax()', function () {
it('should be defined', function () {
expect($.ajax).to.be.defined;
});

it('should make a request', function () {
var cb = sinon.spy();
$.ajax({
type: 'GET',
url: 'my/url',
success: cb
});

expect(requests.length).to.equal(1);
requests[0].respond(200, {'Content-Type': 'test/html'}, 'hello');
expect(cb.called).to.be.true;
});

it('should allow sending data', function () {
var cb = sinon.spy();
$.ajax({
type: 'POST',
url: 'my/url',
success: cb,
data: {name: 'new'}
});

expect(requests.length).to.equal(1);
requests[0].respond(200, {'Content-Type': 'test/html'}, 'hello');
expect(cb.called).to.be.true;
});

it('should be able to define a custom timeout', function (done) {
this.timeout(800);
var cb = sinon.spy();

server.respondWith(function (xhr) {
xhr.respond(200, null, 'hello');
});

$.ajax({
type: 'GET',
url: 'my/url',
success: cb,
timeout: 500
});

server.respond();

window.setTimeout(function () {
expect(cb.called).to.be.false;
done();
} , 600);
});
});

describe('get()', function () {
it('should be defined', function () {
expect($.get).to.be.defined;
Expand Down Expand Up @@ -100,6 +156,20 @@ describe('ajax', function () {
expect(requests.length).to.equal(1);
requests[0].respond(200, {'Content-Type': 'application/json'}, '{"test": true}');
});

it('should fail if json is not well formatted', function () {
var cb = sinon.spy();
$.ajax({
type: 'GET',
url: 'my/url',
error: cb,
dataType: 'json'
});

expect(requests.length).to.equal(1);
requests[0].respond(200, {'Content-Type': 'application/json'}, '{"test: "asd"}');
expect(cb.called).to.be.true;
});
});

describe('param()', function () {
Expand Down
4 changes: 4 additions & 0 deletions test/css.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ describe('css', function () {
});

describe('hide()', function () {
it('should be defined', function () {
expect($.fn.hide).to.be.defined;
});

it('should hide an element', function () {
var $section = $('section');
$section.hide();
Expand Down
66 changes: 66 additions & 0 deletions test/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,48 @@ describe('event', function () {
spec.trigger($li[0], 'click');
expect(cb.called).to.be.true;
});

it('should not add register listener if no callback passed', function () {
var $li = $('#second');
$li.on('click');
});

it('should handle preventDefault()', function () {
var $ul = $('#list');
var $li = $('#second');
$li.on('click', function (ev) {
ev.preventDefault();
});
$ul.on('click', function (ev) {
expect(ev.isDefaultPrevented()).to.be.true;
});
spec.trigger($li[0], 'click');
});

it('should handle stopPropagation()', function () {
var $ul = $('#list');
var $li = $('#second');
var cb = sinon.spy();
$li.on('click', function (ev) {
ev.stopPropagation();
});
$ul.on('click', cb);
spec.trigger($li[0], 'click');
expect(cb.called).to.be.false;
});

it('should handle stopImmediatePropagation()', function () {
var $li = $('#second');
var lastClick = 'click1';
$li.on('click', function (ev) {
ev.stopImmediatePropagation();
});
$li.on('click', function () {
lastClick = 'click2';
});
spec.trigger($li[0], 'click');
expect(lastClick).to.equal('click1');
});
});

describe('off()', function () {
Expand All @@ -64,6 +106,30 @@ describe('event', function () {
spec.trigger($li[0], 'click');
expect(cb.called).to.be.false;
});

it('should remove all handlers if no event type provided', function () {
var $li = $('#second');
var cb = sinon.spy();
var cb2 = sinon.spy();
$li.on('click', cb);
$li.on('dblclick', cb2);
spec.trigger($li[0], 'click');
spec.trigger($li[0], 'dblclick');
expect(cb.called).to.be.true;
expect(cb2.called).to.be.true;

cb.called = false;
cb2.called = false;
$li.off();
spec.trigger($li[0], 'click');
spec.trigger($li[0], 'dblclick');
expect(cb.called).to.be.false;
expect(cb2.called).to.be.false;
});

it('should not try to remove an event handlers if the element has none for specified event type', function () {
$('#second').off('click');
});
});

describe('trigger()', function () {
Expand Down

0 comments on commit 19b4eeb

Please sign in to comment.