InspectErrorTab.test.tsx 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import { render, screen } from '@testing-library/react';
  2. import React from 'react';
  3. import { InspectErrorTab } from './InspectErrorTab';
  4. describe('InspectErrorTab', () => {
  5. it('should return null when error does not exist', () => {
  6. const { container } = render(<InspectErrorTab />);
  7. expect(container.childElementCount).toEqual(0);
  8. });
  9. it('should return a jsonFormatter object of error.data if it exists', () => {
  10. const error = {
  11. data: {
  12. message: 'This is an error',
  13. error: 'my error',
  14. },
  15. };
  16. render(<InspectErrorTab error={error} />);
  17. expect(screen.getByText('This is an error')).toBeInTheDocument();
  18. expect(screen.getByText('error:')).toBeInTheDocument();
  19. expect(screen.getByText('"my error"')).toBeInTheDocument();
  20. });
  21. it('should return a jsonFormatter object of error.message if it exists and data does not exist', () => {
  22. const error = {
  23. message:
  24. '{ "error": { "code": "BadRequest", "message": "Please provide below info when asking for support.", "details": [] } }',
  25. };
  26. const { container } = render(<InspectErrorTab error={error} />);
  27. expect(container.childElementCount).toEqual(1);
  28. expect(screen.getByText('code:')).toBeInTheDocument();
  29. expect(screen.getByText('"BadRequest"')).toBeInTheDocument();
  30. expect(screen.getByText('"Please provide below info when asking for support."')).toBeInTheDocument();
  31. });
  32. it('should return an h3 and jsonFormatter object of error.message if it exists and data does not exist', () => {
  33. const error = {
  34. message:
  35. '400 BadRequest, Error from Azure: { "error": { "code": "BadRequest", "message": "Please provide below info when asking for support.", "details": [] } }',
  36. };
  37. const { container } = render(<InspectErrorTab error={error} />);
  38. expect(container.childElementCount).toEqual(2);
  39. expect(screen.getByRole('heading', { name: '400 BadRequest, Error from Azure:' })).toBeInTheDocument();
  40. expect(screen.getByText('code:')).toBeInTheDocument();
  41. expect(screen.getByText('"BadRequest"')).toBeInTheDocument();
  42. });
  43. [
  44. '{ invalidJSON{',
  45. "hello, I am an error that's just text, no json at all, altoough I do mention template variables {{test}}",
  46. 'and I am a simple string',
  47. ].forEach((errMsg) => {
  48. it(`should return error.message error.data does not exist nd error.message cannot be parsed - ${errMsg} `, () => {
  49. const error = {
  50. message: errMsg,
  51. };
  52. render(<InspectErrorTab error={error} />);
  53. expect(screen.queryByRole('heading')).toBeNull();
  54. expect(screen.getByText(errMsg)).toBeInTheDocument();
  55. });
  56. });
  57. it('should return a jsonFormatter object of error if it has no .data and no .message', () => {
  58. const error = {
  59. status: 400,
  60. };
  61. const { container } = render(<InspectErrorTab error={error} />);
  62. expect(container.childElementCount).toEqual(1);
  63. expect(screen.getByText('status:')).toBeInTheDocument();
  64. expect(screen.getByText('400')).toBeInTheDocument();
  65. });
  66. });