Skip to content

Commit a0c2d48

Browse files
committed
Adding a new test suite to test horizontally scrolling lists.
This verifies that when content is scrolled horizontally (e.g. a list of items), the isInViewport calculations are correct after scrolling. You can see a CodePen with correct functionality here: http://codepen.io/npafundi/pen/wBNrop
1 parent 381242d commit a0c2d48

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
function runHorizontallyScrollingViewport() {
2+
var visible = '';
3+
$('li:in-viewport(0, #blocks)').each(function() {
4+
visible += $(this).text() + ' ';
5+
});
6+
return visible.trim();
7+
}
8+
9+
describe('isInViewport', function() {
10+
describe('viewport is a horizonatlly scrollable list (ul#blocks)', function() {
11+
var buidList = function() {
12+
$('body').prepend('<ul id="blocks"></ul>');
13+
14+
// Add 10 list items to the list
15+
for (var i=1; i<=10; i++)
16+
$('#blocks').append('<li>' + i + '</li>');
17+
};
18+
var removeList = function() {
19+
$('#blocks').remove();
20+
};
21+
var scrollLeft = function(px) {
22+
px = px || $('#blocks')[0].scrollWidth
23+
$('#blocks').scrollLeft(px);
24+
};
25+
26+
before(buidList);
27+
after(removeList);
28+
29+
describe('when the first four items are visible', function() {
30+
it('should return the string "1 2 3 4" as a list of currently visible items', function() {
31+
runHorizontallyScrollingViewport().should.be.exactly('1 2 3 4');
32+
});
33+
});
34+
describe('when we scroll the list left by 525px', function() {
35+
it('should return the string "4 5 6 7 8" as a list of currently visible items', function() {
36+
scrollLeft(525);
37+
runHorizontallyScrollingViewport().should.be.exactly('4 5 6 7 8');
38+
});
39+
});
40+
describe('when we scroll the list to the end', function() {
41+
it('should return the string "7 8 9 10" as a list of currently visible items', function() {
42+
scrollLeft();
43+
runHorizontallyScrollingViewport().should.be.exactly('7 8 9 10');
44+
});
45+
});
46+
});
47+
});

tests/tests.html

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,26 @@
5353
padding: 19px 36px;
5454
color: #fff;
5555
}
56+
ul#blocks {
57+
padding: 0;
58+
width: 600px;
59+
height: 170px;
60+
overflow-x: scroll;
61+
white-space: nowrap;
62+
font-size: 0;
63+
}
64+
65+
ul#blocks li {
66+
box-sizing: border-box;
67+
display: inline-block;
68+
background-color: #ff0000;
69+
height: 150px;
70+
width: 150px;
71+
text-align: center;
72+
font-size: 50px;
73+
padding: 50px;
74+
border: 1px solid #fff;
75+
}
5676
</style>
5777
</head>
5878

@@ -77,6 +97,7 @@
7797
<script type="text/javascript" src="./lib/mocha-blanket.js"></script>
7898
<script src="defaultViewportTests.js"></script>
7999
<script src="customViewportTests.js"></script>
100+
<script src="horizontallyScrollingViewportTests.js"></script>
80101

81102
<script>
82103
mocha.checkLeaks();

0 commit comments

Comments
 (0)