replaySearchAlert.spec.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import {render, screen} from 'sentry-test/reactTestingLibrary';
  2. import {PageFilters} from 'sentry/types';
  3. import {useLocation} from 'sentry/utils/useLocation';
  4. import usePageFilters from 'sentry/utils/usePageFilters';
  5. import useProjects from 'sentry/utils/useProjects';
  6. import {useProjectSdkUpdates} from 'sentry/utils/useProjectSdkUpdates';
  7. import {ReplaySearchAlert} from 'sentry/views/replays/list/replaySearchAlert';
  8. jest.mock('sentry/utils/useProjects');
  9. jest.mock('sentry/utils/useLocation');
  10. jest.mock('sentry/utils/usePageFilters');
  11. jest.mock('sentry/utils/useProjectSdkUpdates');
  12. const mockUseProjects = useProjects as jest.MockedFunction<typeof useProjects>;
  13. const mockUsePageFilters = usePageFilters as jest.MockedFunction<typeof usePageFilters>;
  14. const mockUseLocation = useLocation as jest.MockedFunction<typeof useLocation>;
  15. const mockUseProjectSdkUpdates = useProjectSdkUpdates as jest.MockedFunction<
  16. typeof useProjectSdkUpdates
  17. >;
  18. const project = TestStubs.Project();
  19. function getMockContext() {
  20. return TestStubs.routerContext([{}]);
  21. }
  22. function mockLocationReturn(query: string = ''): ReturnType<typeof useLocation> {
  23. return {
  24. query: {
  25. query,
  26. },
  27. pathname: '',
  28. search: '',
  29. hash: '',
  30. state: {},
  31. action: 'PUSH',
  32. key: '',
  33. };
  34. }
  35. describe('ReplaySearchAlert', () => {
  36. beforeEach(() => {
  37. mockUseProjects.mockReturnValue({
  38. projects: [project],
  39. fetching: false,
  40. hasMore: false,
  41. onSearch: () => Promise.resolve(),
  42. fetchError: null,
  43. initiallyLoaded: false,
  44. placeholders: [],
  45. });
  46. mockUsePageFilters.mockReturnValue({
  47. selection: {
  48. // for some reason project.id selections are numbers, but elsewhere project.id is string
  49. projects: [Number(project.id)],
  50. datetime: {} as PageFilters['datetime'],
  51. environments: [],
  52. },
  53. isReady: true,
  54. shouldPersist: true,
  55. desyncedFilters: new Set(),
  56. pinnedFilters: new Set(),
  57. });
  58. mockUseLocation.mockReturnValue(mockLocationReturn());
  59. mockUseProjectSdkUpdates.mockReturnValue({
  60. type: 'initial',
  61. });
  62. });
  63. it('should not render search alert by default', () => {
  64. const {container} = render(<ReplaySearchAlert />, {
  65. context: getMockContext(),
  66. });
  67. expect(container).toBeEmptyDOMElement();
  68. });
  69. it('should render dismissible alert if minSdk <= 7.44.0', () => {
  70. mockUseProjectSdkUpdates.mockReturnValue({
  71. type: 'resolved',
  72. // @ts-expect-error - ts doesn't play nice with overloaded returns
  73. data: [
  74. {
  75. projectId: project.id,
  76. sdkName: 'javascript',
  77. sdkVersion: '7.0.0',
  78. suggestions: [],
  79. },
  80. ],
  81. });
  82. const {container} = render(<ReplaySearchAlert />, {
  83. context: getMockContext(),
  84. });
  85. expect(container).not.toBeEmptyDOMElement();
  86. expect(screen.queryByTestId('min-sdk-alert')).toBeInTheDocument();
  87. expect(container).toHaveTextContent(
  88. "Search for dom elements clicked during a replay by using our new search key 'click'. Sadly, it requires an SDK version >= 7.44.0"
  89. );
  90. });
  91. it('should render update alert if minSdk <= 7.44.0 and search contains "click" key', () => {
  92. mockUseLocation.mockReturnValue(mockLocationReturn('click.alt:foo'));
  93. mockUseProjectSdkUpdates.mockReturnValue({
  94. type: 'resolved',
  95. // @ts-expect-error - ts doesn't play nice with overloaded returns
  96. data: [
  97. {
  98. projectId: project.id,
  99. sdkName: 'javascript',
  100. sdkVersion: '7.0.0',
  101. suggestions: [],
  102. },
  103. ],
  104. });
  105. const {container} = render(<ReplaySearchAlert />, {
  106. context: getMockContext(),
  107. });
  108. expect(container).not.toBeEmptyDOMElement();
  109. expect(screen.queryByTestId('min-sdk-alert')).toBeInTheDocument();
  110. expect(container).toHaveTextContent(
  111. "Search field 'click' requires a minimum SDK version of >= 7.44.0."
  112. );
  113. });
  114. });