Skip to content

Commit bd5d524

Browse files
committed
Tests!
1 parent 96c131f commit bd5d524

File tree

8 files changed

+169
-15
lines changed

8 files changed

+169
-15
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
bower_components
2+
node_modules

Gruntfile.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*global module:false*/
2+
module.exports = function(grunt) {
3+
4+
// Project configuration.
5+
grunt.initConfig({
6+
pkg: grunt.file.readJSON('package.json'),
7+
watch: {
8+
files: ['fixedsticky.css', 'fixedsticky.js', 'test/fixed-sticky-tests.js'],
9+
tasks: 'qunit'
10+
},
11+
qunit: {
12+
all: ['test/**/*.html']
13+
}
14+
});
15+
16+
require( 'matchdep' ).filterDev( 'grunt-*' ).forEach( grunt.loadNpmTasks );
17+
18+
// Default task.
19+
grunt.registerTask( 'test', [ 'qunit' ] );
20+
grunt.registerTask( 'default', [ 'qunit' ] );
21+
};

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,7 @@ These tests were performed using fixed-sticky with fixed-fixed. It’s safest to
7070

7171
## TODO
7272

73-
* Tests (of course). I have a serious case of developer guilt releasing this without tests.
7473
* Add support for table headers.
75-
* Method to unbind scroll/resize events.
7674
* Vanilla JS version.
7775

7876
## Release History

demos/demo.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<meta charset="utf-8" />
55
<meta name="viewport" content="width=device-width,initial-scale=1">
66
<title>Fixed Sticky</title>
7-
<link rel="stylesheet" href="fixedsticky.css">
7+
<link rel="stylesheet" href="../fixedsticky.css">
88
<style>
99
/* Demo CSS */
1010
body { font-family: sans-serif; color: #444; margin: 0 auto; padding: 50px 20px; max-width: 600px; }
@@ -177,9 +177,9 @@ <h2>Header Level 2</h2>
177177
<li>Aliquam tincidunt mauris eu risus.</li>
178178
</ol>
179179

180-
<script src="bower_components/jquery/jquery.js"></script>
181-
<script src="bower_components/filament-fixed/fixedfixed.js"></script>
182-
<script src="fixedsticky.js"></script>
180+
<script src="../bower_components/jquery/jquery.js"></script>
181+
<script src="../bower_components/filament-fixed/fixedfixed.js"></script>
182+
<script src="../fixedsticky.js"></script>
183183
<script>
184184
FixedSticky.tests.sticky = false;
185185
$( '.top, .bottom' ).fixedsticky();

fixedsticky.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
clone: 'fixedsticky-dummy',
2222
withoutFixedFixed: 'fixedsticky-withoutfixedfixed'
2323
},
24+
keys: {
25+
offset: 'fixedStickyOffset',
26+
position: 'fixedStickyPosition'
27+
},
2428
tests: {
2529
sticky: featureTest( 'position', 'sticky' ),
2630
fixed: featureTest( 'position', 'fixed', true )
@@ -41,26 +45,22 @@
4145
if( !el.offsetWidth ) return;
4246

4347
var $el = $( el ),
44-
keys = {
45-
offset: 'fixedStickyOffset',
46-
position: 'fixedStickyPosition'
47-
},
4848
height = $el.outerHeight(),
49-
initialOffset = $el.data( keys.offset ),
49+
initialOffset = $el.data( S.keys.offset ),
5050
scroll = S.getScrollTop(),
5151
isAlreadyOn = $el.is( '.' + S.classes.active ),
5252
toggle = function( turnOn ) {
5353
$el[ turnOn ? 'addClass' : 'removeClass' ]( S.classes.active );
5454
},
5555
viewportHeight = $( window ).height(),
56-
position = $el.data( keys.position ),
56+
position = $el.data( S.keys.position ),
5757
skipSettingToFixed,
5858
elTop,
5959
elBottom;
6060

6161
if( !initialOffset ) {
6262
initialOffset = $el.offset().top;
63-
$el.data( keys.offset, initialOffset );
63+
$el.data( S.keys.offset, initialOffset );
6464
$el.after( $( '<div>' ).addClass( S.classes.clone ).height( height ) );
6565
}
6666

@@ -81,7 +81,7 @@
8181
$el.css( 'position', '' );
8282
}
8383

84-
$el.data( keys.position, position );
84+
$el.data( S.keys.position, position );
8585
}
8686

8787
elTop = parseInt( $( el ).css( 'top' ), 10 ) || 0;
@@ -107,7 +107,7 @@
107107

108108
return $el.each(function() {
109109
$( this )
110-
.removeData( [ keys.offset, keys.position ] )
110+
.removeData( [ S.keys.offset, S.keys.position ] )
111111
.removeClass( S.classes.active )
112112
.nextUntil( S.classes.clone ).remove();
113113
});

package.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "fixed-sticky",
3+
"version": "0.1.1",
4+
"description": "A position: sticky polyfill that works with filamentgroup/fixed-fixed for a safer position:fixed fallback.",
5+
"main": "fixedsticky.js",
6+
"scripts": {
7+
"test": "grunt test"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git://github.com/filamentgroup/fixed-sticky.git"
12+
},
13+
"keywords": [
14+
"sticky"
15+
],
16+
"author": "Zach Leatherman",
17+
"license": "MIT",
18+
"bugs": {
19+
"url": "https://github.com/filamentgroup/fixed-sticky/issues"
20+
},
21+
"devDependencies": {
22+
"grunt": "~0.4.1",
23+
"grunt-contrib-qunit": "~0.3.0",
24+
"matchdep": "~0.3.0",
25+
"grunt-contrib-watch": "~0.5.3"
26+
}
27+
}

test/fixed-sticky-tests.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*global QUnit:false, module:false, test:false, asyncTest:false, expect:false, start:false, stop:false, ok:false, equal:false, notEqual:false, deepEqual:false, notDeepEqual:false, strictEqual:false, notStrictEqual:false, raises:false, SocialCount:true */
2+
(function($) {
3+
4+
/*
5+
======== A Handy Little QUnit Reference ========
6+
http://docs.jquery.com/QUnit
7+
8+
Test methods:
9+
expect(numAssertions)
10+
stop(increment)
11+
start(decrement)
12+
Test assertions:
13+
ok(value, [message])
14+
equal(actual, expected, [message])
15+
notEqual(actual, expected, [message])
16+
deepEqual(actual, expected, [message])
17+
notDeepEqual(actual, expected, [message])
18+
strictEqual(actual, expected, [message])
19+
notStrictEqual(actual, expected, [message])
20+
raises(block, [expected], [message])
21+
*/
22+
23+
module('testDefault');
24+
25+
test( 'Setup', function() {
26+
// These tests require non-native sticky support. Should be set in the test runner.
27+
ok( !FixedSticky.tests.sticky );
28+
});
29+
30+
test( 'Standard Top', function() {
31+
$('#qunit-fixture').html(
32+
['<style>#sticky { top: 0; }</style>',
33+
'<div id="sticky" class="fixedsticky">Sticky</div>',
34+
'<div style="width: 100%; height: 2000px">Test</div>'].join( '' ) );
35+
36+
var $sticky = $( '#sticky' );
37+
$sticky.fixedsticky();
38+
39+
ok( $sticky.hasClass( 'fixedsticky' ) );
40+
$(window).scrollTop( 1000 ).trigger( 'scroll' );
41+
equal( $sticky.css( 'position' ), 'fixed' );
42+
equal( $sticky.offset().top, 1000 );
43+
});
44+
45+
test( 'Standard Bottom', function() {
46+
$('#qunit-fixture').html(
47+
['<style>#sticky { bottom: 0; }</style>',
48+
'<div style="width: 100%; height: 2000px">Test</div>',
49+
'<div id="sticky" class="fixedsticky">Sticky</div>'].join( '' ) );
50+
51+
var $sticky = $( '#sticky' );
52+
$sticky.fixedsticky();
53+
54+
ok( $sticky.hasClass( 'fixedsticky' ) );
55+
$(window).scrollTop( 1000 ).trigger( 'scroll' );
56+
equal( $sticky.css( 'position' ), 'fixed' );
57+
equal( $sticky.offset().top, 1000 + $( window ).height() - $sticky.height() );
58+
});
59+
60+
test( 'Cleanup', function() {
61+
$('#qunit-fixture').html('<div id="sticky" class="fixedsticky">Sticky</div>' );
62+
63+
var $sticky = $( '#sticky' );
64+
$sticky.fixedsticky();
65+
66+
ok( $sticky.next().hasClass( FixedSticky.classes.clone ) );
67+
68+
$sticky.fixedsticky( 'destroy' );
69+
ok( !$sticky.siblings( '.' + FixedSticky.classes.clone ).length );
70+
});
71+
72+
}( jQuery ));

test/fixed-sticky.html

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>fixed-sticky Test Suite</title>
6+
<!-- Load local jQuery, removing access to $ (use jQuery, not $). -->
7+
<script src="../bower_components/jquery/jquery.js"></script>
8+
<script>jQuery.noConflict()</script>
9+
<!-- Load local QUnit (grunt requires v1.0.0 or newer). -->
10+
<link rel="stylesheet" href="../bower_components/qunit/qunit/qunit.css">
11+
<style>
12+
#qunit-fixture {
13+
position: static !important;
14+
width: auto;
15+
height: auto;
16+
}
17+
</style>
18+
<link rel="stylesheet" href="../fixedsticky.css">
19+
<script src="../bower_components/qunit/qunit/qunit.js"></script>
20+
<!-- Load local lib and tests. -->
21+
<script src="../fixedsticky.js"></script>
22+
<script>
23+
FixedSticky.tests.sticky = false;
24+
</script>
25+
<script src="fixed-sticky-tests.js"></script>
26+
</head>
27+
<body>
28+
<h1 id="qunit-header">fixed-sticky Test Suite</h1>
29+
<h2 id="qunit-banner"></h2>
30+
<div id="qunit-testrunner-toolbar"></div>
31+
<h2 id="qunit-userAgent"></h2>
32+
<ol id="qunit-tests"></ol>
33+
<div id="qunit-fixture"></div>
34+
</body>
35+
</html>

0 commit comments

Comments
 (0)