useActiveReplayTab.spec.tsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import {browserHistory} from 'react-router';
  2. import type {Location} from 'history';
  3. import {OrganizationFixture} from 'sentry-fixture/organization';
  4. import {renderHook} from 'sentry-test/reactTestingLibrary';
  5. import useActiveReplayTab, {TabKey} from 'sentry/utils/replays/hooks/useActiveReplayTab';
  6. import {useLocation} from 'sentry/utils/useLocation';
  7. import useOrganization from 'sentry/utils/useOrganization';
  8. jest.mock('react-router');
  9. jest.mock('sentry/utils/useLocation');
  10. jest.mock('sentry/utils/useOrganization');
  11. const mockPush = jest.mocked(browserHistory.push);
  12. function mockLocation(query: string = '') {
  13. jest.mocked(useLocation).mockReturnValue({
  14. action: 'PUSH',
  15. hash: '',
  16. key: '',
  17. pathname: '',
  18. query: {query},
  19. search: '',
  20. state: undefined,
  21. } as Location);
  22. }
  23. function mockOrganizationFixture(props?: {features: string[]}) {
  24. const features = props?.features ?? [];
  25. jest.mocked(useOrganization).mockReturnValue(OrganizationFixture({features}));
  26. }
  27. describe('useActiveReplayTab', () => {
  28. beforeEach(() => {
  29. mockLocation();
  30. mockOrganizationFixture();
  31. mockPush.mockReset();
  32. });
  33. it('should use Breadcrumbs as a default', () => {
  34. const {result} = renderHook(useActiveReplayTab, {
  35. initialProps: {},
  36. });
  37. expect(result.current.getActiveTab()).toBe(TabKey.BREADCRUMBS);
  38. });
  39. it('should use Tags as a default for video replays', () => {
  40. const {result} = renderHook(useActiveReplayTab, {
  41. initialProps: {isVideoReplay: true},
  42. });
  43. expect(result.current.getActiveTab()).toBe(TabKey.TAGS);
  44. });
  45. it('should use Breadcrumbs as a default, when there is a click search in the url', () => {
  46. mockLocation('click.tag:button');
  47. const {result} = renderHook(useActiveReplayTab, {
  48. initialProps: {},
  49. });
  50. expect(result.current.getActiveTab()).toBe(TabKey.BREADCRUMBS);
  51. });
  52. it('should allow case-insensitive tab names', () => {
  53. const {result} = renderHook(useActiveReplayTab, {
  54. initialProps: {},
  55. });
  56. expect(result.current.getActiveTab()).toBe(TabKey.BREADCRUMBS);
  57. result.current.setActiveTab('nEtWoRk');
  58. expect(mockPush).toHaveBeenLastCalledWith({
  59. pathname: '',
  60. query: {t_main: TabKey.NETWORK},
  61. });
  62. });
  63. it('should set the default tab if the name is invalid', () => {
  64. const {result} = renderHook(useActiveReplayTab, {
  65. initialProps: {},
  66. });
  67. expect(result.current.getActiveTab()).toBe(TabKey.BREADCRUMBS);
  68. result.current.setActiveTab('foo bar');
  69. expect(mockPush).toHaveBeenLastCalledWith({
  70. pathname: '',
  71. query: {t_main: TabKey.BREADCRUMBS},
  72. });
  73. });
  74. it('should set the default tab if the name is invalid for video replays', () => {
  75. const {result} = renderHook(useActiveReplayTab, {
  76. initialProps: {isVideoReplay: true},
  77. });
  78. expect(result.current.getActiveTab()).toBe(TabKey.TAGS);
  79. result.current.setActiveTab('foo bar');
  80. expect(mockPush).toHaveBeenLastCalledWith({
  81. pathname: '',
  82. query: {t_main: TabKey.TAGS},
  83. });
  84. });
  85. });