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:
Koen Vlaswinkel
2023-10-13 13:21:53 +02:00
parent 5e8de88ee0
commit 7baad1a5c6
2 changed files with 51 additions and 1 deletions

View File

@@ -63,7 +63,16 @@ export const ModelKindDropdown = ({
useEffect(() => { useEffect(() => {
const value = modeledMethod?.kind; const value = modeledMethod?.kind;
if (value === undefined && kinds.length > 0) {
if (kinds.length === 0) {
if (value !== "") {
onChangeKind("");
}
return;
}
if (value === undefined) {
onChangeKind(kinds[0]); onChangeKind(kinds[0]);
} }

View File

@@ -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: "",
}),
);
});
}); });