Skip to content

Commit 0cdc2f4

Browse files
committed
added tests for history extension
1 parent 9fc8014 commit 0cdc2f4

File tree

6 files changed

+84
-6
lines changed

6 files changed

+84
-6
lines changed

src/extension/history.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ IASHistoryExtension.prototype.initialize = function (ias) {
135135

136136
// expose prev method
137137
ias.prev = function() {
138-
self.prev();
138+
return self.prev();
139139
};
140140

141141
this.prevUrl = this.getPrevUrl();

test/04-history-extension-test.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
buster.spec.expose();
2+
3+
describe("IAS", function () {
4+
before(function() {
5+
this.timeout = 10000;
6+
7+
window.scrollTo(0, 0);
8+
});
9+
10+
after(function() {
11+
jQuery.ias('destroy');
12+
});
13+
14+
it("should expose a prev method", function() {
15+
var ias = jQuery.ias({
16+
container : '.listing',
17+
item: '.post',
18+
pagination: '.navigation',
19+
next: '.next-posts a'
20+
});
21+
22+
ias.extension(new IASTriggerExtension());
23+
ias.extension(new IASPagingExtension());
24+
ias.extension(new IASHistoryExtension({
25+
prev: '.prev-posts a'
26+
}));
27+
28+
ias.initialize();
29+
30+
expect(ias.prev).toBeDefined();
31+
});
32+
33+
it("should load the prev page when prev() is called", function() {
34+
var deferred = when.defer();
35+
36+
loadFixture("page2.html", function() {
37+
var ias = jQuery.ias({
38+
container : '.listing',
39+
item: '.post',
40+
pagination: '.navigation',
41+
next: '.next-posts a'
42+
});
43+
44+
ias.extension(new IASPagingExtension());
45+
ias.extension(new IASHistoryExtension({
46+
prev: '.prev-posts a'
47+
}));
48+
49+
// ias auto-loads the prev page when initialized,
50+
// this prevents the autoloading
51+
var firstTime = true;
52+
ias.on('prev', function () {
53+
if (firstTime) {
54+
firstTime = false;
55+
56+
return false;
57+
}
58+
59+
return true;
60+
}, 1000);
61+
62+
ias.initialize();
63+
64+
expect($('#post1').length).toEqual(0);
65+
66+
expect(ias.prev()).toBeTrue();
67+
68+
wait(1000).then(function() {
69+
expect($('#post1').length).toEqual(1);
70+
71+
deferred.resolve();
72+
});
73+
});
74+
75+
return deferred.promise;
76+
});
77+
});

test/buster.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ config["My tests"] = {
1212
"src/extension/paging.js",
1313
"src/extension/spinner.js",
1414
"src/extension/trigger.js",
15-
"src/extension/noneleft.js"
15+
"src/extension/noneleft.js",
16+
"src/extension/history.js"
1617
],
1718
resources: [
1819
{ path: "/", file: "test/fixtures/page1.html" },

test/fixtures/page1.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<body>
2222
<div id="content">
2323
<div class="listing">
24-
<div class="post">
24+
<div class="post" id="post1">
2525
<strong>item 1</strong>
2626
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis.</p>
2727
</div>

test/fixtures/page2.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@
6262

6363
<div class="navigation">
6464
<ul>
65-
<li class="prev-posts"><a href="page1.html">prev</a></li>
66-
<li><a href="page1.html">1</a></li>
65+
<li class="prev-posts"><a href="./">prev</a></li>
66+
<li><a href="./">1</a></li>
6767
<li>2</li>
6868
<li><a href="page3.html">3</a></li>
6969
<li class="next-posts"><a href="page3.html">next</a></li>

test/fixtures/page3.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
<div class="navigation">
6464
<ul>
6565
<li class="prev-posts"><a href="page2.html">prev</a></li>
66-
<li><a href="page1.html">1</a></li>
66+
<li><a href="./">1</a></li>
6767
<li><a href="page2.html">2</a></li>
6868
<li>3</li>
6969
</ul>

0 commit comments

Comments
 (0)