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
2 changes: 1 addition & 1 deletion static/app/chartcuterie/dashboardsWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const makeDashboardsWidgetCharts = (
const {thresholds} = data.widget;
if (
thresholds &&
(defined(thresholds.max_values.max1) || defined(thresholds.max_values.max2))
(defined(thresholds.max_values?.max1) || defined(thresholds.max_values?.max2))
) {
extraPlottables.push(
new Thresholds({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,29 @@ const exampleThresholdsConfig: ThresholdsConfig = {
};

describe('Widget Builder > ThresholdsStep', () => {
it('renders without crashing when max_values is undefined', async () => {
const onChange = jest.fn();
const configWithoutMaxValues = {
max_values: undefined,
unit: null,
} as unknown as ThresholdsConfig;
render(
<Thresholds
thresholdsConfig={configWithoutMaxValues}
onThresholdChange={onChange}
onUnitChange={onChange}
errors={{}}
/>
);

// Component should render with empty max value inputs instead of throwing
expect(await screen.findByLabelText('First Minimum')).toBeInTheDocument();
expect(screen.getByLabelText('First Maximum', {selector: 'input'})).toHaveValue(null);
expect(screen.getByLabelText('Second Maximum', {selector: 'input'})).toHaveValue(
null
);
});

it('renders thresholds step', async () => {
const onChange = jest.fn();
render(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ enum ThresholdMaxKeys {
type ThresholdMaxValues = Partial<Record<ThresholdMaxKeys, number>>;

export type ThresholdsConfig = {
max_values: ThresholdMaxValues;
max_values: ThresholdMaxValues | undefined;
unit: string | null;
preferredPolarity?: Polarity;
};
Expand Down Expand Up @@ -102,8 +102,8 @@ export function Thresholds({
onPolarityChange,
}: ThresholdsStepProps) {
const theme = useTheme();
const maxOneValue = thresholdsConfig?.max_values[ThresholdMaxKeys.MAX_1] ?? '';
const maxTwoValue = thresholdsConfig?.max_values[ThresholdMaxKeys.MAX_2] ?? '';
const maxOneValue = thresholdsConfig?.max_values?.[ThresholdMaxKeys.MAX_1] ?? '';
const maxTwoValue = thresholdsConfig?.max_values?.[ThresholdMaxKeys.MAX_2] ?? '';
const unit = thresholdsConfig?.unit ?? dataUnit;
const unitOptions = getThresholdUnitSelectOptions(dataType);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ type Props = {
};

export function ThresholdsHoverWrapper({children, thresholds, type}: Props) {
const {
unit,
max_values: {max1, max2},
} = thresholds;
const {unit, max_values: {max1, max2} = {}} = thresholds;
const theme = useTheme();
const formattedUnit =
unit && (type === 'duration' ? `${unit}s` : `/${unit.split('/')[1]}`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export function ThresholdsSection({
}

if (newThresholds) {
newThresholds.max_values ??= {};
if (value) {
newThresholds.max_values[maxKey] = Number(value);
} else {
Expand All @@ -92,7 +93,7 @@ export function ThresholdsSection({

// Check if the value cleared all of the max values
if (
newThresholds &&
newThresholds?.max_values &&
Object.values(newThresholds.max_values).every(
nextMaxValue => !defined(nextMaxValue)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -404,8 +404,8 @@ function VisualizationWidgetContent({
);

if (
defined(widget.thresholds?.max_values.max1) ||
defined(widget.thresholds?.max_values.max2)
defined(widget.thresholds?.max_values?.max1) ||
defined(widget.thresholds?.max_values?.max2)
) {
plottables.push(
new Thresholds({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,14 @@ function BigNumberWidgetVisualizationInner(props: BigNumberWidgetVisualizationPr
return (
<Wrapper>
<NumberAndDifferenceContainer>
{defined(props.thresholds?.max_values.max1) &&
{defined(props.thresholds?.max_values?.max1) &&
defined(props.thresholds?.max_values.max2) && (
<ThresholdsIndicator
preferredPolarity={props.preferredPolarity}
thresholds={{
unit: props.thresholds.unit ?? undefined,
max_values: {
max1: props.thresholds.max_values.max1,
max1: props.thresholds.max_values?.max1,
max2: props.thresholds.max_values.max2,
},
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,20 @@ export class Thresholds implements Plottable {
this.thresholds = {
...thresholds,
max_values: {
max1: thresholds.max_values.max1
max1: thresholds.max_values?.max1
? normalizeUnit(thresholds.max_values.max1, thresholdUnit, dataType)
: thresholds.max_values.max1,
max2: thresholds.max_values.max2
: thresholds.max_values?.max1,
max2: thresholds.max_values?.max2
? normalizeUnit(thresholds.max_values.max2, thresholdUnit, dataType)
: thresholds.max_values.max2,
: thresholds.max_values?.max2,
},
};
} else {
this.thresholds = thresholds;
}

this.showLabels = options.showLabels ?? false;
this.isEmpty = !this.thresholds.max_values.max1 && !this.thresholds.max_values.max2;
this.isEmpty = !this.thresholds.max_values?.max1 && !this.thresholds.max_values?.max2;
}

toMarkArea(
Expand All @@ -95,7 +95,7 @@ export class Thresholds implements Plottable {
}

toMarkAreas(theme: Theme, maxOffset = this.maxOffset) {
const {max1, max2} = this.thresholds.max_values;
const {max1, max2} = this.thresholds.max_values ?? {};
const isHigherBetter = this.thresholds.preferredPolarity === '+';

const colorOrder = isHigherBetter
Expand Down Expand Up @@ -160,7 +160,7 @@ export class Thresholds implements Plottable {
}

toMarkLines(theme: Theme, maxOffset = this.maxOffset) {
const {max1, max2} = this.thresholds.max_values;
const {max1, max2} = this.thresholds.max_values ?? {};
const isHigherBetter = this.thresholds.preferredPolarity === '+';

// For '-' (lower is better): Good (green), Meh (yellow), Poor (red) bottom-to-top
Expand Down
Loading