Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
Mrtenz committed Oct 11, 2024
1 parent 6a31d26 commit 0d87cc5
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 22 deletions.
22 changes: 0 additions & 22 deletions packages/examples/packages/preinstalled/src/index.test.ts

This file was deleted.

64 changes: 64 additions & 0 deletions packages/examples/packages/preinstalled/src/index.test.tsx
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');
});
});
});

0 comments on commit 0d87cc5

Please sign in to comment.