123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462 |
- // $.pjax fallback tests should run on both pushState and
- // non-pushState compatible browsers.
- $.each([true, false], function() {
- var disabled = this == false
- var s = disabled ? " (disabled)" : ""
- var ua = navigator.userAgent
- var safari = ua.match("Safari") && !ua.match("Chrome") && !ua.match("Edge")
- var chrome = ua.match("Chrome") && !ua.match("Edge")
- module("$.pjax fallback"+s, {
- setup: function() {
- var self = this
- stop()
- this.loaded = function(frame) {
- self.frame = frame
- self.loaded = $.noop
- start()
- }
- window.iframeLoad = function(frame) {
- setTimeout(function() {
- if (disabled && frame.$ && frame.$.pjax) frame.$.pjax.disable()
- self.loaded(frame)
- }, 0)
- }
- $("#qunit-fixture").append("<iframe src='home.html'>")
- },
- teardown: function() {
- delete window.iframeLoad
- }
- })
- asyncTest("sets new url"+s, function() {
- var frame = this.frame
- this.loaded = function() {
- equal(frame.location.pathname, "/hello.html")
- start()
- }
- frame.$.pjax({
- url: "hello.html",
- container: "#main"
- })
- })
- asyncTest("sets new url for function"+s, function() {
- var frame = this.frame
- this.loaded = function() {
- equal(frame.location.pathname, "/hello.html")
- start()
- }
- frame.$.pjax({
- url: function() { return "hello.html" },
- container: "#main"
- })
- })
- asyncTest("updates container html"+s, function() {
- var frame = this.frame
- this.loaded = function(frame) {
- equal(frame.$("#main p").html(), "Hello!")
- start()
- }
- frame.$.pjax({
- url: "/hello.html",
- container: "#main"
- })
- })
- asyncTest("sets title to response <title>"+s, function() {
- var frame = this.frame
- this.loaded = function(frame) {
- equal(frame.document.title, "Hello")
- start()
- }
- frame.$.pjax({
- url: "/hello.html",
- container: "#main"
- })
- })
- asyncTest("sends correct HTTP referer"+s, function() {
- var frame = this.frame
- this.loaded = function(frame) {
- var referer = frame.document.getElementById("referer").textContent
- ok(referer.match("/home.html"), referer)
- start()
- }
- frame.$.pjax({
- url: "/referer.html",
- container: "#main"
- })
- })
- asyncTest("scrolls to top of the page"+s, function() {
- var frame = this.frame
- frame.window.scrollTo(0, 100)
- equal(frame.window.pageYOffset, 100)
- this.loaded = function(frame) {
- equal(frame.window.pageYOffset, 0)
- start()
- }
- frame.$.pjax({
- url: "/long.html",
- container: "#main"
- })
- })
- asyncTest("scrolls to anchor at top page"+s, function() {
- var frame = this.frame
- equal(frame.window.pageYOffset, 0)
- this.loaded = function(frame) {
- setTimeout(function() {
- equal(frame.location.pathname, "/anchor.html")
- equal(frame.location.hash, "#top")
- equal(frame.window.pageYOffset, 8)
- start()
- }, 100)
- }
- frame.$.pjax({
- url: "/anchor.html#top",
- container: "#main"
- })
- if (disabled) {
- equal(frame.location.pathname, "/home.html")
- equal(frame.location.href.indexOf("#"), -1)
- } else {
- equal(frame.location.pathname, "/anchor.html")
- equal(frame.location.hash, "#top")
- }
- })
- asyncTest("empty anchor doesn't scroll page"+s, function() {
- var frame = this.frame
- equal(frame.window.pageYOffset, 0)
- this.loaded = function(frame) {
- setTimeout(function() {
- equal(frame.window.pageYOffset, 0)
- start()
- }, 10)
- }
- frame.$.pjax({
- url: "/anchor.html#",
- container: "#main"
- })
- })
- asyncTest("scrolls to anchor at bottom page"+s, function() {
- var frame = this.frame
- equal(frame.window.pageYOffset, 0)
- this.loaded = function(frame) {
- setTimeout(function() {
- equal(frame.window.pageYOffset, 10008)
- start()
- }, 10)
- }
- frame.$.pjax({
- url: "/anchor.html#bottom",
- container: "#main"
- })
- })
- asyncTest("scrolls to named encoded anchor"+s, function() {
- var frame = this.frame
- equal(frame.window.pageYOffset, 0)
- this.loaded = function(frame) {
- setTimeout(function() {
- equal(frame.window.pageYOffset, 10008)
- start()
- }, 10)
- }
- frame.$.pjax({
- url: "/anchor.html#%62%6F%74%74%6F%6D",
- container: "#main"
- })
- })
- asyncTest("sets GET method"+s, function() {
- var frame = this.frame
- this.loaded = function() {
- equal(frame.location.pathname, "/env.html")
- var env = JSON.parse(frame.$("#env").text())
- equal(env['REQUEST_METHOD'], "GET")
- start()
- }
- frame.$.pjax({
- type: 'GET',
- url: "env.html",
- container: "#main"
- })
- })
- asyncTest("sets POST method"+s, function() {
- var frame = this.frame
- this.loaded = function() {
- equal(frame.location.pathname, "/env.html")
- var env = JSON.parse(frame.$("#env").text())
- equal(env['REQUEST_METHOD'], "POST")
- start()
- }
- frame.$.pjax({
- type: 'POST',
- url: "env.html",
- container: "#main"
- })
- })
- asyncTest("sets PUT method"+s, function() {
- var frame = this.frame
- this.loaded = function() {
- equal(frame.location.pathname, "/env.html")
- var env = JSON.parse(frame.$("#env").text())
- equal(env['REQUEST_METHOD'], "PUT")
- start()
- }
- frame.$.pjax({
- type: 'PUT',
- url: "env.html",
- container: "#main"
- })
- })
- asyncTest("sets DELETE method"+s, function() {
- var frame = this.frame
- this.loaded = function() {
- equal(frame.location.pathname, "/env.html")
- var env = JSON.parse(frame.$("#env").text())
- equal(env['REQUEST_METHOD'], "DELETE")
- start()
- }
- frame.$.pjax({
- type: 'DELETE',
- url: "env.html",
- container: "#main"
- })
- })
- asyncTest("GET with data object"+s, function() {
- var frame = this.frame
- this.loaded = function() {
- equal(frame.location.pathname, "/env.html")
- equal(frame.location.search, "?foo=bar")
- var env = JSON.parse(frame.$("#env").text())
- equal(env['REQUEST_METHOD'], "GET")
- equal(env['rack.request.query_hash']['foo'], 'bar')
- start()
- }
- frame.$.pjax({
- type: 'GET',
- url: "env.html",
- data: {foo: 'bar'},
- container: "#main"
- })
- })
- asyncTest("POST with data object"+s, function() {
- var frame = this.frame
- this.loaded = function() {
- equal(frame.location.pathname, "/env.html")
- equal(frame.location.search, "")
- var env = JSON.parse(frame.$("#env").text())
- equal(env['REQUEST_METHOD'], "POST")
- equal(env['rack.request.form_hash']['foo'], 'bar')
- start()
- }
- frame.$.pjax({
- type: 'POST',
- url: "env.html",
- data: {foo: 'bar'},
- container: "#main"
- })
- })
- asyncTest("GET with data array"+s, function() {
- var frame = this.frame
- this.loaded = function() {
- equal(frame.location.pathname, "/env.html")
- equal(frame.location.search, "?foo%5B%5D=bar&foo%5B%5D=baz")
- var env = JSON.parse(frame.$("#env").text())
- equal(env['REQUEST_METHOD'], "GET")
- var expected = {'foo': ['bar', 'baz']}
- if (!disabled) expected._pjax = "#main"
- deepEqual(env['rack.request.query_hash'], expected)
- start()
- }
- frame.$.pjax({
- type: 'GET',
- url: "env.html",
- data: [{name: "foo[]", value: "bar"}, {name: "foo[]", value: "baz"}],
- container: "#main"
- })
- })
- asyncTest("POST with data array"+s, function() {
- var frame = this.frame
- this.loaded = function() {
- equal(frame.location.pathname, "/env.html")
- equal(frame.location.search, "")
- var env = JSON.parse(frame.$("#env").text())
- equal(env['REQUEST_METHOD'], "POST")
- var expected = {'foo': ['bar', 'baz']}
- if (!disabled) expected._pjax = "#main"
- deepEqual(env['rack.request.form_hash'], expected)
- start()
- }
- frame.$.pjax({
- type: 'POST',
- url: "env.html",
- data: [{name: "foo[]", value: "bar"}, {name: "foo[]", value: "baz"}],
- container: "#main"
- })
- })
- asyncTest("GET with data string"+s, function() {
- var frame = this.frame
- this.loaded = function() {
- equal(frame.location.pathname, "/env.html")
- equal(frame.location.search, "?foo=bar")
- var env = JSON.parse(frame.$("#env").text())
- equal(env['REQUEST_METHOD'], "GET")
- equal(env['rack.request.query_hash']['foo'], 'bar')
- start()
- }
- frame.$.pjax({
- type: 'GET',
- url: "env.html",
- data: "foo=bar",
- container: "#main"
- })
- })
- asyncTest("POST with data string"+s, function() {
- var frame = this.frame
- this.loaded = function() {
- equal(frame.location.pathname, "/env.html")
- equal(frame.location.search, "")
- var env = JSON.parse(frame.$("#env").text())
- equal(env['REQUEST_METHOD'], "POST")
- equal(env['rack.request.form_hash']['foo'], 'bar')
- start()
- }
- frame.$.pjax({
- type: 'POST',
- url: "env.html",
- data: "foo=bar",
- container: "#main"
- })
- })
- asyncTest("handle form submit"+s, function() {
- var frame = this.frame
- frame.$(frame.document).on("submit", "form", function(event) {
- frame.$.pjax.submit(event, "#main")
- })
- this.loaded = function() {
- var env = JSON.parse(frame.$("#env").text())
- var expected = {foo: "1", bar: "2"}
- if (!disabled) expected._pjax = "#main"
- deepEqual(env['rack.request.query_hash'], expected)
- start()
- }
- frame.$("form").submit()
- })
- asyncTest("browser URL is correct after redirect"+s, function() {
- var frame = this.frame
- this.loaded = function() {
- equal(frame.location.pathname, "/hello.html")
- var expectedHash = safari && disabled ? "" : "#new"
- equal(frame.location.hash, expectedHash)
- start()
- }
- frame.$.pjax({
- url: "redirect.html#new",
- container: "#main"
- })
- })
- asyncTest("server can't affect anchor after redirect"+s, function() {
- var frame = this.frame
- this.loaded = function() {
- equal(frame.location.pathname, "/hello.html")
- var expectedHash = safari && disabled ? "" : "#new"
- equal(frame.location.hash, expectedHash)
- start()
- }
- frame.$.pjax({
- url: "redirect.html?anchor=server#new",
- container: "#main"
- })
- })
- })
|