useParams.spec.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import {LocationFixture} from 'sentry-fixture/locationFixture';
  2. import {RouterFixture} from 'sentry-fixture/routerFixture';
  3. import {render, screen} from 'sentry-test/reactTestingLibrary';
  4. import type {RouteContextInterface} from 'sentry/types/legacyReactRouter';
  5. import {useParams} from 'sentry/utils/useParams';
  6. import {useRouteContext} from 'sentry/utils/useRouteContext';
  7. import {RouteContext} from 'sentry/views/routeContext';
  8. const mockUsingCustomerDomain = jest.fn();
  9. const mockCustomerDomain = jest.fn();
  10. jest.mock('sentry/constants', () => {
  11. const sentryConstant = jest.requireActual('sentry/constants');
  12. return {
  13. ...sentryConstant,
  14. get USING_CUSTOMER_DOMAIN() {
  15. return mockUsingCustomerDomain();
  16. },
  17. get CUSTOMER_DOMAIN() {
  18. return mockCustomerDomain();
  19. },
  20. };
  21. });
  22. describe('useParams', () => {
  23. describe('when the path has no params', () => {
  24. it('returns an empty object', () => {
  25. let params;
  26. function HomePage() {
  27. params = useParams();
  28. return null;
  29. }
  30. const routeContext: RouteContextInterface = {
  31. location: LocationFixture(),
  32. params: {},
  33. router: RouterFixture(),
  34. routes: [],
  35. };
  36. render(
  37. <RouteContext.Provider value={routeContext}>
  38. <HomePage />
  39. </RouteContext.Provider>
  40. );
  41. expect(params).toEqual({});
  42. });
  43. });
  44. describe('when the path has some params', () => {
  45. it('returns an object of the URL params', () => {
  46. let params;
  47. function HomePage() {
  48. params = useParams();
  49. return null;
  50. }
  51. const routeContext: RouteContextInterface = {
  52. location: LocationFixture(),
  53. params: {slug: 'sentry'},
  54. router: RouterFixture(),
  55. routes: [],
  56. };
  57. render(
  58. <RouteContext.Provider value={routeContext}>
  59. <HomePage />
  60. </RouteContext.Provider>
  61. );
  62. expect(params).toEqual({slug: 'sentry'});
  63. });
  64. });
  65. describe('customer domains', function () {
  66. afterEach(function () {
  67. jest.resetAllMocks();
  68. });
  69. it('populates orgId when customer domain is being used', function () {
  70. mockUsingCustomerDomain.mockReturnValue(true);
  71. mockCustomerDomain.mockReturnValue('albertos-apples');
  72. let originalParams;
  73. let useParamsValue;
  74. function Component() {
  75. const {params} = useRouteContext()!;
  76. originalParams = params;
  77. useParamsValue = useParams();
  78. return (
  79. <div>rendered component for org: {useParamsValue.orgId ?? 'no org id'}</div>
  80. );
  81. }
  82. const routeContext: RouteContextInterface = {
  83. location: LocationFixture(),
  84. params: {},
  85. router: RouterFixture(),
  86. routes: [],
  87. };
  88. render(
  89. <RouteContext.Provider value={routeContext}>
  90. <Component />
  91. </RouteContext.Provider>
  92. );
  93. expect(
  94. screen.getByText('rendered component for org: albertos-apples')
  95. ).toBeInTheDocument();
  96. expect(originalParams).toEqual({});
  97. expect(useParamsValue).toEqual({
  98. orgId: 'albertos-apples',
  99. });
  100. });
  101. it('does not populate orgId when customer domain is not being used', function () {
  102. mockUsingCustomerDomain.mockReturnValue(false);
  103. mockCustomerDomain.mockReturnValue(undefined);
  104. let originalParams;
  105. let useParamsValue;
  106. function Component() {
  107. const {params} = useRouteContext()!;
  108. originalParams = params;
  109. useParamsValue = useParams();
  110. return (
  111. <div>rendered component for org: {useParamsValue.orgId ?? 'no org id'}</div>
  112. );
  113. }
  114. const routeContext: RouteContextInterface = {
  115. location: LocationFixture(),
  116. params: {},
  117. router: RouterFixture(),
  118. routes: [],
  119. };
  120. render(
  121. <RouteContext.Provider value={routeContext}>
  122. <Component />
  123. </RouteContext.Provider>
  124. );
  125. expect(
  126. screen.getByText('rendered component for org: no org id')
  127. ).toBeInTheDocument();
  128. expect(originalParams).toEqual({});
  129. expect(useParamsValue).toEqual({});
  130. });
  131. });
  132. });