indicators.spec.jsx 6.0 KB

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