ajax.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. responseText: {
  31. 'detail': 'ok'
  32. }
  33. });
  34. ajax.request('GET', '/working-url/').then(function(data) {
  35. assert.equal(data.detail, 'ok', "ajax succeeded on /working-url/");
  36. done();
  37. });
  38. });
  39. it("rejects request to backend", function(done) {
  40. $.mockjax({
  41. url: '/failing-url/',
  42. status: 400,
  43. responseText: {
  44. 'detail': 'fail'
  45. }
  46. });
  47. ajax.request('GET', '/failing-url/').then(function() {
  48. assert.fail("request to /failing-url/ should be rejected");
  49. }, function(rejection) {
  50. assert.equal(rejection.detail, 'fail',
  51. "ajax handled error from /failing-url/");
  52. done();
  53. });
  54. });
  55. it("makes GET request", function(done) {
  56. $.mockjax({
  57. url: '/test-url/',
  58. status: 200,
  59. responseText: {
  60. 'detail': 'ok'
  61. }
  62. });
  63. ajax.get('/test-url/').then(function(data) {
  64. assert.equal(data.detail, 'ok', "GET succeeded");
  65. done();
  66. });
  67. });
  68. it("makes PATCH request", function(done) {
  69. $.mockjax({
  70. type: 'PATCH',
  71. url: '/test-url/',
  72. status: 200,
  73. responseText: {
  74. 'detail': 'patched'
  75. }
  76. });
  77. ajax.patch('/test-url/').then(function(data) {
  78. assert.equal(data.detail, 'patched', "PATCH succeeded");
  79. done();
  80. });
  81. });
  82. it("makes PUT request", function(done) {
  83. $.mockjax({
  84. type: 'PUT',
  85. url: '/test-url/',
  86. status: 200,
  87. responseText: {
  88. 'detail': 'put'
  89. }
  90. });
  91. ajax.put('/test-url/').then(function(data) {
  92. assert.equal(data.detail, 'put', "PUT succeeded");
  93. done();
  94. });
  95. });
  96. it("makes DELETE request", function(done) {
  97. $.mockjax({
  98. type: 'DELETE',
  99. url: '/test-url/',
  100. status: 200,
  101. responseText: {
  102. 'detail': 'deleted'
  103. }
  104. });
  105. ajax.delete('/test-url/').then(function(data) {
  106. assert.equal(data.detail, 'deleted', "DELETE succeeded");
  107. done();
  108. });
  109. });
  110. });