Skip to content

Commit 0ad84fc

Browse files
committed
Added some basic init and render tests
1 parent 48da3fc commit 0ad84fc

File tree

5 files changed

+92
-27
lines changed

5 files changed

+92
-27
lines changed

bower.json

+3
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,8 @@
1414
"devDependencies": {
1515
"chai": "*",
1616
"lodash": "~2.2.1"
17+
},
18+
"dependencies": {
19+
"jquery": "~2.0.3"
1720
}
1821
}

carousel.js

+14-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
/*! Carousel.js v0.0.1 - MIT license */
1+
/*!
2+
* Carousel.js v1.0.0 ~ Copyright (c) 2013 Nino D'Aversa, http://ndaversa.com
3+
* Released under MIT license
4+
*/
25

36
;(function (global) { function moduleDefinition(_) {
47

@@ -14,35 +17,34 @@
1417

1518
function Carousel(options) {
1619
var carouselDefaults = {
17-
loop: true
20+
loop: true,
21+
bufferPages: 2
1822
};
19-
2023
options || (options = {});
21-
2224
_.extend(this, carouselDefaults, _.pick(options, carouselOptions));
25+
26+
this.el = typeof this.el == 'string' ? document.querySelector(this.el) : this.el;
2327
}
2428

25-
var carouselOptions = ['pageWidth', 'loop'];
29+
var carouselOptions = ['pageWidth', 'loop', 'el', 'bufferPages'];
2630

2731
_.extend(Carousel.prototype, {
2832

33+
render: function () {
34+
this.el.style.overflow = 'hidden';
35+
this.el.style.position = 'relative';
36+
return this;
37+
}
2938
});
3039

31-
/**
32-
* Expose carousel
33-
*/
34-
3540
return Carousel;
3641

3742
// ---------------------------------------------------------------------------
3843

3944
} if (typeof exports === 'object') {
40-
// node export
4145
module.exports = moduleDefinition(require('lodash'));
4246
} else if (typeof define === 'function' && define.amd) {
43-
// amd anonymous module registration
4447
define([require('lodash')], moduleDefinition);
4548
} else {
46-
// browser global
4749
global.Carousel = moduleDefinition(global._);
4850
}}(this));

karma.conf.js

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ module.exports = function (config) {
1616

1717
// list of files / patterns to load in the browser
1818
files: [
19+
'bower_components/jquery/jquery.js',
1920
'bower_components/lodash/dist/lodash.js',
2021
'bower_components/chai/chai.js',
2122
'carousel.js',

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "carousel.js",
33
"description": "",
4-
"version": "0.0.1",
4+
"version": "1.0.0",
55
"devDependencies": {
66
"karma": "~0.10.1",
77
"karma-mocha": "~0.1.0",

test/test.js

+73-14
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,82 @@ describe('Carousel', function () {
66
expect(carousel).to.not.be.undefined;
77
});
88

9-
it('has the correct default values', function () {
10-
var carousel = new Carousel();
11-
var carouselOptions = [
12-
'loop'
13-
];
14-
var pickedOptions = _.pick(carousel, carouselOptions);
9+
describe('options', function () {
10+
it('has the correct default values', function () {
11+
var carousel = new Carousel();
12+
var carouselOptions = [
13+
'loop',
14+
'bufferPages'
15+
];
16+
var pickedOptions = _.pick(carousel, carouselOptions);
17+
18+
expect(pickedOptions).to.deep.equal({
19+
loop: true,
20+
bufferPages: 2
21+
});
22+
});
23+
24+
it('allows defaults to be overridden', function () {
25+
var carousel = new Carousel({
26+
loop: false,
27+
bufferPages: 4
28+
});
29+
var carouselOptions = ['loop', 'bufferPages'];
30+
var pickedOptions = _.pick(carousel, carouselOptions);
31+
32+
expect(pickedOptions).to.deep.equal({
33+
loop: false,
34+
bufferPages: 4
35+
});
36+
});
37+
38+
it('accepts an element as an option', function () {
39+
$('<div id="test" />').appendTo('body');
40+
var carousel = new Carousel({
41+
el: $('#test')[0]
42+
});
43+
44+
expect(carousel.el.id).to.equal('test');
45+
});
1546

16-
expect(pickedOptions).to.deep.equal({ loop: true });
47+
it('accepts a selector as an option', function () {
48+
$('<div id="test" />').appendTo('body');
49+
var carousel = new Carousel({
50+
el: '#test'
51+
});
52+
53+
expect(carousel.el.id).to.equal('test');
54+
});
1755
});
1856

19-
it('allows defaults to be overridden', function () {
20-
var carousel = new Carousel({ loop: false });
21-
var carouselOptions = [
22-
'loop'
23-
];
24-
var pickedOptions = _.pick(carousel, carouselOptions);
57+
describe('render', function () {
58+
var el, carousel;
59+
60+
beforeEach(function () {
61+
el = $('<div id="test" />');
62+
el.appendTo('body');
63+
carousel = new Carousel({
64+
el: '#test'
65+
});
66+
});
67+
68+
afterEach(function () {
69+
el.remove();
70+
});
71+
72+
it('returns the instance of the object', function () {
73+
expect(carousel.render()).to.equal(carousel);
74+
});
75+
76+
it('correctly renders after instantiation', function () {
77+
var styleProperites = ['overflow', 'position'];
78+
var styles = _.pick(carousel.el.style, styleProperites);
2579

26-
expect(pickedOptions).to.deep.equal({ loop: false });
80+
carousel.render();
81+
expect(styles).to.deep.equal({
82+
overflow: 'hidden',
83+
position: 'relative'
84+
});
85+
});
2786
});
2887
})

0 commit comments

Comments
 (0)