Skip to content

Commit

Permalink
update more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hmalik88 committed Nov 4, 2024
1 parent 50e864f commit 47ffbf8
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { TransactionType } from '@metamask/transaction-controller';
import { renderWithConfirmContextProvider } from '../../../../../../../test/lib/confirmations/render-helpers';
import { getMockTypedSignConfirmStateForRequest } from '../../../../../../../test/data/confirmations/helper';
import { unapprovedTypedSignMsgV1 } from '../../../../../../../test/data/confirmations/typed_sign';
import * as snapUtils from '../../../../../../helpers/utils/snaps';
import TypedSignInfoV1 from './typed-sign-v1';

jest.mock(
Expand All @@ -16,6 +17,21 @@ jest.mock(
}),
);

jest.mock('../../../../../../../node_modules/@metamask/snaps-utils', () => {
const originalUtils = jest.requireActual(
'../../../../../../../node_modules/@metamask/snaps-utils',
);
return {
...originalUtils,
stripSnapPrefix: jest.fn().mockReturnValue('@metamask/examplesnap'),
getSnapPrefix: jest.fn().mockReturnValue('npm:'),
};
});

jest.mock('../../../../../../helpers/utils/snaps', () => ({
isSnapId: jest.fn(),
}));

describe('TypedSignInfo', () => {
it('correctly renders typed sign data request', () => {
const mockState = getMockTypedSignConfirmStateForRequest(
Expand All @@ -42,4 +58,50 @@ describe('TypedSignInfo', () => {
);
expect(container).toMatchInlineSnapshot(`<div />`);
});

it('displays "requestFromInfoSnap" tooltip when origin is a snap', async () => {
const mockState = getMockTypedSignConfirmStateForRequest({
id: '123',
type: TransactionType.signTypedData,
chainId: '0x5',
});
(snapUtils.isSnapId as jest.Mock).mockReturnValue(true);
const mockStore = configureMockStore([])(mockState);
const { queryByText } = renderWithConfirmContextProvider(
<TypedSignInfoV1 />,
mockStore,
);

const requestFromLabel = queryByText('Request from');

await requestFromLabel?.dispatchEvent(
new MouseEvent('mouseenter', { bubbles: true }),
);
expect(
queryByText('This is the Snap asking for your signature.'),
).toBeDefined();
});

it('displays "requestFromInfo" tooltip when origin is not a snap', async () => {
const mockState = getMockTypedSignConfirmStateForRequest({
id: '123',
type: TransactionType.signTypedData,
chainId: '0x5',
});
(snapUtils.isSnapId as jest.Mock).mockReturnValue(false);
const mockStore = configureMockStore([])(mockState);
const { queryByText } = renderWithConfirmContextProvider(
<TypedSignInfoV1 />,
mockStore,
);

const requestFromLabel = queryByText('Request from');

await requestFromLabel?.dispatchEvent(
new MouseEvent('mouseenter', { bubbles: true }),
);
expect(
queryByText('This is the site asking for your signature.'),
).toBeDefined();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
unapprovedTypedSignMsgV3,
} from '../../../../../../../test/data/confirmations/typed_sign';
import { renderWithConfirmContextProvider } from '../../../../../../../test/lib/confirmations/render-helpers';
import * as snapUtils from '../../../../../../helpers/utils/snaps';
import TypedSignInfo from './typed-sign';

jest.mock(
Expand All @@ -33,6 +34,21 @@ jest.mock('../../../../../../store/actions', () => {
};
});

jest.mock('../../../../../../../node_modules/@metamask/snaps-utils', () => {
const originalUtils = jest.requireActual(
'../../../../../../../node_modules/@metamask/snaps-utils',
);
return {
...originalUtils,
stripSnapPrefix: jest.fn().mockReturnValue('@metamask/examplesnap'),
getSnapPrefix: jest.fn().mockReturnValue('npm:'),
};
});

jest.mock('../../../../../../helpers/utils/snaps', () => ({
isSnapId: jest.fn(),
}));

describe('TypedSignInfo', () => {
it('renders origin for typed sign data request', () => {
const state = getMockTypedSignConfirmState();
Expand Down Expand Up @@ -127,4 +143,50 @@ describe('TypedSignInfo', () => {
);
expect(container).toMatchSnapshot();
});

it('displays "requestFromInfoSnap" tooltip when origin is a snap', async () => {
const mockState = getMockTypedSignConfirmStateForRequest({
id: '123',
type: TransactionType.signTypedData,
chainId: '0x5',
});
(snapUtils.isSnapId as jest.Mock).mockReturnValue(true);
const mockStore = configureMockStore([])(mockState);
const { queryByText } = renderWithConfirmContextProvider(
<TypedSignInfo />,
mockStore,
);

const requestFromLabel = queryByText('Request from');

await requestFromLabel?.dispatchEvent(
new MouseEvent('mouseenter', { bubbles: true }),
);
expect(
queryByText('This is the Snap asking for your signature.'),
).toBeDefined();
});

it('displays "requestFromInfo" tooltip when origin is not a snap', async () => {
const mockState = getMockTypedSignConfirmStateForRequest({
id: '123',
type: TransactionType.signTypedData,
chainId: '0x5',
});
(snapUtils.isSnapId as jest.Mock).mockReturnValue(false);
const mockStore = configureMockStore([])(mockState);
const { queryByText } = renderWithConfirmContextProvider(
<TypedSignInfo />,
mockStore,
);

const requestFromLabel = queryByText('Request from');

await requestFromLabel?.dispatchEvent(
new MouseEvent('mouseenter', { bubbles: true }),
);
expect(
queryByText('This is the site asking for your signature.'),
).toBeDefined();
});
});

0 comments on commit 47ffbf8

Please sign in to comment.