indicators.spec.jsx 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import {act, render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
  2. import {
  3. addErrorMessage,
  4. addMessage,
  5. addSuccessMessage,
  6. clearIndicators,
  7. } from 'sentry/actionCreators/indicator';
  8. import Indicators from 'sentry/components/indicators';
  9. import IndicatorStore from 'sentry/stores/indicatorStore';
  10. // Make sure we use `duration: null` to test add/remove
  11. jest.useFakeTimers();
  12. jest.mock('framer-motion', () => ({
  13. ...jest.requireActual('framer-motion'),
  14. AnimatePresence: jest.fn(({children}) => children),
  15. }));
  16. describe('Indicators', function () {
  17. beforeEach(function () {
  18. clearIndicators();
  19. act(jest.runAllTimers);
  20. });
  21. it('renders nothing by default', function () {
  22. const {container} = render(<Indicators />);
  23. expect(container).toHaveTextContent('');
  24. });
  25. it('has a loading indicator by default', function () {
  26. const {container} = render(<Indicators />);
  27. // when "type" is empty, we should treat it as loading state
  28. act(() => void IndicatorStore.add('Loading'));
  29. expect(screen.getByTestId('loading-indicator')).toBeInTheDocument();
  30. expect(container).toHaveTextContent('Loading');
  31. });
  32. it('adds and removes a toast by calling IndicatorStore directly', function () {
  33. const {container} = render(<Indicators />);
  34. // when "type" is empty, we should treat it as loading state
  35. let indicator;
  36. act(() => {
  37. indicator = IndicatorStore.add('Loading');
  38. });
  39. expect(container).toHaveTextContent('Loading');
  40. // Old indicator gets replaced when a new one is added
  41. act(() => IndicatorStore.remove(indicator));
  42. expect(container).toHaveTextContent('');
  43. });
  44. // This is a common pattern used throughout the code for API calls
  45. it('adds and replaces toast by calling IndicatorStore directly', function () {
  46. const {container} = render(<Indicators />);
  47. act(() => void IndicatorStore.add('Loading'));
  48. expect(container).toHaveTextContent('Loading');
  49. // Old indicator gets replaced when a new one is added
  50. act(() => void IndicatorStore.add('success', 'success'));
  51. expect(container).toHaveTextContent('success');
  52. });
  53. it('does not have loading indicator when "type" is empty (default)', function () {
  54. const {container} = render(<Indicators />);
  55. addMessage('Loading', '', {duration: null});
  56. act(jest.runAllTimers);
  57. expect(container).toHaveTextContent('Loading');
  58. expect(screen.queryByTestId('loading-indicator')).not.toBeInTheDocument();
  59. });
  60. it('has a loading indicator when type is "loading"', function () {
  61. const {container} = render(<Indicators />);
  62. addMessage('Loading', 'loading', {duration: null});
  63. act(jest.runAllTimers);
  64. expect(container).toHaveTextContent('Loading');
  65. expect(screen.getByTestId('loading-indicator')).toBeInTheDocument();
  66. });
  67. it('adds and removes toast by calling action creators', function () {
  68. const {container} = render(<Indicators />);
  69. // action creators don't return anything
  70. addMessage('Loading', '', {duration: null});
  71. act(jest.runAllTimers);
  72. expect(container).toHaveTextContent('Loading');
  73. // If no indicator is specified, will remove all indicators
  74. clearIndicators();
  75. act(jest.runAllTimers);
  76. expect(container).toHaveTextContent('');
  77. expect(screen.queryByTestId('loading-indicator')).not.toBeInTheDocument();
  78. });
  79. it('adds and replaces toast by calling action creators', function () {
  80. const {container} = render(<Indicators />);
  81. addMessage('Loading', '', {duration: null});
  82. act(jest.runAllTimers);
  83. expect(container).toHaveTextContent('Loading');
  84. // Old indicator gets replaced when a new one is added
  85. addMessage('success', 'success', {duration: null});
  86. act(jest.runAllTimers);
  87. expect(container).toHaveTextContent('success');
  88. expect(screen.queryByTestId('loading-indicator')).not.toBeInTheDocument();
  89. });
  90. it('adds and replaces toasts by calling action creators helpers', async function () {
  91. const {container} = render(<Indicators />);
  92. // Old indicator gets replaced when a new one is added
  93. addSuccessMessage('success');
  94. await waitFor(() => {
  95. expect(container).toHaveTextContent('success');
  96. });
  97. clearIndicators();
  98. addErrorMessage('error');
  99. await waitFor(() => {
  100. expect(container).toHaveTextContent('error');
  101. });
  102. });
  103. it('appends toasts', function () {
  104. const {container} = render(<Indicators />);
  105. addMessage('Loading', '', {append: true, duration: null});
  106. act(jest.runAllTimers);
  107. expect(screen.getByTestId('toast')).toHaveTextContent('Loading');
  108. addMessage('Success', 'success', {append: true, duration: null});
  109. act(jest.runAllTimers);
  110. // Toasts get appended to the end
  111. expect(screen.getByTestId('toast')).toHaveTextContent('Loading');
  112. expect(screen.getByTestId('toast-success')).toHaveTextContent('Success');
  113. addMessage('Error', 'error', {append: true, duration: null});
  114. act(jest.runAllTimers);
  115. // Toasts get appended to the end
  116. expect(screen.getByTestId('toast')).toHaveTextContent('Loading');
  117. expect(screen.getByTestId('toast-success')).toHaveTextContent('Success');
  118. expect(screen.getByTestId('toast-error')).toHaveTextContent('Error');
  119. // clears all toasts
  120. clearIndicators();
  121. act(jest.runAllTimers);
  122. expect(container).toHaveTextContent('');
  123. expect(screen.queryByTestId('loading-indicator')).not.toBeInTheDocument();
  124. });
  125. it('dismisses on click', function () {
  126. const {container} = render(<Indicators />);
  127. addMessage('Loading', '', {append: true, duration: null});
  128. act(jest.runAllTimers);
  129. expect(screen.getByTestId('toast')).toHaveTextContent('Loading');
  130. userEvent.click(screen.getByTestId('toast'));
  131. act(jest.runAllTimers);
  132. expect(container).toHaveTextContent('');
  133. expect(screen.queryByTestId('toast')).not.toBeInTheDocument();
  134. });
  135. it('hides after 10s', function () {
  136. const {container} = render(<Indicators />);
  137. addMessage('Duration', '', {append: true, duration: 10000});
  138. act(() => jest.advanceTimersByTime(9000));
  139. expect(screen.getByTestId('toast')).toHaveTextContent('Duration');
  140. // Still visible
  141. act(() => jest.advanceTimersByTime(999));
  142. expect(screen.getByTestId('toast')).toHaveTextContent('Duration');
  143. act(() => jest.advanceTimersByTime(2));
  144. expect(container).toHaveTextContent('');
  145. expect(screen.queryByTestId('toast')).not.toBeInTheDocument();
  146. });
  147. });