ajax.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import assert from 'assert';
  2. import { Ajax } from 'misago/services/ajax';
  3. var ajax = null;
  4. describe('Ajax', function() {
  5. beforeEach(function() {
  6. document.cookie = '';
  7. ajax = new Ajax();
  8. ajax.init('csrf_token');
  9. });
  10. afterEach(function() {
  11. document.cookie = '';
  12. $.mockjax.clear();
  13. });
  14. it("handles csrf token", function() {
  15. ajax = new Ajax();
  16. ajax.init('csrf_token');
  17. assert.strictEqual(ajax.getCsrfToken(), null,
  18. "function returned null for unset cookie");
  19. document.cookie = 'csrf_token=t3stt0k3n';
  20. assert.equal(ajax.getCsrfToken(), 't3stt0k3n', "set cookie was returned");
  21. ajax = new Ajax();
  22. ajax.init('csrf_token');
  23. assert.equal(ajax._csrfToken, 't3stt0k3n',
  24. "csrf token was read and set on init");
  25. });
  26. it("resolves request to backend", function(done) {
  27. $.mockjax({
  28. url: '/working-url/',
  29. status: 200,
  30. dataType: 'json',
  31. responseText: {
  32. 'detail': 'ok'
  33. }
  34. });
  35. ajax.request('GET', '/working-url/').then(function(data) {
  36. assert.equal(data.detail, 'ok', "ajax succeeded on /working-url/");
  37. done();
  38. });
  39. });
  40. it("rejects request to backend", function(done) {
  41. $.mockjax({
  42. url: '/failing-url/',
  43. status: 400,
  44. responseText: {
  45. 'detail': 'fail'
  46. }
  47. });
  48. ajax.request('GET', '/failing-url/').then(function() {
  49. assert.fail("request to /failing-url/ should be rejected");
  50. }, function(rejection) {
  51. assert.equal(rejection.detail, 'fail',
  52. "ajax handled error from /failing-url/");
  53. done();
  54. });
  55. });
  56. it("normalizes disconnection error", function(done) {
  57. $.mockjax({
  58. url: '/failing-url/',
  59. isTimeout: true
  60. });
  61. ajax.request('GET', '/failing-url/').then(function() {
  62. assert.fail("request to /failing-url/ should be rejected");
  63. }, function(rejection) {
  64. assert.equal(rejection.status, 0, "rejection status code is preserved");
  65. assert.equal(rejection.detail, "Lost connection with application.",
  66. "ajax handled disconnection from /failing-url/");
  67. done();
  68. });
  69. });
  70. it("makes GET request", function(done) {
  71. $.mockjax({
  72. url: '/test-url/',
  73. status: 200,
  74. responseText: {
  75. 'detail': 'ok'
  76. }
  77. });
  78. ajax.get('/test-url/').then(function(data) {
  79. assert.equal(data.detail, 'ok', "GET succeeded");
  80. done();
  81. });
  82. });
  83. it("makes GET request with querystring", function(done) {
  84. $.mockjax({
  85. url: '/test-url/?user=123&text=lorem',
  86. status: 200,
  87. responseText: {
  88. 'detail': 'ok'
  89. }
  90. });
  91. ajax.get('/test-url/', {user: 123, text: 'lorem'}).then(function(data) {
  92. assert.equal(data.detail, 'ok', "GET with querystring succeeded");
  93. done();
  94. });
  95. });
  96. it("makes PATCH request", function(done) {
  97. $.mockjax({
  98. type: 'PATCH',
  99. url: '/test-url/',
  100. status: 200,
  101. responseText: {
  102. 'detail': 'patched'
  103. }
  104. });
  105. ajax.patch('/test-url/').then(function(data) {
  106. assert.equal(data.detail, 'patched', "PATCH succeeded");
  107. done();
  108. });
  109. });
  110. it("makes PUT request", function(done) {
  111. $.mockjax({
  112. type: 'PUT',
  113. url: '/test-url/',
  114. status: 200,
  115. responseText: {
  116. 'detail': 'put'
  117. }
  118. });
  119. ajax.put('/test-url/').then(function(data) {
  120. assert.equal(data.detail, 'put', "PUT succeeded");
  121. done();
  122. });
  123. });
  124. it("makes DELETE request", function(done) {
  125. $.mockjax({
  126. type: 'DELETE',
  127. url: '/test-url/',
  128. status: 200,
  129. responseText: {
  130. 'detail': 'deleted'
  131. }
  132. });
  133. ajax.delete('/test-url/').then(function(data) {
  134. assert.equal(data.detail, 'deleted', "DELETE succeeded");
  135. done();
  136. });
  137. });
  138. });