Skip to content

Commit 060ca2f

Browse files
authored
fix(macro): show a just-added Conditional's new condition and branch counts (#2151)
The condition dialog and the branch pages edit the step in place. A step added this session is a class instance that $state does not proxy, and the list got the same object back, so the row kept its old label. Fixes #2147
1 parent 03b3be7 commit 060ca2f

2 files changed

Lines changed: 49 additions & 2 deletions

File tree

‎src/gui/MacroGUIs/CommandList.conditional.test.ts‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { describe, expect, it, vi } from "vitest";
22
import { fireEvent, render } from "@testing-library/svelte";
3+
import { tick } from "svelte";
34

45
import { App } from "obsidian";
56
import CommandList from "./CommandList.svelte";
@@ -68,3 +69,48 @@ describe("CommandList conditional branch persistence", () => {
6869
expect(saveCommands).not.toHaveBeenCalled();
6970
});
7071
});
72+
73+
// A step added in this session is a class instance, which $state does not
74+
// proxy, so an in-place edit only shows if the list gets a new object (#2147).
75+
describe("CommandList conditional label after an edit", () => {
76+
function renderWith(handlers: Partial<Parameters<typeof createCommandListProps>[0]>) {
77+
const props = createCommandListProps({
78+
commands: [new ConditionalCommand()],
79+
app: new App() as never,
80+
plugin: {} as never,
81+
deleteCommand: vi.fn(),
82+
saveCommands: vi.fn(),
83+
...handlers,
84+
});
85+
return render(CommandList, { props });
86+
}
87+
88+
it("shows the new condition once its dialog is saved", async () => {
89+
const { getByLabelText, container } = renderWith({
90+
onConfigureCondition: (command) => {
91+
command.condition = { ...command.condition, variableName: "mood" } as typeof command.condition;
92+
return true;
93+
},
94+
});
95+
96+
await fireEvent.click(getByLabelText("Edit condition for (missing variable) is truthy"));
97+
await tick();
98+
99+
expect(container.querySelector(".conditionalSummary")?.textContent).toBe("$mood is truthy");
100+
expect(getByLabelText("Edit then branch for $mood is truthy")).toBeTruthy();
101+
});
102+
103+
it("shows the new branch count once its page is left", async () => {
104+
const { getByLabelText, container } = renderWith({
105+
onEditThenBranch: (command, onEdited) => {
106+
command.thenCommands = [new WaitCommand(100)];
107+
onEdited();
108+
},
109+
});
110+
111+
await fireEvent.click(getByLabelText("Edit then branch for (missing variable) is truthy"));
112+
await tick();
113+
114+
expect(container.querySelector(".conditionalBranches")?.textContent).toContain("Then: 1");
115+
});
116+
});

‎src/gui/MacroGUIs/CommandList.svelte‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,9 @@ function moveCommand(id: string, direction: -1 | 1) {
199199
200200
function updateCommand(command: ICommand) {
201201
// `renderable` for the same reason as moveCommand: replaceById maps over
202-
// `item.id`.
203-
commands = replaceById(renderable, command);
202+
// `item.id`. A copy, so the row re-renders after an edit made in place on a
203+
// step added this session: a class instance, which $state does not proxy.
204+
commands = replaceById(renderable, { ...command });
204205
persist();
205206
}
206207

0 commit comments

Comments
 (0)