Skip to content

Commit

Permalink
Update specs
Browse files Browse the repository at this point in the history
  • Loading branch information
Ianfeather committed Jul 24, 2013
1 parent 95b6320 commit 8a8e89d
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 59 deletions.
28 changes: 21 additions & 7 deletions dist/$.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/*globals Node:true, NodeList:true*/
$ = (function (document, window, $) {
$$ = (function (document, window, $$) {
// Node covers all elements, but also the document objects
var node = Node.prototype,
nodeList = NodeList.prototype,
forEach = 'forEach',
trigger = 'trigger',
each = [][forEach],
cookies = {},
// note: createElement requires a string in Firefox
dummy = document.createElement('i');

Expand Down Expand Up @@ -49,7 +50,13 @@ $ = (function (document, window, $) {
return this;
};

$ = function (s) {

// Get a cookie value
cookies.get = function(key) {
return unescape(document.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*" + escape(key).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")) || null;
}

$$ = function (s) {
// querySelectorAll requires a string with a length
// otherwise it throws an exception
var r = document.querySelectorAll(s || '☺'),
Expand All @@ -60,10 +67,17 @@ $ = (function (document, window, $) {
return length == 1 ? r[0] : r;
};

// $.on and $.trigger allow for pub/sub type global
// $$.on and $$.trigger allow for pub/sub type global
// custom events.
$.on = node.on.bind(dummy);
$[trigger] = node[trigger].bind(dummy);
$$.on = node.on.bind(dummy);
$$[trigger] = node[trigger].bind(dummy);

$$.cookies = cookies

if ( typeof define === "function") {
define( "jsmin", [], function () { return $$; } );
}

return $$;
})(document, this);

return $;
})(document, this);
2 changes: 1 addition & 1 deletion dist/$.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 16 additions & 16 deletions test/spec/api.js
Original file line number Diff line number Diff line change
@@ -1,76 +1,76 @@
/*globals afterEach: true, describe: true, it: true, expect: true, beforeEach: true, appendToDom:true, destroyDom:true, $:true*/
/*globals afterEach: true, describe: true, it: true, expect: true, beforeEach: true, appendToDom:true, destroyDom:true, $$:true*/
'use strict';

describe('api', function () {
describe('no selector', function () {
it('should return on method', function () {
assert(typeof $.on === 'function');
assert(typeof $$.on === 'function');
});

it('should return trigger method', function () {
assert(typeof $.trigger === 'function');
assert(typeof $$.trigger === 'function');
});
});

describe('empty selector', function () {
var $empty = $('');
var $$empty = $$('');

it('should return on method', function () {
assert(typeof $empty.on === 'function');
assert(typeof $$empty.on === 'function');
});

it('should return trigger method', function () {
assert(typeof $empty.trigger === 'function');
assert(typeof $$empty.trigger === 'function');
});
});

describe('iterator', function () {
var $link;
var $$link;

beforeEach(function () {
appendToDom('section', ['a', 'a', 'a']);

$link = $('section > a');
$$link = $$('section > a');
});

afterEach(destroyDom);

it('should have forEach', function () {
assert(typeof $link.forEach === 'function');
assert(typeof $$link.forEach === 'function');
});

it('should loop', function () {
var ctr = 0;
$link.forEach(function () {
$$link.forEach(function () {
ctr++;
});
assert(ctr === 3);
});

it('should have each element passed to iterator', function () {
$link.forEach(function (el, i) {
assert(el === $link[i]);
$$link.forEach(function (el, i) {
assert(el === $$link[i]);
});
});
});

describe('matched selector', function () {
var $link;
var $$link;

beforeEach(function () {
appendToDom('section', ['a']);

$link = $('section > a');
$$link = $$('section > a');
});

afterEach(destroyDom);

it('should return on method', function () {
assert(typeof $link.on === 'function');
assert(typeof $$link.on === 'function');
});

it('should return trigger method', function () {
assert(typeof $link.trigger === 'function');
assert(typeof $$link.trigger === 'function');
});
});
});
20 changes: 10 additions & 10 deletions test/spec/chaining.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,51 @@
/*globals noop:true, appendToDom:true, destroyDom:true, $:true*/
/*globals noop:true, appendToDom:true, destroyDom:true, $$:true*/
'use strict';

describe('chaining', function () {
describe('no selector', function () {
it('should return trigger method from on', function () {
assert(typeof $.on('event', noop).trigger === 'function');
assert(typeof $$.on('event', noop).trigger === 'function');
});

it('should return on method from trigger', function () {
assert(typeof $.on('event', noop).trigger('event').on === 'function');
assert(typeof $$.on('event', noop).trigger('event').on === 'function');
});
});

// left in, note that Firefox does not support empty selectors
describe('unmatched selector', function () {
it('should return trigger method from on', function () {
assert(typeof $('unmatched').on('event', noop).trigger === 'function');
assert(typeof $$('unmatched').on('event', noop).trigger === 'function');
});

it('should return on method from trigger', function () {
assert(typeof $('unmatched').on('event', noop).trigger('event').on === 'function');
assert(typeof $$('unmatched').on('event', noop).trigger('event').on === 'function');
});
});

describe('empty selector', function () {
it('should still allow for chained methods', function () {
assert(typeof $('').on('event', noop).trigger === 'function');
assert(typeof $$('').on('event', noop).trigger === 'function');
});
});

describe('matched selector', function () {
var $link;
var $$link;

beforeEach(function () {
appendToDom('section', ['a']);

$link = $('section > a');
$$link = $$('section > a');
});

afterEach(destroyDom);

it('should return trigger method from on', function () {
assert(typeof $link.on('event', noop).trigger === 'function');
assert(typeof $$link.on('event', noop).trigger === 'function');
});

it('should return on method from trigger', function () {
assert(typeof $link.on('event', noop).trigger('event').on === 'function');
assert(typeof $$link.on('event', noop).trigger('event').on === 'function');
});
});
});
24 changes: 12 additions & 12 deletions test/spec/delegation.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,45 @@
/*globals appendToDom:true, destroyDom:true, $:true*/
/*globals appendToDom:true, destroyDom:true, $$:true*/
'use strict';

describe('delegation', function () {
var $section,
$article,
var $$section,
$$article,
spy;

beforeEach(function () {
spy = sinon.spy();
appendToDom('section', ['article', 'a', 'a']);

$section = $('section');
$article = $('section > article');
$$section = $$('section');
$$article = $$('section > article');
});

afterEach(destroyDom);

it('should trigger container events from children', function () {
var target;

$section
$$section
.on('event', spy)
.on('event', function (event) {
target = event.target;
});

$article.trigger('event');
$$article.trigger('event');

assert(spy.called);
assert(target === $article);
assert(target === $$article);
});

it('should trigger click event using event delegation', function () {
$('body').delegate('section > a', 'click', spy);
$('section > a')[0].trigger('click');
$$('body').delegate('section > a', 'click', spy);
$$('section > a')[0].trigger('click');

assert(spy.called);
$('section > a')[0].trigger('click');
$$('section > a')[0].trigger('click');
assert(spy.calledTwice);

$('section > a')[1].trigger('click');
$$('section > a')[1].trigger('click');
assert(spy.calledThrice);
});
});
4 changes: 2 additions & 2 deletions test/spec/elements.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*globals appendToDom:true, destroyDom:true, $:true*/
/*globals appendToDom:true, destroyDom:true, $$:true*/
'use strict';

describe('elements', function () {
Expand All @@ -9,7 +9,7 @@ describe('elements', function () {
afterEach(destroyDom);

it('should grab an element from the DOM', function () {
var link = $('div > a');
var link = $$('div > a');

assert(link.outerHTML === '<a></a>');
});
Expand Down
22 changes: 11 additions & 11 deletions test/spec/events.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*globals $:true*/
/*globals $$:true*/
'use strict';

describe('events', function () {
Expand All @@ -13,11 +13,11 @@ describe('events', function () {
});

it('should assign an event to an element', function () {
var $body = $('body');
var $$body = $$('body');

$body.on('click', spy);
$$body.on('click', spy);

$body.trigger('click');
$$body.trigger('click');

sinon.assert.called(spy);
});
Expand All @@ -39,17 +39,17 @@ describe('events', function () {
});

it('should not trigger an event on a non-element', function () {
$('.this-isnt-on-the-dom').on('event', spy);
$$('.this-isnt-on-the-dom').on('event', spy);

$('.this-isnt-on-the-dom').trigger('event');
$$('.this-isnt-on-the-dom').trigger('event');

sinon.assert.notCalled(spy);
});

it('should assign an event to the internal element', function () {
$.on('event', spy);
$$.on('event', spy);

$.trigger('event');
$$.trigger('event');

sinon.assert.called(spy);
});
Expand All @@ -62,7 +62,7 @@ describe('events', function () {
afterEach(destroyDom);

it('should trigger', function () {
$('div > a').on('click', spy).trigger('click');
$$('div > a').on('click', spy).trigger('click');
sinon.assert.calledTwice(spy);
});

Expand All @@ -76,8 +76,8 @@ describe('events', function () {
afterEach(destroyDom);

it('should trigger', function () {
$('div > a').addEventListener('click', spy);
$('div > a').trigger('click');
$$('div > a').addEventListener('click', spy);
$$('div > a').trigger('click');
sinon.assert.called(spy);
});
});
Expand Down

0 comments on commit 8a8e89d

Please sign in to comment.