Copilot commented on code in PR #44957:
URL: https://github.com/apache/superset/pull/44957#discussion_r4186870843
##########
superset-frontend/src/explore/components/controls/ZoomConfigControl/ZoomConfigControl.tsx:
##########
@@ -72,7 +72,7 @@ export const ZoomConfigControl: FC<ZoomConfigsControlProps> =
({
}
const newValue = { ...value };
- newValue.configs.width = width;
+ newValue.configs = { ...newValue.configs, width };
newValue.values = computeConfigValues(newValue);
onChange(newValue);
Review Comment:
The four handlers now use two different patterns to rebuild `newValue`
(`const newValue = { ...value }; newValue.configs = ...` vs an object literal
with `configs: ...`). For consistency and to reduce future regressions,
consider consolidating to a single pattern (or a small helper like
`updateConfigs(partial)` that always rebuilds `configs` and recomputes
`values`). This is optional, but it will make the immutability guarantee easier
to maintain.
##########
superset-frontend/src/explore/components/controls/ZoomConfigControl/ZoomConfigControl.tsx:
##########
@@ -84,16 +84,18 @@ export const ZoomConfigControl: FC<ZoomConfigsControlProps>
= ({
}
const newValue = { ...value };
- newValue.configs.height = height;
+ newValue.configs = { ...newValue.configs, height };
newValue.values = computeConfigValues(newValue);
onChange(newValue);
Review Comment:
The four handlers now use two different patterns to rebuild `newValue`
(`const newValue = { ...value }; newValue.configs = ...` vs an object literal
with `configs: ...`). For consistency and to reduce future regressions,
consider consolidating to a single pattern (or a small helper like
`updateConfigs(partial)` that always rebuilds `configs` and recomputes
`values`). This is optional, but it will make the immutability guarantee easier
to maintain.
##########
superset-frontend/src/explore/components/controls/ZoomConfigControl/ZoomConfigControl.test.tsx:
##########
@@ -0,0 +1,86 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+import { render, screen, userEvent } from 'spec/helpers/testing-library';
+import ZoomConfigControl from './ZoomConfigControl';
+import { ZoomConfigs } from './types';
+
+jest.mock('./ZoomConfigsChart', () => () => null);
+
+test.each([
+ {
+ field: 'width' as const,
+ sliderIndex: 0,
+ value: {
+ type: 'FIXED',
+ configs: { zoom: 5, width: 100, height: 100 },
+ values: {},
+ } satisfies ZoomConfigs,
+ },
+ {
+ field: 'height' as const,
+ sliderIndex: 1,
+ value: {
+ type: 'FIXED',
+ configs: { zoom: 5, width: 100, height: 100 },
+ values: {},
+ } satisfies ZoomConfigs,
+ },
+ {
+ field: 'slope' as const,
+ sliderIndex: 2,
+ value: {
+ type: 'LINEAR',
+ configs: { zoom: 5, width: 100, height: 100, slope: 2 },
+ values: {},
+ } satisfies ZoomConfigs,
+ },
+ {
+ field: 'exponent' as const,
+ sliderIndex: 3,
+ value: {
+ type: 'EXP',
+ configs: { zoom: 5, width: 100, height: 100, exponent: 1 },
+ values: {},
+ } satisfies ZoomConfigs,
+ },
+])(
+ 'copies configs for $field without mutating the incoming value',
+ async ({ field, sliderIndex, value }) => {
+ const onChange = jest.fn();
+ const originalFieldValue = value.configs[field];
+ Object.freeze(value.configs);
+
+ render(
+ <ZoomConfigControl
+ name="zoomConfig"
+ label="Zoom configuration"
+ onChange={onChange}
+ value={value}
+ />,
+ );
+
+ const slider = screen.getAllByRole('slider')[sliderIndex];
+ await userEvent.click(slider);
+ await userEvent.type(slider, '{arrowright}');
+
+ expect(value.configs[field]).toBe(originalFieldValue);
+ const updatedValue = onChange.mock.lastCall?.[0] as ZoomConfigs;
+ expect(updatedValue.configs).not.toBe(value.configs);
Review Comment:
This test verifies non-mutation and a new `configs` reference, but it
doesn’t assert that the updated `configs[field]` actually changed. As written,
it could pass even if a ref copy is made without updating the field. Consider
adding an assertion like
`expect(updatedValue.configs[field]).not.toBe(originalFieldValue)` (or a
specific expected delta) and, if possible, avoid relying on
`getAllByRole('slider')[sliderIndex]` by querying the slider by accessible
name/label to make the test less brittle to UI reordering.
##########
superset-frontend/src/explore/components/controls/ZoomConfigControl/ZoomConfigControl.tsx:
##########
@@ -102,8 +104,10 @@ export const ZoomConfigControl:
FC<ZoomConfigsControlProps> = ({
const onBaseExponentChange = (exponent: number) => {
setBaseExponent(exponent);
if (value && isZoomConfigsExp(value)) {
- const newValue = { ...value };
- newValue.configs.exponent = exponent;
+ const newValue = {
+ ...value,
+ configs: { ...value.configs, exponent },
+ };
newValue.values = computeConfigValues(newValue);
onChange(newValue);
Review Comment:
The four handlers now use two different patterns to rebuild `newValue`
(`const newValue = { ...value }; newValue.configs = ...` vs an object literal
with `configs: ...`). For consistency and to reduce future regressions,
consider consolidating to a single pattern (or a small helper like
`updateConfigs(partial)` that always rebuilds `configs` and recomputes
`values`). This is optional, but it will make the immutability guarantee easier
to maintain.
##########
superset-frontend/src/explore/components/controls/ZoomConfigControl/ZoomConfigControl.tsx:
##########
@@ -84,16 +84,18 @@ export const ZoomConfigControl: FC<ZoomConfigsControlProps>
= ({
}
const newValue = { ...value };
- newValue.configs.height = height;
+ newValue.configs = { ...newValue.configs, height };
newValue.values = computeConfigValues(newValue);
onChange(newValue);
};
const onBaseSlopeChange = (slope: number) => {
setBaseSlope(slope);
if (value && isZoomConfigsLinear(value)) {
- const newValue = { ...value };
- newValue.configs.slope = slope;
+ const newValue = {
+ ...value,
+ configs: { ...value.configs, slope },
+ };
newValue.values = computeConfigValues(newValue);
onChange(newValue);
Review Comment:
The four handlers now use two different patterns to rebuild `newValue`
(`const newValue = { ...value }; newValue.configs = ...` vs an object literal
with `configs: ...`). For consistency and to reduce future regressions,
consider consolidating to a single pattern (or a small helper like
`updateConfigs(partial)` that always rebuilds `configs` and recomputes
`values`). This is optional, but it will make the immutability guarantee easier
to maintain.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]