logging.spec.jsx 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import {logAjaxError} from 'app/utils/logging';
  2. import * as Sentry from '@sentry/browser';
  3. describe('logging', function() {
  4. describe('logAjaxError()', function() {
  5. beforeEach(function() {
  6. jest.spyOn(console, 'error').mockImplementation(() => {});
  7. Sentry.captureMessage.mockReset();
  8. });
  9. afterEach(function() {
  10. window.console.error.mockRestore();
  11. });
  12. it('should handle (Sentry) JSON responses', function() {
  13. logAjaxError(
  14. {
  15. status: 500,
  16. responseJSON: {detail: 'A bad thing happened'},
  17. },
  18. {foo: 'bar'} /* context */
  19. );
  20. expect(Sentry.captureMessage).toHaveBeenCalled();
  21. expect(Sentry.captureMessage.mock.calls[0][0]).toEqual(
  22. 'HTTP 500: A bad thing happened'
  23. );
  24. });
  25. it('should handle text/html responses', function() {
  26. logAjaxError(
  27. {
  28. status: 401,
  29. responseText: 'You are not authenticated',
  30. },
  31. {foo: 'bar'} /* context */
  32. );
  33. expect(Sentry.captureMessage).toHaveBeenCalled();
  34. expect(Sentry.captureMessage.mock.calls[0][0]).toEqual(
  35. 'HTTP 401: You are not authenticated'
  36. );
  37. });
  38. it('should handle responseJSON/responseText undefined (bad content type?)', function() {
  39. logAjaxError({status: 404}, {foo: 'bar'} /* context */);
  40. expect(Sentry.captureMessage).toHaveBeenCalled();
  41. expect(Sentry.captureMessage.mock.calls[0][0]).toEqual(
  42. 'HTTP 404: <unknown response>'
  43. );
  44. });
  45. });
  46. });