Fix unmodeled methods always being marked as unsaved
When opening a library group in the model editor, unmodeled methods would always be marked as unsaved, even if there were no changes. This was because the `ModelKindDropdown` component did not properly take into account that the `kind` for an unmodeled method should be an empty string. It would always try setting it to `undefined`, which would cause the method to be marked as unsaved. This fixes it by checking if there are valid kinds before setting the kind to the first one.
This commit is contained in:
@@ -63,7 +63,16 @@ export const ModelKindDropdown = ({
|
||||
|
||||
useEffect(() => {
|
||||
const value = modeledMethod?.kind;
|
||||
if (value === undefined && kinds.length > 0) {
|
||||
|
||||
if (kinds.length === 0) {
|
||||
if (value !== "") {
|
||||
onChangeKind("");
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (value === undefined) {
|
||||
onChangeKind(kinds[0]);
|
||||
}
|
||||
|
||||
|
||||
@@ -92,4 +92,45 @@ describe(ModelKindDropdown.name, () => {
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it("does not call onChange when unmodeled and the kind is valid", () => {
|
||||
const method = createMethod();
|
||||
const modeledMethod = createModeledMethod({
|
||||
type: "none",
|
||||
kind: "",
|
||||
});
|
||||
|
||||
render(
|
||||
<ModelKindDropdown
|
||||
method={method}
|
||||
modeledMethod={modeledMethod}
|
||||
onChange={onChange}
|
||||
/>,
|
||||
);
|
||||
|
||||
expect(onChange).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("calls onChange when unmodeled and the kind is valid", () => {
|
||||
const method = createMethod();
|
||||
const modeledMethod = createModeledMethod({
|
||||
type: "none",
|
||||
kind: "local",
|
||||
});
|
||||
|
||||
render(
|
||||
<ModelKindDropdown
|
||||
method={method}
|
||||
modeledMethod={modeledMethod}
|
||||
onChange={onChange}
|
||||
/>,
|
||||
);
|
||||
|
||||
expect(onChange).toHaveBeenCalledTimes(1);
|
||||
expect(onChange).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
kind: "",
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user