withSentryRouter.spec.tsx 2.1 KB

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