ElggAjaxTest.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. define(function(require) {
  2. var elgg = require('elgg');
  3. describe("elgg.ajax", function() {
  4. var wwwroot, ajax;
  5. beforeEach(function() {
  6. wwwroot = elgg.config.wwwroot;
  7. ajax = $.ajax;
  8. elgg.config.wwwroot = 'http://www.elgg.org/';
  9. $.ajax = function(options) {
  10. return options;
  11. };
  12. });
  13. afterEach(function() {
  14. $.ajax = ajax;
  15. elgg.config.wwwroot = wwwroot;
  16. });
  17. it("requests elgg.config.wwwroot by default", function() {
  18. expect(elgg.ajax().url).toBe(elgg.config.wwwroot);
  19. });
  20. it("can issue a GET using elgg.get()", function() {
  21. expect(elgg.get().type).toBe('get');
  22. });
  23. it("can issue a POST using elgg.post()", function() {
  24. expect(elgg.post().type).toBe('post');
  25. });
  26. it("can request JSON with elgg.getJSON()", function() {
  27. expect(elgg.getJSON().dataType).toBe('json');
  28. });
  29. describe("handleOptions", function() {
  30. it("accepts handleOptions()", function() {
  31. expect(elgg.ajax.handleOptions()).not.toBe(undefined);
  32. });
  33. it("accepts handleOptions(url)", function() {
  34. var url = 'http://google.com',
  35. result = elgg.ajax.handleOptions(url);
  36. expect(result.url).toBe(url);
  37. });
  38. it("interprets a POJO as data", function() {
  39. var options = {},
  40. result = elgg.ajax.handleOptions(options);
  41. expect(result.data).toBe(options);
  42. });
  43. it("interprets a POJO with a data field as full options", function() {
  44. var options = {data:{arg:1}},
  45. result = elgg.ajax.handleOptions(options);
  46. expect(result).toBe(options);
  47. });
  48. it("interprets a POJO with a function as full options", function() {
  49. function func() {}
  50. var options = {success: func};
  51. var result = elgg.ajax.handleOptions(options);
  52. expect(result).toBe(options);
  53. });
  54. it("accepts handleOptions(url, data)", function() {
  55. var url = 'url',
  56. data = {arg:1},
  57. result = elgg.ajax.handleOptions(url, data);
  58. expect(result.url).toBe(url);
  59. expect(result.data).toBe(data);
  60. });
  61. it("accepts handleOptions(url, successCallback)", function() {
  62. var url = 'http://google.com',
  63. result = elgg.ajax.handleOptions(url, elgg.nullFunction);
  64. expect(result.url).toBe(url);
  65. expect(result.success).toBe(elgg.nullFunction);
  66. });
  67. it("accepts handleOptions(url, options)", function() {
  68. var url = 'url',
  69. options = {data:{arg:1}},
  70. result = elgg.ajax.handleOptions(url, options);
  71. expect(result.url).toBe(url);
  72. expect(result.data).toBe(options.data);
  73. });
  74. });
  75. describe("elgg.action()", function() {
  76. it("issues a POST request", function() {
  77. var result = elgg.action('action');
  78. expect(result.type).toBe('post');
  79. });
  80. it("expects a JSON response", function() {
  81. var result = elgg.action('action');
  82. expect(result.dataType).toBe('json');
  83. });
  84. it("accepts action names", function() {
  85. var result = elgg.action('action');
  86. expect(result.url).toBe(elgg.config.wwwroot + 'action/action');
  87. });
  88. it("accepts action URLs", function() {
  89. var result = elgg.action(elgg.config.wwwroot + 'action/action');
  90. expect(result.url).toBe(elgg.config.wwwroot + 'action/action');
  91. });
  92. it("includes CSRF tokens automatically in the request", function() {
  93. var result = elgg.action('action');
  94. expect(result.data.__elgg_ts).toBe(elgg.security.token.__elgg_ts);
  95. });
  96. it("throws an exception if you don't specify an action", function() {
  97. expect(function() { elgg.action(); }).toThrow();
  98. expect(function() { elgg.action({}); }).toThrow();
  99. });
  100. });
  101. });
  102. });