ajax_deferred.html 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
  6. <link rel="stylesheet" href="test.css">
  7. <title>Zepto Ajax deferred</title>
  8. <script src="../vendor/evidence.js"></script>
  9. <script src="evidence_runner.js"></script>
  10. <script>
  11. // avoid caching
  12. (function(){
  13. function load(scripts){
  14. scripts.split(' ').forEach(function(script){
  15. document.write('<script src="../src/'+script+'.js?'+(+new Date)+'"></scr'+'ipt>')
  16. })
  17. }
  18. load('zepto event ajax ie callbacks deferred')
  19. })()
  20. </script>
  21. </head>
  22. <body>
  23. <h1>Zepto Ajax deferred</h1>
  24. <p id="results">
  25. Running… see browser console for results
  26. </p>
  27. <div id="fixtures">
  28. </div>
  29. <script>
  30. (function(){
  31. function toArray(list) {
  32. if ($.type(list) == "string") return list.split(/(?:\s*,\s*|\s+)/)
  33. else return list
  34. }
  35. Evidence.Assertions.assertEqualList = function(expected, actual, message) {
  36. var expectedList = toArray(expected),
  37. actualList = toArray(actual)
  38. this._assertExpression(
  39. expectedList.join(' ') == actualList.join(' '),
  40. message || "Lists don't match.",
  41. 'Expected %o, got %o.', expectedList, actualList
  42. )
  43. }
  44. var OriginalXHR = $.ajaxSettings.xhr
  45. function MockXHR() {
  46. this.requestHeaders = {}
  47. this.responseHeaders = {}
  48. MockXHR.last = this
  49. }
  50. MockXHR.prototype = {
  51. open: function(method, url) {
  52. this.method = method
  53. this.url = url
  54. },
  55. setRequestHeader: function(name, value) {
  56. this.requestHeaders[name.toLowerCase()] = value
  57. },
  58. getResponseHeader: function(name) {
  59. return this.responseHeaders[name]
  60. },
  61. send: function(data) {
  62. this.data = data
  63. },
  64. abort: function() {
  65. this.aborted = true
  66. },
  67. ready: function(readyState, status, responseText, headers) {
  68. this.readyState = readyState
  69. this.status = status
  70. this.responseText = responseText
  71. $.extend(this.responseHeaders, headers)
  72. this.onreadystatechange()
  73. },
  74. onreadystatechange: function() {}
  75. }
  76. Evidence('ZeptoAjaxDeferred', {
  77. setUp: function() {
  78. $.ajaxSettings.xhr = function(){ return new MockXHR }
  79. },
  80. tearDown: function() {
  81. $.ajaxSettings.xhr = OriginalXHR
  82. $(document).off()
  83. $.active = 0
  84. for (var key in window)
  85. if (/^Zepto\d+$/.test(key) && window[key] === undefined)
  86. delete window[key]
  87. },
  88. testDone: function(t) {
  89. var log = []
  90. var xhr = $.ajax({
  91. success: function() { log.push('success') }
  92. })
  93. .done(function(response, status, gotXhr){
  94. t.assertEqual('hello', response)
  95. t.assertEqual('success', status)
  96. t.assertIdentical(xhr, gotXhr)
  97. log.push('done1')
  98. })
  99. .fail(function(){ log.push('fail') })
  100. .always(function(){ log.push('always') })
  101. .done(function(){ log.push('done2') })
  102. xhr.ready(3, 0)
  103. t.assertEqual(0, log.length)
  104. xhr.ready(4, 200, 'hello')
  105. t.assertEqualList('success done1 always done2', log)
  106. xhr.done(function(){ log.push('done3') })
  107. t.assertEqualList('success done1 always done2 done3', log)
  108. },
  109. testFail: function(t) {
  110. var log = []
  111. var xhr = $.ajax({
  112. error: function() { log.push('error') }
  113. })
  114. .done(function(){ log.push('done') })
  115. .fail(function(gotXhr, status, error){
  116. t.assertIdentical(xhr, gotXhr)
  117. t.assertEqual('error', status)
  118. log.push('fail1')
  119. })
  120. .always(function(){ log.push('always') })
  121. .fail(function(){ log.push('fail2') })
  122. xhr.ready(3, 0)
  123. t.assertEqual(0, log.length)
  124. xhr.ready(4, 404)
  125. t.assertEqualList('error fail1 always fail2', log)
  126. xhr.fail(function(){ log.push('fail3') })
  127. t.assertEqualList('error fail1 always fail2 fail3', log)
  128. },
  129. testAbortInBeforeSend: function(t) {
  130. var log = []
  131. var xhr = $.ajax({
  132. beforeSend: function() { return false }
  133. })
  134. xhr.done(function(){ log.push('done') })
  135. .fail(function(gotXhr, status, error){
  136. t.assertIdentical(xhr, gotXhr)
  137. t.assertEqual('abort', status)
  138. log.push('fail1')
  139. })
  140. t.assertEqualList('fail1', log)
  141. },
  142. testJSONP: function(t) {
  143. var log = []
  144. t.pause()
  145. var xhr = $.ajax({
  146. url: 'jsonp',
  147. dataType: 'jsonp',
  148. success: function() { log.push('success') }
  149. })
  150. .done(function(){ log.push('done1') })
  151. .always(function(){
  152. setTimeout(function(){
  153. t.resume(function(){
  154. t.assertEqualList('success done1', log)
  155. xhr.done(function(){ log.push('done2') })
  156. t.assertEqualList('success done1 done2', log)
  157. })
  158. }, 10)
  159. })
  160. t.assertEqual(0, log.length)
  161. },
  162. testJSONPAbortInBeforeSend: function(t) {
  163. var log = []
  164. var xhr = $.ajax({
  165. url: 'jsonp',
  166. dataType: 'jsonp',
  167. beforeSend: function() { return false }
  168. })
  169. xhr.done(function(){ log.push('done') })
  170. .fail(function(gotXhr, status, error){
  171. t.assertIdentical(xhr, gotXhr)
  172. t.assertEqual('abort', status)
  173. log.push('fail1')
  174. })
  175. t.assertEqualList('fail1', log)
  176. }
  177. })
  178. })()
  179. </script>
  180. </body>
  181. </html>