Skip to content

Commit

Permalink
Add support for form property in snaps-simulation
Browse files Browse the repository at this point in the history
  • Loading branch information
Mrtenz committed Oct 11, 2024
1 parent f4890dd commit 82fcd40
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 2 deletions.
91 changes: 91 additions & 0 deletions packages/snaps-simulation/src/interface.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
SelectorOption,
Card,
Selector,
Field,
} from '@metamask/snaps-sdk/jsx';
import {
getJsxElementFromComponent,
Expand Down Expand Up @@ -446,6 +447,31 @@ describe('getElement', () => {
form: 'form-2',
});
});

it('gets a button with a form property', () => {
const content = (
<Box>
<Form name="referenced-form">
<Field>
<Input name="input" />
</Field>
</Form>
<Button name="button" type="submit" form="referenced-form">
foo
</Button>
</Box>
);
const result = getElement(content, 'button');

expect(result).toStrictEqual({
element: (
<Button name="button" type="submit" form="referenced-form">
foo
</Button>
),
form: 'referenced-form',
});
});
});

describe('clickElement', () => {
Expand Down Expand Up @@ -555,6 +581,71 @@ describe('clickElement', () => {
});
});

it('sends a `FormSubmitEvent` to the Snap for a button with a form property', async () => {
const content = (
<Box>
<Form name="referenced-form">
<Field>
<Input name="input" />
</Field>
</Form>
<Button name="button" type="submit" form="referenced-form">
foo
</Button>
</Box>
);

const interfaceId = await interfaceController.createInterface(
MOCK_SNAP_ID,
content,
);

await clickElement(
rootControllerMessenger,
interfaceId,
content,
MOCK_SNAP_ID,
'button',
);

expect(handleRpcRequestMock).toHaveBeenCalledWith(MOCK_SNAP_ID, {
origin: '',
handler: HandlerType.OnUserInput,
request: {
jsonrpc: '2.0',
method: ' ',
params: {
event: {
type: UserInputEventType.ButtonClickEvent,
name: 'button',
},
id: interfaceId,
context: null,
},
},
});

expect(handleRpcRequestMock).toHaveBeenCalledWith(MOCK_SNAP_ID, {
origin: '',
handler: HandlerType.OnUserInput,
request: {
jsonrpc: '2.0',
method: ' ',
params: {
event: {
type: UserInputEventType.FormSubmitEvent,
name: 'referenced-form',
value: {
input: null,
},
},
id: interfaceId,
context: null,
},
},
});
});

it('supports checkboxes', async () => {
const content = (
<Form name="form">
Expand Down
24 changes: 22 additions & 2 deletions packages/snaps-simulation/src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,26 @@ function getFormElement(form: FormElement, name: string) {
return { element, form: form.props.name };
}

/**
* Get an object containing the element, and optional form that's associated
* with the element if any.
*
* @param element - The JSX element.
* @returns An object containing the element and optional form.
*/
function getElementWithOptionalForm(element: NamedJSXElement):
| {
element: NamedJSXElement;
form?: string;
}
| undefined {
if (element.type !== 'Button' || !element.props.form) {
return { element };
}

return { element, form: element.props.form };
}

/**
* Get an element from a JSX tree with the given name.
*
Expand All @@ -246,7 +266,7 @@ export function getElement(
}
| undefined {
if (isJSXElementWithName(content, name)) {
return { element: content };
return getElementWithOptionalForm(content);
}

return walkJsx(content, (element) => {
Expand All @@ -255,7 +275,7 @@ export function getElement(
}

if (isJSXElementWithName(element, name)) {
return { element };
return getElementWithOptionalForm(element);
}

return undefined;
Expand Down

0 comments on commit 82fcd40

Please sign in to comment.