Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions packages/react/src/renderer.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
type ComponentRenderProps,
} from "./renderer";
import { schema } from "./schema";
import type { SetState } from "./catalog-types";

describe("Renderer", () => {
it("renders null for null spec", () => {
Expand Down Expand Up @@ -198,3 +199,37 @@ describe("Renderer", () => {
warn.mockRestore();
});
});

describe("defineRegistry", () => {
it("executes actions when no state setter is available", async () => {
const catalog = defineCatalog(schema, {
components: {},
actions: {
run: { description: "Run an action" },
},
});
const action = vi.fn(
async (
_params: Record<string, unknown> | undefined,
setState: SetState,
) => {
setState((prev) => prev);
},
);
const { handlers } = defineRegistry(catalog, {
actions: { run: action },
});

const actionHandlers = handlers(
() => undefined,
() => ({}),
);
const run = actionHandlers.run;
if (!run) {
throw new Error("Expected run action handler");
}
await run({ value: 1 });

expect(action).toHaveBeenCalledWith({ value: 1 }, expect.any(Function), {});
});
});
6 changes: 2 additions & 4 deletions packages/react/src/renderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1142,11 +1142,9 @@ export function defineRegistry<C extends Catalog>(
> = {};
for (const [name, actionFn] of actionMap) {
result[name] = async (params) => {
const setState = getSetState();
const setState = getSetState() ?? (() => undefined);
const state = getState();
if (setState) {
await actionFn(params, setState, state);
}
await actionFn(params, setState, state);
};
}
return result;
Expand Down