Skip to content

Commit

Permalink
Adding a bunch more tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
nrn committed Mar 19, 2012
1 parent 9061ccd commit 10b591e
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
2 changes: 1 addition & 1 deletion readme.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ arguments.
dom elements, or a string name.
* [Optional] Data to be stored if a string name was used.
Can be a function that gets called with the elment as this, and the
index of the element in the selection as the first argument.
element, index, and the entire selection as the arguments.

If there is both a name and data the function sets the appropriate data
on the dom and returns the selected elements. If a name is given but no data
Expand Down
4 changes: 4 additions & 0 deletions test.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,9 @@
</head>
<body>
<div id="mocha"></div>
<div id="test"></div>
<div id="obj"></div>
<div id="func" class="map"></div>
<div id="func1" class="map"></div>
</body>
</html>
63 changes: 62 additions & 1 deletion test/factory.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
describe('factory', function () {
describe('#DataDash({noMethods: true})', function () {
dataDash = DataDash({noMethods: true});
var dataDash = DataDash({noMethods: true});
it('should not add method to jQuery', function () {
expect(window.jQuery.fn.dataDash).to.not.exist;
});
Expand All @@ -24,3 +24,64 @@ describe('factory', function () {
});
});
});

describe('String as name', function () {
describe('#$("#test").dataDash', function () {
it('Should set/get strings', function () {
$('#test').dataDash('asdf', 'qwert');
expect($('#test').dataDash('asdf')[0]).to.equal('qwert');
});
it('Should set/get numbers', function () {
$('#test').dataDash('num', 42);
expect($('#test').dataDash('num')[0]).to.equal(42);
});
it('Should set/get objects', function () {
$('#test').dataDash('obj', {foo: 'blah', bar: 123, nested: {asdf: 'test'}});
expect($('#test').dataDash('obj')[0]).to.eql({
foo: 'blah',
bar: 123,
nested: {asdf: 'test'}
});
});
it('Should set/get arrays', function () {
$('#test').dataDash('arr', ['asdf', 1234, ['netsted', 4321]]);
expect($('#test').dataDash('arr')[0]).to.eql(['asdf', 1234, ['netsted', 4321]]);
});
it('Should get everything', function () {
expect($('#test').dataDash()[0]).to.eql({
asdf: 'qwert',
num: 42,
obj:{foo: 'blah', bar: 123, nested: {asdf: 'test'}},
arr:['asdf', 1234, ['netsted', 4321]]
});
});
});
});

describe('Setting objects', function () {
describe('#$("#obj").dataDash()', function () {
it('Should set an entire object', function () {
var obj = {key: 'value', stuff: 3333, inside: ['blah', 16], hmm: {wee: 'asdf'}};
$('#obj').dataDash(obj);
expect($('#obj').dataDash()[0]).to.eql(obj);
});
});
});

describe('Setting by mapping', function () {
describe('#$(".map").dataDash("str", function (ele, idx, array) {};)', function () {
it('Should map a over a set of tags', function () {
var maper = function (ele, idx, sel) {
expect(ele).to.eql(sel[idx]);
expect(ele).to.eql(this);
return 'asdf' + idx;
};
$('.map').dataDash('asdf', maper);
expect($('.map').dataDash('asdf')).to.eql([
'asdf0',
'asdf1'
]);
});
});
});

0 comments on commit 10b591e

Please sign in to comment.