Skip to content

Commit 7baad1a

Browse files
committed
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.
1 parent 5e8de88 commit 7baad1a

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

extensions/ql-vscode/src/view/model-editor/ModelKindDropdown.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,16 @@ export const ModelKindDropdown = ({
6363

6464
useEffect(() => {
6565
const value = modeledMethod?.kind;
66-
if (value === undefined && kinds.length > 0) {
66+
67+
if (kinds.length === 0) {
68+
if (value !== "") {
69+
onChangeKind("");
70+
}
71+
72+
return;
73+
}
74+
75+
if (value === undefined) {
6776
onChangeKind(kinds[0]);
6877
}
6978

extensions/ql-vscode/src/view/model-editor/__tests__/ModelKindDropdown.spec.tsx

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,45 @@ describe(ModelKindDropdown.name, () => {
9292
}),
9393
);
9494
});
95+
96+
it("does not call onChange when unmodeled and the kind is valid", () => {
97+
const method = createMethod();
98+
const modeledMethod = createModeledMethod({
99+
type: "none",
100+
kind: "",
101+
});
102+
103+
render(
104+
<ModelKindDropdown
105+
method={method}
106+
modeledMethod={modeledMethod}
107+
onChange={onChange}
108+
/>,
109+
);
110+
111+
expect(onChange).not.toHaveBeenCalled();
112+
});
113+
114+
it("calls onChange when unmodeled and the kind is valid", () => {
115+
const method = createMethod();
116+
const modeledMethod = createModeledMethod({
117+
type: "none",
118+
kind: "local",
119+
});
120+
121+
render(
122+
<ModelKindDropdown
123+
method={method}
124+
modeledMethod={modeledMethod}
125+
onChange={onChange}
126+
/>,
127+
);
128+
129+
expect(onChange).toHaveBeenCalledTimes(1);
130+
expect(onChange).toHaveBeenCalledWith(
131+
expect.objectContaining({
132+
kind: "",
133+
}),
134+
);
135+
});
95136
});

0 commit comments

Comments
 (0)