Skip to content

Commit abd9e64

Browse files
author
Chasen Le Hara
committed
Fix the third test
1 parent 350ce59 commit abd9e64

File tree

2 files changed

+49
-23
lines changed

2 files changed

+49
-23
lines changed

static/canjs.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,16 @@ function navigate(href, updateLocation) {
371371
return;
372372
}
373373

374+
// don’t do anything if just the hash changed
375+
var currentHrefBase = currentHref.replace(/#.*/, '');// This is the current URL without the hash
376+
var hrefBase = href.replace(/#.*/, '');// This is the new URL without the hash
377+
if (currentHrefBase === hrefBase) {
378+
if (updateLocation !== false) {// We don’t want to pushState when our popstate listener calls this
379+
window.history.pushState({ articleScroll: getPageScrollTop() }, null, href);
380+
}
381+
return;
382+
}
383+
374384
loader.start();
375385

376386
if($menuButton.is(':visible')){

test/scroll.js

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ QUnit.test('Scroll down and refresh the page', function(assert) {
3232
F.win.location.reload();
3333

3434
// Wait for the page to be loaded
35-
F("body").wait(function(){
35+
F("body").wait(function() {
3636
// Check whether the element exists
3737
return this[0] ? true : false;
3838
}, function() {
@@ -61,41 +61,57 @@ QUnit.test('Refresh after going to a specific section', function(assert) {
6161
F.win.location.reload();
6262

6363
// Wait for the page to be loaded
64-
F("#Components").wait(function(){
64+
F("#Components").wait(function() {
6565
// Check whether the element exists
6666
return this[0] ? true : false;
67-
}, F.wait(300, function() {
68-
// Determine whether the section is in view
69-
var rect = F.win.$("#Components")[0].getBoundingClientRect();
70-
assert.ok(utils.rectIntersectsWithWindow(rect, F.win));
71-
done();
72-
}));
67+
}, function() {
68+
// Wait for the restoration logic to run
69+
setTimeout(function() {
70+
// Determine whether the section is in view
71+
var rect = F.win.$("#Components")[0].getBoundingClientRect();
72+
assert.ok(utils.rectIntersectsWithWindow(rect, F.win));
73+
done();
74+
}, 300);
75+
});
7376
});
7477
});
7578

7679
QUnit.test("Refresh after going to a specific section and scrolling", function(assert) {
7780
var done = assert.async();
7881

82+
// Open a page
7983
F.open('../doc/guides/html.html', function() {
84+
var offset = 100;
85+
86+
// Set the size so the page takes up some space
8087
F.frame.height = 400;
8188
F.frame.width = "100%";
8289

90+
// Click a TOC link to go to a specific section
8391
F(".on-this-page-table a[href='#Overview']").click();
8492

85-
var pos = F("#Components").offset().top - 60;
86-
F.win.scroll("top", pos);
87-
F.win.location.reload();
88-
89-
F("#Components").wait(function() {
90-
var element = this[0];
91-
if (!element) {
92-
return false;
93-
}
94-
var rect = element.getBoundingClientRect();
95-
return utils.rectIntersectsWithWindow(rect, F.win)
96-
}, function() {
97-
assert.ok(true);
98-
done();
93+
// Wait for the page to be scrolled down to the section
94+
F.wait(300, function() {
95+
96+
// Scroll to 100px from the top of the page
97+
F.win.scroll("top", offset);
98+
99+
// Reload the page
100+
F.win.location.reload();
101+
102+
// Wait for the page to be loaded
103+
F("body").wait(function() {
104+
// Check whether the element exists
105+
return this[0] ? true : false;
106+
}, function() {
107+
// Wait for the restoration logic to run
108+
setTimeout(function() {
109+
// Get the amount the body has been scrolled
110+
var bodyTopOffset = F("body")[0].getBoundingClientRect().top;
111+
assert.equal(bodyTopOffset, 0 - offset);
112+
done();
113+
}, 300);
114+
});
99115
});
100116
});
101-
})
117+
});

0 commit comments

Comments
 (0)