withSentryRouter.spec.tsx 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import type {WithRouterProps} from 'react-router';
  2. import {OrganizationFixture} from 'sentry-fixture/organization';
  3. import {initializeOrg} from 'sentry-test/initializeOrg';
  4. import {render, screen} from 'sentry-test/reactTestingLibrary';
  5. // eslint-disable-next-line no-restricted-imports
  6. import withSentryRouter from 'sentry/utils/withSentryRouter';
  7. const mockUsingCustomerDomain = jest.fn();
  8. const mockCustomerDomain = jest.fn();
  9. jest.mock('sentry/constants', () => {
  10. const sentryConstant = jest.requireActual('sentry/constants');
  11. return {
  12. ...sentryConstant,
  13. get usingCustomerDomain() {
  14. return mockUsingCustomerDomain();
  15. },
  16. get customerDomain() {
  17. return mockCustomerDomain();
  18. },
  19. };
  20. });
  21. describe('withSentryRouter', function () {
  22. type Props = WithRouterProps<{orgId: string}>;
  23. function MyComponent(props: Props) {
  24. const {params} = props;
  25. return <div>Org slug: {params.orgId ?? 'no org slug'}</div>;
  26. }
  27. it('injects orgId when a customer domain is being used', function () {
  28. mockUsingCustomerDomain.mockReturnValue(true);
  29. mockCustomerDomain.mockReturnValue('albertos-apples');
  30. const organization = OrganizationFixture({
  31. slug: 'albertos-apples',
  32. features: [],
  33. });
  34. const {router} = initializeOrg({
  35. organization,
  36. });
  37. const WrappedComponent = withSentryRouter(MyComponent);
  38. render(<WrappedComponent />, {
  39. router,
  40. });
  41. expect(screen.getByText('Org slug: albertos-apples')).toBeInTheDocument();
  42. });
  43. it('does not inject orgId when a customer domain is not being used', function () {
  44. mockUsingCustomerDomain.mockReturnValue(false);
  45. mockCustomerDomain.mockReturnValue(undefined);
  46. const organization = OrganizationFixture({
  47. slug: 'albertos-apples',
  48. features: [],
  49. });
  50. const params = {
  51. orgId: 'something-else',
  52. };
  53. const {router} = initializeOrg({
  54. organization,
  55. router: {
  56. params,
  57. },
  58. });
  59. const WrappedComponent = withSentryRouter(MyComponent);
  60. render(<WrappedComponent />, {
  61. router,
  62. });
  63. expect(screen.getByText('Org slug: something-else')).toBeInTheDocument();
  64. });
  65. });