pjax_fallback.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. // $.pjax fallback tests should run on both pushState and
  2. // non-pushState compatible browsers.
  3. $.each([true, false], function() {
  4. var disabled = this == false
  5. var s = disabled ? " (disabled)" : ""
  6. var ua = navigator.userAgent
  7. var safari = ua.match("Safari") && !ua.match("Chrome") && !ua.match("Edge")
  8. var chrome = ua.match("Chrome") && !ua.match("Edge")
  9. module("$.pjax fallback"+s, {
  10. setup: function() {
  11. var self = this
  12. stop()
  13. this.loaded = function(frame) {
  14. self.frame = frame
  15. self.loaded = $.noop
  16. start()
  17. }
  18. window.iframeLoad = function(frame) {
  19. setTimeout(function() {
  20. if (disabled && frame.$ && frame.$.pjax) frame.$.pjax.disable()
  21. self.loaded(frame)
  22. }, 0)
  23. }
  24. $("#qunit-fixture").append("<iframe src='home.html'>")
  25. },
  26. teardown: function() {
  27. delete window.iframeLoad
  28. }
  29. })
  30. asyncTest("sets new url"+s, function() {
  31. var frame = this.frame
  32. this.loaded = function() {
  33. equal(frame.location.pathname, "/hello.html")
  34. start()
  35. }
  36. frame.$.pjax({
  37. url: "hello.html",
  38. container: "#main"
  39. })
  40. })
  41. asyncTest("sets new url for function"+s, function() {
  42. var frame = this.frame
  43. this.loaded = function() {
  44. equal(frame.location.pathname, "/hello.html")
  45. start()
  46. }
  47. frame.$.pjax({
  48. url: function() { return "hello.html" },
  49. container: "#main"
  50. })
  51. })
  52. asyncTest("updates container html"+s, function() {
  53. var frame = this.frame
  54. this.loaded = function(frame) {
  55. equal(frame.$("#main p").html(), "Hello!")
  56. start()
  57. }
  58. frame.$.pjax({
  59. url: "/hello.html",
  60. container: "#main"
  61. })
  62. })
  63. asyncTest("sets title to response <title>"+s, function() {
  64. var frame = this.frame
  65. this.loaded = function(frame) {
  66. equal(frame.document.title, "Hello")
  67. start()
  68. }
  69. frame.$.pjax({
  70. url: "/hello.html",
  71. container: "#main"
  72. })
  73. })
  74. asyncTest("sends correct HTTP referer"+s, function() {
  75. var frame = this.frame
  76. this.loaded = function(frame) {
  77. var referer = frame.document.getElementById("referer").textContent
  78. ok(referer.match("/home.html"), referer)
  79. start()
  80. }
  81. frame.$.pjax({
  82. url: "/referer.html",
  83. container: "#main"
  84. })
  85. })
  86. asyncTest("scrolls to top of the page"+s, function() {
  87. var frame = this.frame
  88. frame.window.scrollTo(0, 100)
  89. equal(frame.window.pageYOffset, 100)
  90. this.loaded = function(frame) {
  91. equal(frame.window.pageYOffset, 0)
  92. start()
  93. }
  94. frame.$.pjax({
  95. url: "/long.html",
  96. container: "#main"
  97. })
  98. })
  99. asyncTest("scrolls to anchor at top page"+s, function() {
  100. var frame = this.frame
  101. equal(frame.window.pageYOffset, 0)
  102. this.loaded = function(frame) {
  103. setTimeout(function() {
  104. equal(frame.location.pathname, "/anchor.html")
  105. equal(frame.location.hash, "#top")
  106. equal(frame.window.pageYOffset, 8)
  107. start()
  108. }, 100)
  109. }
  110. frame.$.pjax({
  111. url: "/anchor.html#top",
  112. container: "#main"
  113. })
  114. if (disabled) {
  115. equal(frame.location.pathname, "/home.html")
  116. equal(frame.location.href.indexOf("#"), -1)
  117. } else {
  118. equal(frame.location.pathname, "/anchor.html")
  119. equal(frame.location.hash, "#top")
  120. }
  121. })
  122. asyncTest("empty anchor doesn't scroll page"+s, function() {
  123. var frame = this.frame
  124. equal(frame.window.pageYOffset, 0)
  125. this.loaded = function(frame) {
  126. setTimeout(function() {
  127. equal(frame.window.pageYOffset, 0)
  128. start()
  129. }, 10)
  130. }
  131. frame.$.pjax({
  132. url: "/anchor.html#",
  133. container: "#main"
  134. })
  135. })
  136. asyncTest("scrolls to anchor at bottom page"+s, function() {
  137. var frame = this.frame
  138. equal(frame.window.pageYOffset, 0)
  139. this.loaded = function(frame) {
  140. setTimeout(function() {
  141. equal(frame.window.pageYOffset, 10008)
  142. start()
  143. }, 10)
  144. }
  145. frame.$.pjax({
  146. url: "/anchor.html#bottom",
  147. container: "#main"
  148. })
  149. })
  150. asyncTest("scrolls to named encoded anchor"+s, function() {
  151. var frame = this.frame
  152. equal(frame.window.pageYOffset, 0)
  153. this.loaded = function(frame) {
  154. setTimeout(function() {
  155. equal(frame.window.pageYOffset, 10008)
  156. start()
  157. }, 10)
  158. }
  159. frame.$.pjax({
  160. url: "/anchor.html#%62%6F%74%74%6F%6D",
  161. container: "#main"
  162. })
  163. })
  164. asyncTest("sets GET method"+s, function() {
  165. var frame = this.frame
  166. this.loaded = function() {
  167. equal(frame.location.pathname, "/env.html")
  168. var env = JSON.parse(frame.$("#env").text())
  169. equal(env['REQUEST_METHOD'], "GET")
  170. start()
  171. }
  172. frame.$.pjax({
  173. type: 'GET',
  174. url: "env.html",
  175. container: "#main"
  176. })
  177. })
  178. asyncTest("sets POST method"+s, function() {
  179. var frame = this.frame
  180. this.loaded = function() {
  181. equal(frame.location.pathname, "/env.html")
  182. var env = JSON.parse(frame.$("#env").text())
  183. equal(env['REQUEST_METHOD'], "POST")
  184. start()
  185. }
  186. frame.$.pjax({
  187. type: 'POST',
  188. url: "env.html",
  189. container: "#main"
  190. })
  191. })
  192. asyncTest("sets PUT method"+s, function() {
  193. var frame = this.frame
  194. this.loaded = function() {
  195. equal(frame.location.pathname, "/env.html")
  196. var env = JSON.parse(frame.$("#env").text())
  197. equal(env['REQUEST_METHOD'], "PUT")
  198. start()
  199. }
  200. frame.$.pjax({
  201. type: 'PUT',
  202. url: "env.html",
  203. container: "#main"
  204. })
  205. })
  206. asyncTest("sets DELETE method"+s, function() {
  207. var frame = this.frame
  208. this.loaded = function() {
  209. equal(frame.location.pathname, "/env.html")
  210. var env = JSON.parse(frame.$("#env").text())
  211. equal(env['REQUEST_METHOD'], "DELETE")
  212. start()
  213. }
  214. frame.$.pjax({
  215. type: 'DELETE',
  216. url: "env.html",
  217. container: "#main"
  218. })
  219. })
  220. asyncTest("GET with data object"+s, function() {
  221. var frame = this.frame
  222. this.loaded = function() {
  223. equal(frame.location.pathname, "/env.html")
  224. equal(frame.location.search, "?foo=bar")
  225. var env = JSON.parse(frame.$("#env").text())
  226. equal(env['REQUEST_METHOD'], "GET")
  227. equal(env['rack.request.query_hash']['foo'], 'bar')
  228. start()
  229. }
  230. frame.$.pjax({
  231. type: 'GET',
  232. url: "env.html",
  233. data: {foo: 'bar'},
  234. container: "#main"
  235. })
  236. })
  237. asyncTest("POST with data object"+s, function() {
  238. var frame = this.frame
  239. this.loaded = function() {
  240. equal(frame.location.pathname, "/env.html")
  241. equal(frame.location.search, "")
  242. var env = JSON.parse(frame.$("#env").text())
  243. equal(env['REQUEST_METHOD'], "POST")
  244. equal(env['rack.request.form_hash']['foo'], 'bar')
  245. start()
  246. }
  247. frame.$.pjax({
  248. type: 'POST',
  249. url: "env.html",
  250. data: {foo: 'bar'},
  251. container: "#main"
  252. })
  253. })
  254. asyncTest("GET with data array"+s, function() {
  255. var frame = this.frame
  256. this.loaded = function() {
  257. equal(frame.location.pathname, "/env.html")
  258. equal(frame.location.search, "?foo%5B%5D=bar&foo%5B%5D=baz")
  259. var env = JSON.parse(frame.$("#env").text())
  260. equal(env['REQUEST_METHOD'], "GET")
  261. var expected = {'foo': ['bar', 'baz']}
  262. if (!disabled) expected._pjax = "#main"
  263. deepEqual(env['rack.request.query_hash'], expected)
  264. start()
  265. }
  266. frame.$.pjax({
  267. type: 'GET',
  268. url: "env.html",
  269. data: [{name: "foo[]", value: "bar"}, {name: "foo[]", value: "baz"}],
  270. container: "#main"
  271. })
  272. })
  273. asyncTest("POST with data array"+s, function() {
  274. var frame = this.frame
  275. this.loaded = function() {
  276. equal(frame.location.pathname, "/env.html")
  277. equal(frame.location.search, "")
  278. var env = JSON.parse(frame.$("#env").text())
  279. equal(env['REQUEST_METHOD'], "POST")
  280. var expected = {'foo': ['bar', 'baz']}
  281. if (!disabled) expected._pjax = "#main"
  282. deepEqual(env['rack.request.form_hash'], expected)
  283. start()
  284. }
  285. frame.$.pjax({
  286. type: 'POST',
  287. url: "env.html",
  288. data: [{name: "foo[]", value: "bar"}, {name: "foo[]", value: "baz"}],
  289. container: "#main"
  290. })
  291. })
  292. asyncTest("GET with data string"+s, function() {
  293. var frame = this.frame
  294. this.loaded = function() {
  295. equal(frame.location.pathname, "/env.html")
  296. equal(frame.location.search, "?foo=bar")
  297. var env = JSON.parse(frame.$("#env").text())
  298. equal(env['REQUEST_METHOD'], "GET")
  299. equal(env['rack.request.query_hash']['foo'], 'bar')
  300. start()
  301. }
  302. frame.$.pjax({
  303. type: 'GET',
  304. url: "env.html",
  305. data: "foo=bar",
  306. container: "#main"
  307. })
  308. })
  309. asyncTest("POST with data string"+s, function() {
  310. var frame = this.frame
  311. this.loaded = function() {
  312. equal(frame.location.pathname, "/env.html")
  313. equal(frame.location.search, "")
  314. var env = JSON.parse(frame.$("#env").text())
  315. equal(env['REQUEST_METHOD'], "POST")
  316. equal(env['rack.request.form_hash']['foo'], 'bar')
  317. start()
  318. }
  319. frame.$.pjax({
  320. type: 'POST',
  321. url: "env.html",
  322. data: "foo=bar",
  323. container: "#main"
  324. })
  325. })
  326. asyncTest("handle form submit"+s, function() {
  327. var frame = this.frame
  328. frame.$(frame.document).on("submit", "form", function(event) {
  329. frame.$.pjax.submit(event, "#main")
  330. })
  331. this.loaded = function() {
  332. var env = JSON.parse(frame.$("#env").text())
  333. var expected = {foo: "1", bar: "2"}
  334. if (!disabled) expected._pjax = "#main"
  335. deepEqual(env['rack.request.query_hash'], expected)
  336. start()
  337. }
  338. frame.$("form").submit()
  339. })
  340. asyncTest("browser URL is correct after redirect"+s, function() {
  341. var frame = this.frame
  342. this.loaded = function() {
  343. equal(frame.location.pathname, "/hello.html")
  344. var expectedHash = safari && disabled ? "" : "#new"
  345. equal(frame.location.hash, expectedHash)
  346. start()
  347. }
  348. frame.$.pjax({
  349. url: "redirect.html#new",
  350. container: "#main"
  351. })
  352. })
  353. asyncTest("server can't affect anchor after redirect"+s, function() {
  354. var frame = this.frame
  355. this.loaded = function() {
  356. equal(frame.location.pathname, "/hello.html")
  357. var expectedHash = safari && disabled ? "" : "#new"
  358. equal(frame.location.hash, expectedHash)
  359. start()
  360. }
  361. frame.$.pjax({
  362. url: "redirect.html?anchor=server#new",
  363. container: "#main"
  364. })
  365. })
  366. })