-
Notifications
You must be signed in to change notification settings - Fork 561
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update preinstalled example to make buttons actually work (#2829)
Previously the preinstalled example didn't have any logic, and since it's set up to hide the header, there was no way to get out of the dialog. I've added some basic logic now, so it has a working cancel button, and a form with a submit button.
- Loading branch information
Showing
7 changed files
with
158 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
packages/examples/packages/preinstalled/src/components/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './dialog'; | ||
export * from './result'; |
34 changes: 34 additions & 0 deletions
34
packages/examples/packages/preinstalled/src/components/result.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import type { SnapComponent } from '@metamask/snaps-sdk/jsx'; | ||
import { | ||
Copyable, | ||
Box, | ||
Container, | ||
Heading, | ||
Text, | ||
Footer, | ||
Button, | ||
} from '@metamask/snaps-sdk/jsx'; | ||
|
||
export type ResultProps = { | ||
value: string; | ||
}; | ||
|
||
/** | ||
* A result component. | ||
* | ||
* @param props - The props of the component. | ||
* @param props.value - The value to display. | ||
* @returns The result component. | ||
*/ | ||
export const Result: SnapComponent<ResultProps> = ({ value }) => ( | ||
<Container> | ||
<Box> | ||
<Heading>Custom Dialog</Heading> | ||
<Text>The form was submitted with the following value:</Text> | ||
<Copyable value={value} /> | ||
</Box> | ||
<Footer> | ||
<Button name="ok">Ok</Button> | ||
</Footer> | ||
</Container> | ||
); |
This file was deleted.
Oops, something went wrong.
64 changes: 64 additions & 0 deletions
64
packages/examples/packages/preinstalled/src/index.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { expect } from '@jest/globals'; | ||
import { installSnap } from '@metamask/snaps-jest'; | ||
|
||
import { Dialog, Result } from './components'; | ||
|
||
describe('onRpcRequest', () => { | ||
it('throws an error if the requested method does not exist', async () => { | ||
const { request } = await installSnap(); | ||
|
||
const response = await request({ | ||
method: 'foo', | ||
}); | ||
|
||
expect(response).toRespondWithError({ | ||
code: -32601, | ||
message: 'The method does not exist / is not available.', | ||
stack: expect.any(String), | ||
data: { | ||
method: 'foo', | ||
cause: null, | ||
}, | ||
}); | ||
}); | ||
|
||
describe('showDialog', () => { | ||
it('closes the dialog when clicking cancel, and resolves with `null`', async () => { | ||
const { request } = await installSnap(); | ||
|
||
const response = request({ | ||
method: 'showDialog', | ||
}); | ||
|
||
const formScreen = await response.getInterface(); | ||
expect(formScreen).toRender(<Dialog />); | ||
|
||
await formScreen.clickElement('cancel'); | ||
|
||
const result = await response; | ||
expect(result).toRespondWith(null); | ||
}); | ||
|
||
it('shows the result when clicking confirm, and resolves with the result', async () => { | ||
const { request } = await installSnap(); | ||
|
||
const response = request({ | ||
method: 'showDialog', | ||
}); | ||
|
||
const formScreen = await response.getInterface(); | ||
expect(formScreen).toRender(<Dialog />); | ||
|
||
await formScreen.typeInField('custom-input', 'foo bar'); | ||
await formScreen.clickElement('confirm'); | ||
|
||
const resultScreen = await response.getInterface(); | ||
expect(resultScreen).toRender(<Result value="foo bar" />); | ||
|
||
await resultScreen.clickElement('ok'); | ||
|
||
const result = await response; | ||
expect(result).toRespondWith('foo bar'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters