import {OrganizationFixture} from 'sentry-fixture/organization'; import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary'; import {OrganizationSampleRateInput} from './organizationSampleRateInput'; describe('OrganizationSampleRateInput', () => { const organization = OrganizationFixture({ access: ['org:write'], }); const defaultProps = { value: '10', onChange: jest.fn(), label: 'Sample Rate', help: 'Help text', previousValue: '20', showPreviousValue: false, }; beforeEach(() => { jest.clearAllMocks(); }); it('renders with basic props', () => { render(, { organization, }); expect(screen.getByRole('spinbutton')).toHaveValue(10); expect(screen.getByText('Sample Rate')).toBeInTheDocument(); expect(screen.getByText('Help text')).toBeInTheDocument(); }); it('shows previous value when showPreviousValue is true', () => { render(, { organization, }); expect(screen.getByText('previous: 20%')).toBeInTheDocument(); }); it('shows "All spans are stored" message when value is 100', () => { render(, { organization, }); expect(screen.getByText('All spans are stored')).toBeInTheDocument(); }); it('shows error message when error prop is provided', () => { render(, { organization, }); expect(screen.getByText('Invalid value')).toBeInTheDocument(); }); describe('Access Control', () => { it('disables input when user does not have access', () => { const orgWithoutAccess = OrganizationFixture({ access: [], // No org:write access }); render(, { organization: orgWithoutAccess, }); expect(screen.getByRole('spinbutton')).toBeDisabled(); }); it('enables input when user has access', () => { render(, { organization, }); expect(screen.getByRole('spinbutton')).toBeEnabled(); }); }); describe('Bulk Edit Mode', () => { it('shows bulk edit button when enabled and user has access', () => { const {rerender} = render( , {organization} ); expect( screen.getByRole('button', {name: 'Proportionally scale project rates'}) ).toBeInTheDocument(); // In active state, the button should not be shown rerender( ); expect( screen.queryByRole('button', {name: 'Proportionally scale project rates'}) ).not.toBeInTheDocument(); }); it('hides bulk edit button when user does not have access', () => { const orgWithoutAccess = OrganizationFixture({ access: [], // No org:write access }); render( , {organization: orgWithoutAccess} ); expect( screen.queryByRole('button', {name: 'Proportionally scale project rates'}) ).not.toBeInTheDocument(); }); it('autofocuses input after bulk edit becomes active', async () => { const onBulkEditChange = jest.fn(); const {rerender} = render( , {organization} ); const input = screen.getByRole('spinbutton'); expect(input).not.toHaveFocus(); await userEvent.click( screen.getByRole('button', {name: 'Proportionally scale project rates'}) ); expect(onBulkEditChange).toHaveBeenCalledWith(true); // Simulate the parent component updating the active state rerender( ); expect(input).toHaveFocus(); }); }); });