Skip to content

Commit 63f9821

Browse files
authored
fix(react-grab): preserve function options (#554)
1 parent c5190f7 commit 63f9821

2 files changed

Lines changed: 43 additions & 1 deletion

File tree

packages/react-grab/e2e/api-methods.spec.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,48 @@ test.describe("API Methods", () => {
165165
});
166166
expect(success).toBe(true);
167167
});
168+
169+
test("preserves a getContent callback passed to setOptions", async ({ reactGrab }) => {
170+
const result = await reactGrab.page.evaluate(async () => {
171+
const api = (
172+
window as {
173+
__REACT_GRAB__?: {
174+
copyElement: (element: Element) => Promise<boolean>;
175+
setOptions: (options: { getContent: () => string }) => void;
176+
};
177+
}
178+
).__REACT_GRAB__;
179+
const element = document.querySelector("[data-testid='todo-list'] h1");
180+
if (!api || !element) throw new Error("React Grab API or test element unavailable");
181+
182+
let getContentCallCount = 0;
183+
api.setOptions({
184+
getContent: () => {
185+
getContentCallCount += 1;
186+
return "custom content";
187+
},
188+
});
189+
const callCountBeforeCopy = getContentCallCount;
190+
const originalExecCommand = document.execCommand;
191+
document.execCommand = () => true;
192+
193+
try {
194+
return {
195+
callCountBeforeCopy,
196+
didCopy: await api.copyElement(element),
197+
getContentCallCount,
198+
};
199+
} finally {
200+
document.execCommand = originalExecCommand;
201+
}
202+
});
203+
204+
expect(result).toEqual({
205+
callCountBeforeCopy: 0,
206+
didCopy: true,
207+
getContentCallCount: 1,
208+
});
209+
});
168210
});
169211

170212
test.describe("Theme via setOptions", () => {

packages/react-grab/src/core/plugin-registry.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ const createPluginRegistry = (initialOptions: SettableOptions = {}) => {
102102
optionValue: OptionsState[OptionKey],
103103
) => {
104104
directOptionOverrides[optionKey] = optionValue;
105-
setStore("options", optionKey, optionValue);
105+
setStore("options", optionKey, () => optionValue);
106106
};
107107

108108
const SETTABLE_OPTION_KEYS: Array<keyof OptionsState> = [

0 commit comments

Comments
 (0)