replaySearchAlert.spec.tsx 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import {browserHistory} from 'react-router';
  2. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  3. import useDismissAlert from 'sentry/utils/useDismissAlert';
  4. import {useLocation} from 'sentry/utils/useLocation';
  5. import {ReplaySearchAlert} from 'sentry/views/replays/list/replaySearchAlert';
  6. jest.mock('sentry/utils/useLocation');
  7. jest.mock('sentry/utils/useDismissAlert');
  8. jest.mock('react-router');
  9. const mockBrowserHistoryPush = browserHistory.push as jest.MockedFunction<
  10. typeof browserHistory.push
  11. >;
  12. const mockUseDismissAlert = useDismissAlert as jest.MockedFunction<
  13. typeof useDismissAlert
  14. >;
  15. const mockUseLocation = useLocation as jest.MockedFunction<typeof useLocation>;
  16. function getMockContext() {
  17. return TestStubs.routerContext([{}]);
  18. }
  19. describe('ReplaySearchAlert', () => {
  20. beforeEach(() => {
  21. mockUseDismissAlert.mockReturnValue({
  22. dismiss: () => {},
  23. isDismissed: false,
  24. });
  25. mockUseLocation.mockReturnValue({
  26. pathname: '',
  27. query: {},
  28. search: '',
  29. key: '',
  30. state: {},
  31. action: 'PUSH',
  32. hash: '',
  33. });
  34. });
  35. it('should render search alert by w/ Try Now CTA by default', () => {
  36. const {container} = render(<ReplaySearchAlert needSdkUpdates={false} />, {
  37. context: getMockContext(),
  38. });
  39. expect(container).not.toBeEmptyDOMElement();
  40. expect(container).toHaveTextContent('Try Now');
  41. });
  42. it('should render Learn More CTA if SDK requires update', () => {
  43. const {container} = render(<ReplaySearchAlert needSdkUpdates />, {
  44. context: getMockContext(),
  45. });
  46. expect(container).toHaveTextContent('Learn More');
  47. });
  48. it('should push location.query and dismiss when clicking Try Now CTA', async () => {
  49. const dismiss = jest.fn();
  50. mockUseDismissAlert.mockReturnValue({
  51. dismiss,
  52. isDismissed: false,
  53. });
  54. const {container} = render(<ReplaySearchAlert needSdkUpdates={false} />, {
  55. context: getMockContext(),
  56. });
  57. expect(container).toHaveTextContent('Try Now');
  58. const tryNowButton = await screen.findByText('Try Now');
  59. await userEvent.click(tryNowButton);
  60. expect(dismiss).toHaveBeenCalled();
  61. expect(mockBrowserHistoryPush).toHaveBeenCalledWith(
  62. expect.objectContaining({
  63. query: {
  64. query: 'click.tag:button',
  65. },
  66. })
  67. );
  68. });
  69. it('should render nothing if dismissed', () => {
  70. mockUseDismissAlert.mockReturnValue({
  71. dismiss: () => {},
  72. isDismissed: true,
  73. });
  74. const {container} = render(<ReplaySearchAlert needSdkUpdates={false} />, {
  75. context: getMockContext(),
  76. });
  77. expect(container).toBeEmptyDOMElement();
  78. });
  79. });