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 @@ -18,11 +18,18 @@
*/
import { t } from '@apache-superset/core/translation';
import { useState } from 'react';
import { render, screen, userEvent } from 'spec/helpers/testing-library';
import {
fireEvent,
render,
screen,
userEvent,
} from 'spec/helpers/testing-library';
import {
CUSTOM_REFRESH_FREQUENCY,
getCustomValue,
getRefreshFrequencyOptions,
getRefreshWarningMessage,
isPresetValue,
RefreshFrequencySelect,
REFRESH_FREQUENCY_OPTIONS,
validateRefreshFrequency,
Expand Down Expand Up @@ -299,3 +306,93 @@ test('selecting a preset still snaps selection to that preset', async () => {
expect(screen.getByRole('radio', { name: /Custom/i })).not.toBeChecked();
expect(screen.getByRole('spinbutton')).toBeDisabled();
});

test('isPresetValue and getCustomValue work with default and custom options', () => {
expect(isPresetValue(10)).toBe(true);
expect(isPresetValue(42)).toBe(false);
expect(isPresetValue(CUSTOM_REFRESH_FREQUENCY)).toBe(false);

expect(getCustomValue(10)).toBe('');
expect(getCustomValue(0)).toBe('');
expect(getCustomValue(42)).toBe('42');
});

test('uses options prop override when provided', () => {
const propOptions: [number, string][] = [
[0, "Don't refresh"],
[5, '5 seconds'],
[25, '25 seconds'],
];

render(
<RefreshFrequencySelect
value={0}
onChange={jest.fn()}
options={propOptions}
/>,
{
useRedux: true,
initialState: {
common: { conf: { DASHBOARD_AUTO_REFRESH_INTERVALS: INTERVALS } },
},
},
);

expect(screen.getByRole('radio', { name: '5 seconds' })).toBeInTheDocument();
expect(screen.getByRole('radio', { name: '25 seconds' })).toBeInTheDocument();
expect(
screen.queryByRole('radio', { name: '10 minutes' }),
).not.toBeInTheDocument();
});

test('preserves previously entered custom value when switching between preset and custom', async () => {
render(<ControlledSelect initialValue={0} />, {
useRedux: true,
initialState: {
common: { conf: { DASHBOARD_AUTO_REFRESH_INTERVALS: INTERVALS } },
},
});

const customRadio = screen.getByRole('radio', { name: /Custom/i });
const presetRadio = screen.getByRole('radio', { name: '10 minutes' });
const input = screen.getByRole('spinbutton');

await userEvent.click(customRadio);
fireEvent.change(input, { target: { value: '120' } });
expect(input).toHaveValue(120);

await userEvent.click(presetRadio);
expect(presetRadio).toBeChecked();
expect(input).toBeDisabled();

await userEvent.click(customRadio);
expect(customRadio).toBeChecked();
expect(input).toBeEnabled();
expect(input).toHaveValue(120);
});

test('clamps custom draft below minimum interval when switching preset to custom', async () => {
render(<ControlledSelect initialValue={0} />, {
useRedux: true,
initialState: {
common: { conf: { DASHBOARD_AUTO_REFRESH_INTERVALS: INTERVALS } },
},
});

const customRadio = screen.getByRole('radio', { name: /Custom/i });
const presetRadio = screen.getByRole('radio', { name: '10 minutes' });
const input = screen.getByRole('spinbutton');

await userEvent.click(customRadio);
fireEvent.change(input, { target: { value: '-5' } });
expect(input).toHaveValue(-5);

await userEvent.click(presetRadio);
expect(presetRadio).toBeChecked();
expect(input).toBeDisabled();

await userEvent.click(customRadio);
expect(customRadio).toBeChecked();
expect(input).toBeEnabled();
expect(input).toHaveValue(1);
});
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ export const REFRESH_FREQUENCY_OPTIONS = [
{ value: CUSTOM_REFRESH_FREQUENCY, label: t('Custom') },
];

const isPresetValue = (frequency: number, options: RefreshFrequencyOption[]) =>
export const isPresetValue = (
frequency: number,
options: RefreshFrequencyOption[] = REFRESH_FREQUENCY_OPTIONS,
) =>
options.some(
option =>
option.value === frequency && option.value !== CUSTOM_REFRESH_FREQUENCY,
Expand Down Expand Up @@ -131,9 +134,9 @@ export const getRefreshFrequencyOptions = (
: REFRESH_FREQUENCY_OPTIONS;
};

const getCustomValue = (
export const getCustomValue = (
frequency: number,
options: RefreshFrequencyOption[],
options: RefreshFrequencyOption[] = REFRESH_FREQUENCY_OPTIONS,
) =>
!isPresetValue(frequency, options) && frequency > 0
? frequency.toString()
Expand All @@ -153,9 +156,10 @@ const normalizeRefreshLimitSeconds = (
return refreshLimit;
};

interface RefreshFrequencySelectProps {
export interface RefreshFrequencySelectProps {
value: number;
onChange: (value: number) => void;
options?: [number, string][];
}

/**
Expand All @@ -165,6 +169,7 @@ interface RefreshFrequencySelectProps {
export const RefreshFrequencySelect = ({
value,
onChange,
options: optionsProp,
}: RefreshFrequencySelectProps) => {
const configuredIntervals = useSelector(
(state: RootState) =>
Expand All @@ -175,8 +180,8 @@ export const RefreshFrequencySelect = ({
state.common?.conf?.DASHBOARD_AUTO_REFRESH_INTERVALS,
);
const options = useMemo(
() => getRefreshFrequencyOptions(configuredIntervals),
[configuredIntervals],
() => getRefreshFrequencyOptions(optionsProp ?? configuredIntervals),
[optionsProp, configuredIntervals],
);
const presets = options.filter(
option => option.value !== CUSTOM_REFRESH_FREQUENCY,
Expand Down Expand Up @@ -207,15 +212,15 @@ export const RefreshFrequencySelect = ({
? value
: CUSTOM_REFRESH_FREQUENCY;
setRadioSelection(selection);
setCustomValue(
selection === CUSTOM_REFRESH_FREQUENCY
? // getCustomValue would blank a value that collides with a preset;
// a custom-originated value is the input's own text.
cameFromCustomControls
if (selection === CUSTOM_REFRESH_FREQUENCY) {
setCustomValue(
// getCustomValue would blank a value that collides with a preset;
// a custom-originated value is the input's own text.
cameFromCustomControls
? value.toString()
: getCustomValue(value, options)
: '',
);
: getCustomValue(value, options),
);
}
if (!cameFromCustomControls) {
lastCustomEmittedValue.current = null;
}
Expand All @@ -226,11 +231,15 @@ export const RefreshFrequencySelect = ({
setRadioSelection(selectedValue);

if (selectedValue === CUSTOM_REFRESH_FREQUENCY) {
// Custom selected - use current custom value or minimum
const numValue = parseInt(customValue, 10) || MINIMUM_REFRESH_INTERVAL;
// Custom selected - use current custom value if >= minimum, otherwise fallback to minimum
const parsed = parseInt(customValue, 10);
const numValue =
!Number.isNaN(parsed) && parsed >= MINIMUM_REFRESH_INTERVAL
? parsed
: MINIMUM_REFRESH_INTERVAL;
lastCustomEmittedValue.current = numValue;
onChange(numValue);
if (!customValue) {
if (Number.isNaN(parsed) || parsed < MINIMUM_REFRESH_INTERVAL) {
setCustomValue(MINIMUM_REFRESH_INTERVAL.toString());
}
} else {
Expand Down
Loading