Skip to content

Commit 6d65916

Browse files
committed
Add tests for back button behaviour while loading.
1 parent a0011cc commit 6d65916

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed

test/unit/pjax.js

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,111 @@ if ($.support.pjax) {
741741
}, 0)
742742
}
743743

744+
asyncTest("clicking back while loading cancels XHR", function() {
745+
var frame = this.frame
746+
747+
equal(frame.location.pathname, "/home.html")
748+
equal(frame.document.title, "Home")
749+
750+
frame.$("#main").on('pjax:timeout', function(e) {
751+
e.preventDefault();
752+
})
753+
754+
frame.$("#main").one('pjax:complete', function() {
755+
756+
equal(frame.location.pathname, "/hello.html")
757+
equal(frame.document.title, "Hello")
758+
759+
frame.$("#main").one('pjax:send', function() {
760+
761+
// don't use goBack here, because pjax:end isn't triggered
762+
// when clicking back while loading
763+
764+
frame.$("#main").one('pjax:complete', function(e, xhr) {
765+
equal(xhr.status, 0);
766+
})
767+
768+
frame.history.back();
769+
770+
setTimeout(function() {
771+
var afterBackLocation = frame.location.pathname
772+
var afterBackTitle = frame.document.title
773+
774+
setTimeout(function() {
775+
equal(frame.location.pathname, afterBackLocation)
776+
equal(frame.document.title, afterBackTitle)
777+
start()
778+
}, 1000)
779+
}, 500)
780+
781+
})
782+
783+
frame.$.pjax({
784+
url: "timeout.html",
785+
container: "#main"
786+
})
787+
788+
})
789+
790+
frame.$.pjax({
791+
url: "hello.html",
792+
container: "#main"
793+
})
794+
795+
})
796+
797+
asyncTest("clicking back while loading maintains history", function() {
798+
var frame = this.frame
799+
800+
equal(frame.location.pathname, "/home.html")
801+
equal(frame.document.title, "Home")
802+
803+
frame.$("#main").on('pjax:timeout', function(e) {
804+
e.preventDefault();
805+
})
806+
807+
frame.$("#main").one('pjax:complete', function() {
808+
809+
equal(frame.location.pathname, "/hello.html")
810+
equal(frame.document.title, "Hello")
811+
812+
frame.$("#main").one('pjax:send', function() {
813+
814+
// don't use goBack here, because pjax:end isn't triggered
815+
// when clicking back while loading
816+
817+
frame.history.back();
818+
819+
setTimeout(function() {
820+
equal(frame.location.pathname, "/home.html")
821+
equal(frame.document.title, "Home")
822+
823+
frame.history.forward()
824+
825+
setTimeout(function() {
826+
equal(frame.location.pathname, "/hello.html")
827+
equal(frame.document.title, "Hello")
828+
start()
829+
}, 250)
830+
831+
}, 1500)
832+
833+
834+
})
835+
836+
frame.$.pjax({
837+
url: "timeout.html",
838+
container: "#main"
839+
})
840+
841+
})
842+
843+
frame.$.pjax({
844+
url: "hello.html",
845+
container: "#main"
846+
})
847+
})
848+
744849
asyncTest("popstate going back to page", function() {
745850
var frame = this.frame
746851

0 commit comments

Comments
 (0)