/js/YUI/tests/jsonp/tests/testsuite.js

https://github.com/nottrobin/Emulate-HTML5 · JavaScript · 325 lines · 270 code · 54 blank · 1 comment · 9 complexity · c41cbfb3dcd9311d62af874ed593fb53 MD5 · raw file

  1. YUI.add('jsonp-test', function(Y) {
  2. var suite = new Y.Test.Suite("Y.JSONPRequest and Y.jsonp");
  3. suite.add(new Y.Test.Case({
  4. name : "Callback in URL",
  5. "callback in URL should be executed": function () {
  6. var self = this;
  7. Y.config.win.globalFunction = function (json) {
  8. self.resume(function () {
  9. Y.config.win.globalFunction = undefined;
  10. Y.Assert.isObject(json);
  11. });
  12. };
  13. Y.jsonp("server/service.php?callback=globalFunction");
  14. self.wait(300);
  15. }
  16. }));
  17. suite.add(new Y.Test.Case({
  18. name : "Callback function",
  19. "callback function as second arg should be success handler": function () {
  20. var self = this;
  21. Y.jsonp("server/service.php?callback={callback}", function (json) {
  22. self.resume(function () {
  23. Y.Assert.isObject(json);
  24. });
  25. });
  26. self.wait(300);
  27. },
  28. "inline callback should be replaced if function passed": function () {
  29. var self = this;
  30. Y.config.win.globalFunction = function (json) {
  31. self.resume(function () {
  32. Y.config.win.globalFunction = undefined;
  33. Y.Assert.fail("inline function should not be used");
  34. });
  35. };
  36. Y.jsonp("server/service.php?callback=globalFunction", function (data) {
  37. self.resume(function () {
  38. Y.config.win.globalFunction = undefined;
  39. Y.Assert.isObject(data);
  40. });
  41. });
  42. self.wait(300);
  43. }
  44. }));
  45. suite.add(new Y.Test.Case({
  46. name : "Callback object",
  47. "success handler in callback object should execute": function () {
  48. var self = this;
  49. Y.jsonp("server/service.php?callback={callback}", {
  50. on: {
  51. success: function (json) {
  52. self.resume(function () {
  53. Y.Assert.isObject(json);
  54. });
  55. }
  56. }
  57. });
  58. self.wait(300);
  59. },
  60. "inline callback should be replaced if success function provided in config": function () {
  61. var self = this;
  62. Y.config.win.globalFunction = function (json) {
  63. self.resume(function () {
  64. Y.config.win.globalFunction = undefined;
  65. Y.Assert.fail("inline function should not be used");
  66. });
  67. };
  68. Y.jsonp("server/service.php?callback=globalFunction", {
  69. on: {
  70. success: function (data) {
  71. self.resume(function () {
  72. Y.config.win.globalFunction = undefined;
  73. Y.Assert.isObject(data);
  74. });
  75. }
  76. }
  77. });
  78. self.wait(300);
  79. }
  80. }));
  81. suite.add(new Y.Test.Case({
  82. name : "Complex callback path in URL (jsonp-url)",
  83. "complex nested callback in URL should be executed": function () {
  84. var self = this;
  85. Y.config.win.deeply = [
  86. null,
  87. null,
  88. {
  89. nested: {
  90. global: {
  91. func: {
  92. tion: function (json) {
  93. self.resume(function () {
  94. Y.config.win.deeply = undefined;
  95. Y.Assert.isObject(json);
  96. });
  97. }
  98. }
  99. }
  100. }
  101. }
  102. ];
  103. Y.jsonp('server/service.php?callback=deeply[2].nested["global"].func["tion"]');
  104. self.wait(300);
  105. },
  106. "callback relative to Y should be executed": function () {
  107. var self = this;
  108. Y.callbackFunction = function (json) {
  109. self.resume(function () {
  110. delete Y.callbackFunction;
  111. Y.Assert.isObject(json);
  112. });
  113. };
  114. Y.jsonp("server/service.php?callback=callbackFunction");
  115. self.wait(300);
  116. },
  117. "nested inline callback relative to Y should be executed": function () {
  118. var self = this;
  119. Y.deeply = [
  120. null,
  121. null,
  122. {
  123. nested: {
  124. global: {
  125. func: {
  126. tion: function (json) {
  127. self.resume(function () {
  128. delete Y.deeply;
  129. Y.Assert.isObject(json);
  130. });
  131. }
  132. }
  133. }
  134. }
  135. }
  136. ];
  137. Y.jsonp('server/service.php?callback=deeply[2].nested["global"].func["tion"]');
  138. self.wait(300);
  139. },
  140. "inline callback including 'Y.' should be executed": function () {
  141. var self = this;
  142. Y.callbackFunction = function (json) {
  143. self.resume(function () {
  144. delete Y.callbackFunction;
  145. Y.Assert.isObject(json);
  146. });
  147. };
  148. Y.jsonp("server/service.php?callback=Y.callbackFunction");
  149. self.wait(300);
  150. },
  151. "inline callback should be replaced if function passed": function () {
  152. var self = this;
  153. Y.deeply = [
  154. null,
  155. null,
  156. {
  157. nested: {
  158. global: {
  159. func: {
  160. tion: function (json) {
  161. self.resume(function () {
  162. delete Y.deeply;
  163. Y.Assert.fail("inline function should not be used");
  164. });
  165. }
  166. }
  167. }
  168. }
  169. }
  170. ];
  171. Y.jsonp('server/service.php?callback=deeply[2].nested["global"].func["tion"]', function (data) {
  172. self.resume(function () {
  173. delete Y.deeply;
  174. Y.Assert.isObject(data);
  175. });
  176. });
  177. self.wait(300);
  178. },
  179. "inline callback should be replaced if success function provided in config": function () {
  180. var self = this;
  181. Y.deeply = [
  182. null,
  183. null,
  184. {
  185. nested: {
  186. global: {
  187. func: {
  188. tion: function (json) {
  189. self.resume(function () {
  190. delete Y.deeply;
  191. Y.Assert.fail("inline function should not be used");
  192. });
  193. }
  194. }
  195. }
  196. }
  197. }
  198. ];
  199. Y.jsonp('server/service.php?callback=deeply[2].nested["global"].func["tion"]', {
  200. on: {
  201. success: function (data) {
  202. self.resume(function () {
  203. delete Y.deeply;
  204. Y.Assert.isObject(data);
  205. });
  206. }
  207. }
  208. });
  209. self.wait(300);
  210. },
  211. "allowCache should preserve the same callback": function () {
  212. var test = this,
  213. remaining = 2,
  214. callback,
  215. jsonp = new Y.JSONPRequest('server/service.php?callback={callback}', {
  216. allowCache: true,
  217. on: {
  218. success: function (data) {
  219. if (callback) {
  220. if (callback !== data.callback) {
  221. test.resume(function () {
  222. Y.Assert.areSame(callback, data.callback, "callback proxy name should be reused");
  223. });
  224. } else if (--remaining) {
  225. jsonp.send();
  226. } else {
  227. test.resume(function () {
  228. // Pass
  229. });
  230. }
  231. } else {
  232. callback = data.callback;
  233. jsonp.send();
  234. }
  235. }
  236. }
  237. });
  238. jsonp.send();
  239. this.wait();
  240. },
  241. "allowCache should not clear proxy if another send() is pending response":
  242. function () {
  243. var test = this,
  244. callbacks = [],
  245. jsonp = new Y.JSONPRequest('server/service.php?callback={callback}', {
  246. allowCache: true,
  247. on: {
  248. success: function (data) {
  249. callbacks.push(data.callback);
  250. if (callbacks.length > 2) {
  251. test.resume(function () {
  252. Y.Assert.areSame(callbacks[0], callbacks[1]);
  253. Y.Assert.areSame(callbacks[1], callbacks[2]);
  254. Y.Assert.isUndefined(YUI.Env.JSONP[callbacks[0]]);
  255. });
  256. } else if (!YUI.Env.JSONP[data.callback.split(/\./).pop()]) {
  257. test.resume(function () {
  258. Y.Assert.fail("proxy cleared prematurely");
  259. });
  260. }
  261. }
  262. }
  263. });
  264. jsonp.send();
  265. jsonp.send();
  266. jsonp.send();
  267. this.wait();
  268. }
  269. }));
  270. Y.Test.Runner.add(suite);
  271. }, '@VERSION@' ,{requires:['test', 'jsonp-url']});