Skip to content

Commit

Permalink
add docs in README
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeRx committed Mar 26, 2024
1 parent 5282f5a commit e0b1903
Showing 1 changed file with 100 additions and 7 deletions.
107 changes: 100 additions & 7 deletions packages/snaps-jest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ All properties are optional, and have sensible defaults. The addresses are
randomly generated by default. Most values can be specified as a hex string, or
a decimal number.

It returns an object with the user interface that was shown by the snap, in the
It returns a `getInterface` function that gets the user interface that was shown by the snap, in the
[onTransaction](https://docs.metamask.io/snaps/reference/exports/#ontransaction)
function.

Expand All @@ -214,7 +214,9 @@ describe('MySnap', () => {
nonce: '0x0',
});

expect(response).toRender(panel([text('Hello, world!')]));
const screen = response.getInterface();

expect(screen).toRender(panel([text('Hello, world!')]));
});
});
```
Expand All @@ -233,7 +235,7 @@ All properties are optional, and have sensible defaults. The addresses are
randomly generated by default. Most values can be specified as a hex string, or
a decimal number.

It returns an object with the user interface that was shown by the snap, in the
It returns a `getInterface` function that gets the user interface that was shown by the snap, in the
[onSignature](https://docs.metamask.io/snaps/reference/exports/#onsignature)
function.

Expand All @@ -246,7 +248,9 @@ describe('MySnap', () => {
const { onSignature } = await installSnap(/* optional snap ID */);
const response = await onSignature();

expect(response).toRender(
const screen = response.getInterface();

expect(screen).toRender(
panel([text('You are using the personal_sign method')]),
);
});
Expand Down Expand Up @@ -303,7 +307,7 @@ describe('MySnap', () => {
### `snap.onHomePage`

The `onHomePage` function can be used to request the home page of the snap. It
takes no arguments, and returns a promise that resolves to the response from the
takes no arguments, and returns a promise that contains a `getInterface` function to get the response from the
[onHomePage](https://docs.metamask.io/snaps/reference/entry-points/#onhomepage)
function.

Expand All @@ -318,7 +322,9 @@ describe('MySnap', () => {
params: [],
});

expect(response).toRender(/* ... */);
const screen = response.getInterface();

expect(screen).toRender(/* ... */);
});
});
```
Expand All @@ -344,14 +350,16 @@ assert that a response from a snap matches an expected value:

### Interacting with user interfaces

#### `snap_dialog`

If your snap uses `snap_dialog` to show user interfaces, you can use the
`request.getInterface` function to interact with them. This method is present on
the return value of the `snap.request` function.

It waits for the user interface to be shown, and returns an object with
functions that can be used to interact with the user interface.

#### Example
##### Example

```js
import { installSnap } from '@metamask/snaps-jest';
Expand Down Expand Up @@ -384,6 +392,91 @@ describe('MySnap', () => {
});
```

#### handlers

If your snap uses handlers that shows user interfaces (`onTransaction`, `onSignature`, `onHomePage`), you can use the
`response.getInterface` function to interact with them. This method is present on
the return value of the `snap.request` function.

It returns an object with functions that can be used to interact with the user interface.

##### Example

```js
import { installSnap } from '@metamask/snaps-jest';

describe('MySnap', () => {
it('should do something', async () => {
const { onHomePage } = await installSnap(/* optional snap ID */);
const response = await onHomePage({
method: 'foo',
params: [],
});

const screen = response.getInterface();

expect(screen).toRender(/* ... */);
});
});
```

### User interactions in user interfaces

The object returned by the `getInterface` function exposes other functions to trigger user interactions in the user interface.

- `clickElement(elementName)`: Click on a button inside the user interface. If the button with the given name does not exist in the interface this method will throw.
- `typeInField(elementName, valueToType)`: Enter a value in a field inside the user interface. If the input field with the given name des not exist in the interface this method will throw.

#### Example

```js
import { installSnap } from '@metamask/snaps-jest';

describe('MySnap', () => {
it('should do something', async () => {
const { onHomePage } = await installSnap(/* optional snap ID */);
const response = await onHomePage({
method: 'foo',
params: [],
});

const screen = response.getInterface();

expect(screen).toRender(/* ... */);

await screen.clickElement('myButton');

const screen = response.getInterface();

expect(screen).toRender(/* ... */);
});
});
```

```js
import { installSnap } from '@metamask/snaps-jest';

describe('MySnap', () => {
it('should do something', async () => {
const { onHomePage } = await installSnap(/* optional snap ID */);
const response = await onHomePage({
method: 'foo',
params: [],
});

const screen = response.getInterface();

expect(screen).toRender(/* ... */);

await screen.typeInField('myField', 'the value to type');

const screen = response.getInterface();

expect(screen).toRender(/* ... */);
});
});
```

## Options

You can pass options to the test environment by adding a
Expand Down

0 comments on commit e0b1903

Please sign in to comment.