Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,22 @@ describe('SamplingModeSwitch', () => {
MockApiClient.clearMockResponses();
});

it('renders correctly in organization mode', () => {
it('cannot enter advanced mode from organization mode', async () => {
render(<SamplingModeSwitch />, {
organization,
});

expect(screen.getByRole('checkbox')).toBeEnabled();
expect(screen.getByText('Advanced Mode')).toBeInTheDocument();
expect(screen.getByRole('checkbox')).not.toBeChecked();
expect(screen.getByRole('checkbox')).toBeDisabled();

await userEvent.hover(screen.getByRole('checkbox'));
expect(
await screen.findByText(
'Advanced Mode is no longer available. Sample rates are configured for the whole organization.'
)
).toBeInTheDocument();
expect(openSamplingModeSwitchModal).not.toHaveBeenCalled();
});

it('renders correctly in project mode', () => {
Expand All @@ -33,31 +41,37 @@ describe('SamplingModeSwitch', () => {
});

expect(screen.getByRole('checkbox')).toBeChecked();
expect(screen.getByRole('checkbox')).toBeEnabled();
});

it('opens modal when switch is clicked', async () => {
it('opens the modal to leave advanced mode when the switch is clicked', async () => {
render(<SamplingModeSwitch initialTargetRate={0.3} />, {
organization,
organization: {...organization, samplingMode: 'project'},
});

await userEvent.click(screen.getByRole('checkbox'));

expect(openSamplingModeSwitchModal).toHaveBeenCalledWith({
samplingMode: 'project',
samplingMode: 'organization',
initialTargetRate: 0.3,
});
});

it('disables switch when user lacks permission', () => {
it('disables switch when user lacks permission', async () => {
const orgWithoutAccess = OrganizationFixture({
access: [], // No project:write access
samplingMode: 'organization',
samplingMode: 'project',
});

render(<SamplingModeSwitch />, {
organization: orgWithoutAccess,
});

expect(screen.getByRole('checkbox')).toBeDisabled();

await userEvent.hover(screen.getByRole('checkbox'));
expect(
await screen.findByText('You do not have permission to change this setting.')
).toBeInTheDocument();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,22 @@ interface Props {
export function SamplingModeSwitch({initialTargetRate}: Props) {
const {samplingMode} = useOrganization();
const hasAccess = useHasDynamicSamplingWriteAccess();
// Advanced Mode can no longer be entered. An organization already in it can still leave it.
const isInAdvancedMode = samplingMode === 'project';

const handleSwitchMode = () => {
openSamplingModeSwitchModal({
samplingMode: samplingMode === 'organization' ? 'project' : 'organization',
samplingMode: 'organization',
initialTargetRate,
});
};

const disabledReason = isInAdvancedMode
? t('You do not have permission to change this setting.')
: t(
'Advanced Mode is no longer available. Sample rates are configured for the whole organization.'
);

return (
<Flex as="label" align="center" gap="md" marginBottom="0">
<InfoText
Expand All @@ -42,15 +50,12 @@ export function SamplingModeSwitch({initialTargetRate}: Props) {
>
{t('Advanced Mode')}
</InfoText>
<Tooltip
disabled={hasAccess}
title={t('You do not have permission to change this setting.')}
>
<Tooltip disabled={hasAccess && isInAdvancedMode} title={disabledReason}>
<Switch
size="lg"
onChange={handleSwitchMode}
disabled={!hasAccess}
checked={samplingMode === 'project'}
disabled={!hasAccess || !isInAdvancedMode}
checked={isInAdvancedMode}
/>
</Tooltip>
</Flex>
Expand Down
Loading