useActiveReplayTab.spec.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import {browserHistory} from 'react-router';
  2. import {Location} from 'history';
  3. import {reactHooks} from 'sentry-test/reactTestingLibrary';
  4. import useActiveReplayTab, {TabKey} from 'sentry/utils/replays/hooks/useActiveReplayTab';
  5. import {useLocation} from 'sentry/utils/useLocation';
  6. import useOrganization from 'sentry/utils/useOrganization';
  7. jest.mock('react-router');
  8. jest.mock('sentry/utils/useLocation');
  9. jest.mock('sentry/utils/useOrganization');
  10. const mockUseLocation = useLocation as jest.MockedFunction<typeof useLocation>;
  11. const mockUseOrganization = useOrganization as jest.MockedFunction<
  12. typeof useOrganization
  13. >;
  14. const mockPush = browserHistory.push as jest.MockedFunction<typeof browserHistory.push>;
  15. function mockLocation(query: string = '') {
  16. mockUseLocation.mockReturnValue({
  17. action: 'PUSH',
  18. hash: '',
  19. key: '',
  20. pathname: '',
  21. query: {query},
  22. search: '',
  23. state: undefined,
  24. } as Location);
  25. }
  26. function mockOrganization(props?: {features: string[]}) {
  27. const features = props?.features ?? [];
  28. mockUseOrganization.mockReturnValue(
  29. TestStubs.Organization({
  30. features,
  31. })
  32. );
  33. }
  34. describe('useActiveReplayTab', () => {
  35. beforeEach(() => {
  36. mockLocation();
  37. mockOrganization();
  38. mockPush.mockReset();
  39. });
  40. it('should use Console as a default', () => {
  41. const {result} = reactHooks.renderHook(useActiveReplayTab);
  42. expect(result.current.getActiveTab()).toBe(TabKey.CONSOLE);
  43. });
  44. it('should use DOM as a default, when there is a click search in the url', () => {
  45. mockLocation('click.tag:button');
  46. const {result} = reactHooks.renderHook(useActiveReplayTab);
  47. expect(result.current.getActiveTab()).toBe(TabKey.DOM);
  48. });
  49. it('should allow case-insensitive tab names', () => {
  50. const {result} = reactHooks.renderHook(useActiveReplayTab);
  51. expect(result.current.getActiveTab()).toBe(TabKey.CONSOLE);
  52. result.current.setActiveTab('nEtWoRk');
  53. expect(mockPush).toHaveBeenLastCalledWith({
  54. pathname: '',
  55. query: {t_main: TabKey.NETWORK},
  56. });
  57. });
  58. it('should set the default tab if the name is invalid', () => {
  59. const {result} = reactHooks.renderHook(useActiveReplayTab);
  60. expect(result.current.getActiveTab()).toBe(TabKey.CONSOLE);
  61. result.current.setActiveTab('foo bar');
  62. expect(mockPush).toHaveBeenLastCalledWith({
  63. pathname: '',
  64. query: {t_main: TabKey.CONSOLE},
  65. });
  66. });
  67. it('should allow ISSUES if session-replay-errors-tab is disabled', () => {
  68. mockOrganization({
  69. features: [],
  70. });
  71. const {result} = reactHooks.renderHook(useActiveReplayTab);
  72. expect(result.current.getActiveTab()).toBe(TabKey.CONSOLE);
  73. // ISSUES is allowed:
  74. result.current.setActiveTab(TabKey.ISSUES);
  75. expect(mockPush).toHaveBeenLastCalledWith({
  76. pathname: '',
  77. query: {t_main: TabKey.ISSUES},
  78. });
  79. // ERRORS is not enabled, reset to default:
  80. result.current.setActiveTab(TabKey.ERRORS);
  81. expect(mockPush).toHaveBeenLastCalledWith({
  82. pathname: '',
  83. query: {t_main: TabKey.CONSOLE},
  84. });
  85. });
  86. it('should allow ERRORS if session-replay-errors-tab is enabled', () => {
  87. mockOrganization({
  88. features: ['session-replay-errors-tab'],
  89. });
  90. const {result} = reactHooks.renderHook(useActiveReplayTab);
  91. expect(result.current.getActiveTab()).toBe(TabKey.CONSOLE);
  92. // ERRORS is enabled:
  93. result.current.setActiveTab(TabKey.ERRORS);
  94. expect(mockPush).toHaveBeenLastCalledWith({
  95. pathname: '',
  96. query: {t_main: TabKey.ERRORS},
  97. });
  98. // ISSUES is not allowed, it's been replaced by ERRORS, reset to default:
  99. result.current.setActiveTab(TabKey.ISSUES);
  100. expect(mockPush).toHaveBeenLastCalledWith({
  101. pathname: '',
  102. query: {t_main: TabKey.CONSOLE},
  103. });
  104. });
  105. });