api.spec.tsx 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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), {
  15. abort: abort1,
  16. } as any);
  17. const req2 = new Request(new Promise(() => null), {abort: abort2} as any);
  18. api.activeRequests = {
  19. 1: req1,
  20. 2: req2,
  21. };
  22. api.clear();
  23. expect(req1.aborter?.abort).toHaveBeenCalledTimes(1);
  24. expect(req2.aborter?.abort).toHaveBeenCalledTimes(1);
  25. });
  26. });
  27. });
  28. it('does not call success callback if 302 was returned because of a project slug change', function () {
  29. const successCb = jest.fn();
  30. api.activeRequests = {id: {alive: true}};
  31. api.wrapCallback(
  32. 'id',
  33. successCb
  34. )({
  35. responseJSON: {
  36. detail: {
  37. code: PROJECT_MOVED,
  38. message: '...',
  39. extra: {
  40. slug: 'new-slug',
  41. },
  42. },
  43. },
  44. });
  45. expect(successCb).not.toHaveBeenCalled();
  46. });
  47. it('handles error callback', function () {
  48. jest.spyOn(api, 'wrapCallback').mockImplementation((_id, func) => func);
  49. const errorCb = jest.fn();
  50. const args = ['test', true, 1];
  51. api.handleRequestError(
  52. {
  53. id: 'test',
  54. path: 'test',
  55. requestOptions: {error: errorCb},
  56. },
  57. ...args
  58. );
  59. expect(errorCb).toHaveBeenCalledWith(...args);
  60. });
  61. it('handles undefined error callback', function () {
  62. expect(() =>
  63. api.handleRequestError(
  64. {
  65. id: 'test',
  66. path: 'test',
  67. requestOptions: {},
  68. },
  69. {},
  70. {}
  71. )
  72. ).not.toThrow();
  73. });
  74. });