fn_pjax.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. if ($.support.pjax) {
  2. module("$.fn.pjax", {
  3. setup: function() {
  4. var self = this
  5. stop()
  6. window.iframeLoad = function(frame) {
  7. self.frame = frame
  8. window.iframeLoad = $.noop
  9. start()
  10. }
  11. $("#qunit-fixture").append("<iframe src='home.html'>")
  12. },
  13. teardown: function() {
  14. delete window.iframeLoad
  15. }
  16. })
  17. asyncTest("pushes new url", function() {
  18. var frame = this.frame
  19. frame.$("#main").pjax("a", "#main").on("pjax:end", function() {
  20. equal(frame.location.pathname, "/dinosaurs.html")
  21. start()
  22. })
  23. frame.$("a[href='/dinosaurs.html']").click()
  24. })
  25. asyncTest("replaces container html from response data", function() {
  26. var frame = this.frame
  27. frame.$("#main").pjax("a", "#main").on("pjax:end", function() {
  28. equal(frame.$("iframe").attr('title'), "YouTube video player")
  29. start()
  30. })
  31. frame.$("a[href='/dinosaurs.html']").click()
  32. })
  33. asyncTest("sets title to response title tag", function() {
  34. var frame = this.frame
  35. frame.$("#main").pjax("a", "#main").on("pjax:end", function() {
  36. equal(frame.document.title, "dinosaurs")
  37. start()
  38. })
  39. frame.$("a[href='/dinosaurs.html']").trigger('click')
  40. })
  41. asyncTest("uses second argument as options", function() {
  42. var frame = this.frame
  43. frame.$("#main").pjax("a", {container: "#main", push: true}).on("pjax:end", function() {
  44. equal(frame.location.pathname, "/dinosaurs.html")
  45. start()
  46. })
  47. frame.$("a[href='/dinosaurs.html']").click()
  48. })
  49. asyncTest("uses second argument as container and third as options", function() {
  50. var frame = this.frame
  51. frame.$("body").pjax("a", "#main", {push: true}).on("pjax:end", function() {
  52. equal(frame.location.pathname, "/dinosaurs.html")
  53. start()
  54. })
  55. frame.$("a[href='/dinosaurs.html']").click()
  56. })
  57. asyncTest("defaults to data-pjax container", function() {
  58. var frame = this.frame
  59. frame.$("a").attr('data-pjax', "#main")
  60. frame.$("body").pjax("a")
  61. frame.$("#main").on("pjax:end", function() {
  62. equal(frame.location.pathname, "/dinosaurs.html")
  63. start()
  64. })
  65. frame.$("a[href='/dinosaurs.html']").click()
  66. })
  67. asyncTest("sets relatedTarget to clicked element", function() {
  68. var frame = this.frame
  69. frame.$("#main").pjax("a", "#main")
  70. var link = frame.$("a[href='/dinosaurs.html']")[0]
  71. frame.$("#main").on("pjax:end", function(event, xhr, options) {
  72. equal(link, event.relatedTarget)
  73. start()
  74. })
  75. frame.$(link).click()
  76. })
  77. asyncTest("doesn't ignore left click", function() {
  78. var frame = this.frame
  79. frame.$("#main").pjax("a", "#main")
  80. var event = frame.$.Event('click')
  81. event.which = 0
  82. frame.$("a[href='/dinosaurs.html']").trigger(event)
  83. ok(event.isDefaultPrevented())
  84. start()
  85. })
  86. asyncTest("ignores middle clicks", function() {
  87. var frame = this.frame
  88. frame.$("#main").pjax("a", "#main")
  89. var event = frame.$.Event('click')
  90. event.which = 3
  91. frame.$("a[href='/dinosaurs.html']").trigger(event)
  92. ok(!event.isDefaultPrevented())
  93. start()
  94. })
  95. asyncTest("ignores command clicks", function() {
  96. var frame = this.frame
  97. frame.$("#main").pjax("a")
  98. var event = frame.$.Event('click')
  99. event.metaKey = true
  100. frame.$("a[href='/dinosaurs.html']").trigger(event)
  101. ok(!event.isDefaultPrevented())
  102. start()
  103. })
  104. asyncTest("ignores ctrl clicks", function() {
  105. var frame = this.frame
  106. frame.$("#main").pjax("a")
  107. var event = frame.$.Event('click')
  108. event.ctrlKey = true
  109. frame.$("a[href='/dinosaurs.html']").trigger(event)
  110. ok(!event.isDefaultPrevented())
  111. start()
  112. })
  113. asyncTest("ignores cross origin links", function() {
  114. var frame = this.frame
  115. frame.$("#main").pjax("a")
  116. var event = frame.$.Event('click')
  117. frame.$("a[href='https://www.google.com/']").trigger(event)
  118. notEqual(event.result, false)
  119. start()
  120. })
  121. asyncTest("ignores same page anchors", function() {
  122. var event, frame = this.frame
  123. frame.$("#main").pjax("a")
  124. event = frame.$.Event('click')
  125. frame.$("a[href='#main']").trigger(event)
  126. equal(event.isDefaultPrevented(), false)
  127. event = frame.$.Event('click')
  128. frame.$("a[href='#']").trigger(event)
  129. equal(event.isDefaultPrevented(), false)
  130. start()
  131. })
  132. asyncTest("ignores same page anchors from URL that has hash", function() {
  133. var event, frame = this.frame
  134. frame.window.location = "#foo"
  135. frame.$("#main").pjax("a")
  136. event = frame.$.Event('click')
  137. frame.$("a[href='#main']").trigger(event)
  138. equal(event.isDefaultPrevented(), false)
  139. event = frame.$.Event('click')
  140. frame.$("a[href='#']").trigger(event)
  141. equal(event.isDefaultPrevented(), false)
  142. start()
  143. })
  144. asyncTest("ignores event with prevented default", function() {
  145. var frame = this.frame
  146. var eventIgnored = true
  147. frame.$("#main").pjax("a").on("pjax:click", function() {
  148. eventIgnored = false
  149. })
  150. frame.$("a[href='/dinosaurs.html']").on("click", function(event) {
  151. event.preventDefault()
  152. setTimeout(function() {
  153. ok(eventIgnored, "Event with prevented default ignored")
  154. start()
  155. }, 10)
  156. })
  157. frame.$("a[href='/dinosaurs.html']").click()
  158. })
  159. asyncTest("triggers pjax:click event from link", function() {
  160. var frame = this.frame
  161. frame.$("#main").pjax("a", "#main").on("pjax:click", function(event, options) {
  162. ok(event)
  163. equal(options.container, "#main")
  164. ok(options.url.match("/dinosaurs.html"))
  165. start()
  166. })
  167. frame.$("a[href='/dinosaurs.html']").click()
  168. })
  169. asyncTest("triggers pjax:clicked event from link", function() {
  170. var frame = this.frame
  171. frame.$("#main").pjax("a", "#main").on("pjax:clicked", function(event, options) {
  172. ok(event)
  173. equal(options.container, "#main")
  174. ok(options.url.match("/dinosaurs.html"))
  175. start()
  176. })
  177. frame.$("a[href='/dinosaurs.html']").click()
  178. })
  179. }