Skip to content

Commit

Permalink
refactor to add hideExternalAccount
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeRx committed Oct 1, 2024
1 parent 070098d commit fbe40b8
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "hvi0QzsvwnbiFtw2bTGJojg09b8Y/RAlmDm1/H3MaYk=",
"shasum": "nq/+DKJXOe022/IoOctrZsmhRxt7ti91REvS9gXB5cE=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/browserify/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "YWLgNvM8qJee2ZAhuSD3UcYdUqlQ7OX1cYgW8HYYiTI=",
"shasum": "5J7aOXR8VNiyrtVEhXWujSg6oHR4cCUpzh5BgudN+b4=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
import { AccountSelector } from './AccountSelector';

describe('AccountSelector', () => {
it('returns an account selector element', () => {
it('returns an account selector element without filter props', () => {
const result = (
<AccountSelector
name="account"
selectedAccount="1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed"
/>
);

expect(result).toStrictEqual({
type: 'AccountSelector',
props: {
name: 'account',
selectedAccount: '1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed',
},
key: null,
});
});

it('returns an account selector element with the chainIds filter prop', () => {
const result = (
<AccountSelector
name="account"
Expand All @@ -20,4 +38,24 @@ describe('AccountSelector', () => {
key: null,
});
});

it('returns an account selector element with the hideExternalAccounts filter prop', () => {
const result = (
<AccountSelector
name="account"
hideExternalAccounts={true}
selectedAccount="1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed"
/>
);

expect(result).toStrictEqual({
type: 'AccountSelector',
props: {
name: 'account',
hideExternalAccounts: true,
selectedAccount: '1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed',
},
key: null,
});
});
});
7 changes: 5 additions & 2 deletions packages/snaps-sdk/src/jsx/components/form/AccountSelector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import { createSnapComponent } from '../../component';
* @property name - The name of the account selector. This is used to identify the
* state in the form data.
* @property chainIds - The chain IDs of the account selector. This should be a valid CAIP-2 chain ID array.
* @property hideExternalAccounts - Whether to hide accounts not owned by the snap. Defaults to `false`.
* @property selectedAccount - The default selected account of the account selector. This should be a
* valid account ID.
*/
export type AccountSelectorProps = {
name: string;
chainIds: CaipChainId[];
chainIds?: CaipChainId[] | undefined;
hideExternalAccounts?: boolean | undefined;
selectedAccount: string;
};

Expand All @@ -28,13 +30,14 @@ const TYPE = 'AccountSelector';
* @param props.name - The name of the account selector field. This is used to identify the
* state in the form data.
* @param props.chainIds - The chain IDs of the account selector. This should be a valid CAIP-2 chain ID array.
* @param props.hideExternalAccounts - Whether to hide accounts not owned by the snap. Defaults to `false`.
* @param props.selectedAccount - The default selected account of the account selector. This should be a
* valid account ID.
* @returns An account selector element.
* @example
* <AccountSelector name="account" chainIds={["eip155:1"]} selectedAccount="1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed" />
* @example
* <AccountSelector name="account" chainIds={["bip122:000000000019d6689c085ae165831e93"]} selectedAccount="1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed" />
* <AccountSelector name="account" chainIds={["bip122:000000000019d6689c085ae165831e93"]} selectedAccount="1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed" hideExternalAccounts />
*/
export const AccountSelector = createSnapComponent<
AccountSelectorProps,
Expand Down
9 changes: 8 additions & 1 deletion packages/snaps-sdk/src/jsx/validation.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -974,7 +974,7 @@ describe('AccountSelectorStruct', () => {
/>,
<AccountSelector
name="account"
chainIds={['eip155:1']}
hideExternalAccounts={true}
selectedAccount="1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed"
/>,
])('validates an account picker element', (value) => {
Expand Down Expand Up @@ -1005,6 +1005,13 @@ describe('AccountSelectorStruct', () => {
chainIds={['foo:bar']}
selectedAccount="1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed"
/>,
<AccountSelector
name="account"
chainIds={['foo:bar']}
selectedAccount="1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed"
// @ts-expect-error - Invalid props.
hideExternalAccounts={42}
/>,
<Text>foo</Text>,
<Box>
<Text>foo</Text>
Expand Down
11 changes: 5 additions & 6 deletions packages/snaps-sdk/src/jsx/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,12 +340,11 @@ export const AccountSelectorStruct: Describe<AccountSelectorElement> = element(
'AccountSelector',
{
name: string(),
chainIds: array(
CaipChainIdStruct as unknown as Struct<
Infer<typeof CaipChainIdStruct>,
Infer<typeof CaipChainIdStruct>
>,
),
chainIds: optional(array(CaipChainIdStruct)) as unknown as Struct<
Infer<typeof CaipChainIdStruct>[] | undefined,
null
>,
hideExternalAccounts: optional(boolean()),
selectedAccount: string(),
},
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ exports[`SnapsWebpackPlugin applies a transform 1`] = `
})();"
`;

exports[`SnapsWebpackPlugin applies a transform 2`] = `
"(() => {
var __webpack_exports__ = {};
const foo = 'bar';
})();"
`;

exports[`SnapsWebpackPlugin forwards the options 1`] = `
"/******/(() => {
// webpackBootstrap
Expand Down

0 comments on commit fbe40b8

Please sign in to comment.