123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- 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(<OrganizationSampleRateInput {...defaultProps} />, {
- 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(<OrganizationSampleRateInput {...defaultProps} showPreviousValue />, {
- organization,
- });
- expect(screen.getByText('previous: 20%')).toBeInTheDocument();
- });
- it('shows "All spans are stored" message when value is 100', () => {
- render(<OrganizationSampleRateInput {...defaultProps} value="100" />, {
- organization,
- });
- expect(screen.getByText('All spans are stored')).toBeInTheDocument();
- });
- it('shows error message when error prop is provided', () => {
- render(<OrganizationSampleRateInput {...defaultProps} error="Invalid value" />, {
- 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(<OrganizationSampleRateInput {...defaultProps} />, {
- organization: orgWithoutAccess,
- });
- expect(screen.getByRole('spinbutton')).toBeDisabled();
- });
- it('enables input when user has access', () => {
- render(<OrganizationSampleRateInput {...defaultProps} />, {
- organization,
- });
- expect(screen.getByRole('spinbutton')).toBeEnabled();
- });
- });
- describe('Bulk Edit Mode', () => {
- it('shows bulk edit button when enabled and user has access', () => {
- const {rerender} = render(
- <OrganizationSampleRateInput
- {...defaultProps}
- isBulkEditEnabled
- isBulkEditActive={false}
- onBulkEditChange={jest.fn()}
- />,
- {organization}
- );
- expect(
- screen.getByRole('button', {name: 'Proportionally scale project rates'})
- ).toBeInTheDocument();
- // In active state, the button should not be shown
- rerender(
- <OrganizationSampleRateInput
- {...defaultProps}
- isBulkEditEnabled
- isBulkEditActive
- onBulkEditChange={jest.fn()}
- />
- );
- 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(
- <OrganizationSampleRateInput
- {...defaultProps}
- isBulkEditEnabled
- isBulkEditActive={false}
- onBulkEditChange={jest.fn()}
- />,
- {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(
- <OrganizationSampleRateInput
- {...defaultProps}
- isBulkEditEnabled
- isBulkEditActive={false}
- onBulkEditChange={onBulkEditChange}
- />,
- {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(
- <OrganizationSampleRateInput
- {...defaultProps}
- isBulkEditEnabled
- isBulkEditActive
- onBulkEditChange={onBulkEditChange}
- />
- );
- expect(input).toHaveFocus();
- });
- });
- });
|