api.spec.jsx 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import {Client, Request} from 'sentry/api';
  2. import {PROJECT_MOVED} from 'sentry/constants/apiErrorCodes';
  3. jest.unmock('sentry/api');
  4. describe('api', function () {
  5. let api;
  6. beforeEach(function () {
  7. api = new Client();
  8. });
  9. describe('Client', function () {
  10. describe('cancel()', function () {
  11. it('should abort any open XHR requests', function () {
  12. const abort1 = jest.fn();
  13. const abort2 = jest.fn();
  14. const req1 = new Request(new Promise(() => null), {abort: abort1});
  15. const req2 = new Request(new Promise(() => null), {abort: abort2});
  16. api.activeRequests = {
  17. 1: req1,
  18. 2: req2,
  19. };
  20. api.clear();
  21. expect(req1.aborter.abort).toHaveBeenCalledTimes(1);
  22. expect(req2.aborter.abort).toHaveBeenCalledTimes(1);
  23. });
  24. });
  25. });
  26. it('does not call success callback if 302 was returned because of a project slug change', function () {
  27. const successCb = jest.fn();
  28. api.activeRequests = {id: {alive: true}};
  29. api.wrapCallback(
  30. 'id',
  31. successCb
  32. )({
  33. responseJSON: {
  34. detail: {
  35. code: PROJECT_MOVED,
  36. message: '...',
  37. extra: {
  38. slug: 'new-slug',
  39. },
  40. },
  41. },
  42. });
  43. expect(successCb).not.toHaveBeenCalled();
  44. });
  45. it('handles error callback', function () {
  46. jest.spyOn(api, 'wrapCallback').mockImplementation((_id, func) => func);
  47. const errorCb = jest.fn();
  48. const args = ['test', true, 1];
  49. api.handleRequestError(
  50. {
  51. id: 'test',
  52. path: 'test',
  53. requestOptions: {error: errorCb},
  54. },
  55. ...args
  56. );
  57. expect(errorCb).toHaveBeenCalledWith(...args);
  58. });
  59. it('handles undefined error callback', function () {
  60. expect(() =>
  61. api.handleRequestError(
  62. {
  63. id: 'test',
  64. path: 'test',
  65. requestOptions: {},
  66. },
  67. {},
  68. {}
  69. )
  70. ).not.toThrow();
  71. });
  72. });