samplingModeSwitch.spec.tsx 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import {OrganizationFixture} from 'sentry-fixture/organization';
  2. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  3. import {SamplingModeSwitch} from 'sentry/views/settings/dynamicSampling/samplingModeSwitch';
  4. import {openSamplingModeSwitchModal} from 'sentry/views/settings/dynamicSampling/samplingModeSwitchModal';
  5. jest.mock('sentry/views/settings/dynamicSampling/samplingModeSwitchModal');
  6. describe('SamplingModeSwitch', function () {
  7. const organization = OrganizationFixture({
  8. access: ['org:write'],
  9. samplingMode: 'organization',
  10. });
  11. beforeEach(() => {
  12. MockApiClient.clearMockResponses();
  13. });
  14. it('renders correctly in organization mode', function () {
  15. render(<SamplingModeSwitch />, {
  16. organization,
  17. });
  18. expect(screen.getByRole('checkbox')).toBeEnabled();
  19. expect(screen.getByText('Advanced Mode')).toBeInTheDocument();
  20. expect(screen.getByRole('checkbox')).not.toBeChecked();
  21. });
  22. it('renders correctly in project mode', function () {
  23. render(<SamplingModeSwitch />, {
  24. organization: {...organization, samplingMode: 'project'},
  25. });
  26. expect(screen.getByRole('checkbox')).toBeChecked();
  27. });
  28. it('opens modal when switch is clicked', async function () {
  29. render(<SamplingModeSwitch initialTargetRate={0.3} />, {
  30. organization,
  31. });
  32. await userEvent.click(screen.getByRole('checkbox'));
  33. expect(openSamplingModeSwitchModal).toHaveBeenCalledWith({
  34. samplingMode: 'project',
  35. initialTargetRate: 0.3,
  36. });
  37. });
  38. it('disables switch when user lacks permission', function () {
  39. const orgWithoutAccess = OrganizationFixture({
  40. access: [], // No project:write access
  41. samplingMode: 'organization',
  42. });
  43. render(<SamplingModeSwitch />, {
  44. organization: orgWithoutAccess,
  45. });
  46. expect(screen.getByRole('checkbox')).toBeDisabled();
  47. });
  48. });