replaySearchAlert.spec.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. desyncedFilters: new Set(),
  55. pinnedFilters: new Set(),
  56. });
  57. mockUseLocation.mockReturnValue(mockLocationReturn());
  58. mockUseProjectSdkUpdates.mockReturnValue({
  59. type: 'initial',
  60. });
  61. });
  62. it('should not render search alert by default', () => {
  63. const {container} = render(<ReplaySearchAlert />, {
  64. context: getMockContext(),
  65. });
  66. expect(container).toBeEmptyDOMElement();
  67. });
  68. it('should render dismissible alert if minSdk <= 7.44.0', () => {
  69. mockUseProjectSdkUpdates.mockReturnValue({
  70. type: 'resolved',
  71. // @ts-expect-error - ts doesn't play nice with overloaded returns
  72. data: [
  73. {
  74. projectId: project.id,
  75. sdkName: 'javascript',
  76. sdkVersion: '7.0.0',
  77. suggestions: [],
  78. },
  79. ],
  80. });
  81. const {container} = render(<ReplaySearchAlert />, {
  82. context: getMockContext(),
  83. });
  84. expect(container).not.toBeEmptyDOMElement();
  85. expect(screen.queryByTestId('min-sdk-alert')).toBeInTheDocument();
  86. expect(container).toHaveTextContent(
  87. "Search for dom elements clicked during a replay by using our new search key 'click'. Sadly, it requires an SDK version >= 7.44.0"
  88. );
  89. });
  90. it('should render update alert if minSdk <= 7.44.0 and search contains "click" key', () => {
  91. mockUseLocation.mockReturnValue(mockLocationReturn('click.alt:foo'));
  92. mockUseProjectSdkUpdates.mockReturnValue({
  93. type: 'resolved',
  94. // @ts-expect-error - ts doesn't play nice with overloaded returns
  95. data: [
  96. {
  97. projectId: project.id,
  98. sdkName: 'javascript',
  99. sdkVersion: '7.0.0',
  100. suggestions: [],
  101. },
  102. ],
  103. });
  104. const {container} = render(<ReplaySearchAlert />, {
  105. context: getMockContext(),
  106. });
  107. expect(container).not.toBeEmptyDOMElement();
  108. expect(screen.queryByTestId('min-sdk-alert')).toBeInTheDocument();
  109. expect(container).toHaveTextContent(
  110. "Search field 'click' requires a minimum SDK version of >= 7.44.0."
  111. );
  112. });
  113. });