Skip to content

Commit

Permalink
[WIP] refactor AccountSelector
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeRx committed Oct 2, 2024
1 parent a870af3 commit e76ad10
Show file tree
Hide file tree
Showing 7 changed files with 644 additions and 46 deletions.
2 changes: 2 additions & 0 deletions packages/snaps-controllers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
"test:watch": "jest --watch"
},
"dependencies": {
"@metamask/accounts-controller": "^18.2.2",
"@metamask/approval-controller": "^7.0.2",
"@metamask/base-controller": "^6.0.2",
"@metamask/json-rpc-engine": "^9.0.2",
Expand Down Expand Up @@ -114,6 +115,7 @@
"@metamask/eslint-config-jest": "^12.1.0",
"@metamask/eslint-config-nodejs": "^12.1.0",
"@metamask/eslint-config-typescript": "^12.1.0",
"@metamask/keyring-controller": "^17.0.0",
"@metamask/template-snap": "^0.7.0",
"@swc/core": "1.3.78",
"@swc/jest": "^0.2.26",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { AccountsControllerGetSelectedMultichainAccountAction } from '@metamask/accounts-controller';
import type {
AcceptRequest,
HasApprovalRequest,
Expand Down Expand Up @@ -76,7 +77,8 @@ export type SnapInterfaceControllerAllowedActions =
| MaybeUpdateState
| HasApprovalRequest
| AcceptRequest
| GetSnap;
| GetSnap
| AccountsControllerGetSelectedMultichainAccountAction;

export type SnapInterfaceControllerActions =
| CreateInterface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,69 @@ import { AccountSelector } from './AccountSelector';

describe('AccountSelector', () => {
it('returns an account selector element without filter props', () => {
const result = <AccountSelector name="account" />;

expect(result).toStrictEqual({
type: 'AccountSelector',
props: {
name: 'account',
},
key: null,
});
});

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

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

it('returns an account selector element with the chainIds filter prop', () => {
it('returns an account selector element with the hideExternalAccounts filter prop', () => {
const result = (
<AccountSelector name="account" hideExternalAccounts={true} />
);

expect(result).toStrictEqual({
type: 'AccountSelector',
props: {
name: 'account',
hideExternalAccounts: true,
},
key: null,
});
});

it('returns an account selector element with the switchGlobalAccount filter prop', () => {
const result = (
<AccountSelector name="account" switchGlobalAccount={true} />
);

expect(result).toStrictEqual({
type: 'AccountSelector',
props: {
name: 'account',
switchGlobalAccount: true,
},
key: null,
});
});

it('returns an account selector element with a selectedAccount prop', () => {
const result = (
<AccountSelector
name="account"
chainIds={['bip122:p2wpkh']}
selectedAccount="1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed"
/>
);
Expand All @@ -32,18 +73,19 @@ describe('AccountSelector', () => {
type: 'AccountSelector',
props: {
name: 'account',
chainIds: ['bip122:p2wpkh'],
selectedAccount: '1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed',
},
key: null,
});
});

it('returns an account selector element with the hideExternalAccounts filter prop', () => {
it('returns an account selector element with all props', () => {
const result = (
<AccountSelector
name="account"
chainIds={['bip122:000000000019d6689c085ae165831e93']}
hideExternalAccounts={true}
switchGlobalAccount={true}
selectedAccount="1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed"
/>
);
Expand All @@ -52,7 +94,9 @@ describe('AccountSelector', () => {
type: 'AccountSelector',
props: {
name: 'account',
chainIds: ['bip122:000000000019d6689c085ae165831e93'],
hideExternalAccounts: true,
switchGlobalAccount: true,
selectedAccount: '1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed',
},
key: null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ import { createSnapComponent } from '../../component';
* 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 switchGlobalAccount - Whether to switch the currently selected account in MetaMask. 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[] | undefined;
hideExternalAccounts?: boolean | undefined;
selectedAccount: string;
switchGlobalAccount?: boolean | undefined;
selectedAccount?: string | undefined;
};

const TYPE = 'AccountSelector';
Expand All @@ -31,6 +33,7 @@ const TYPE = 'AccountSelector';
* 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.switchGlobalAccount - Whether to switch the currently selected account in MetaMask. 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.
Expand Down
12 changes: 10 additions & 2 deletions packages/snaps-sdk/src/jsx/validation.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -967,14 +967,22 @@ describe('FileInputStruct', () => {

describe('AccountSelectorStruct', () => {
it.each([
<AccountSelector name="account" />,
<AccountSelector
name="account"
chainIds={['bip122:000000000019d6689c085ae165831e93']}
/>,
<AccountSelector name="account" hideExternalAccounts />,
<AccountSelector name="account" switchGlobalAccount />,
<AccountSelector
name="account"
selectedAccount="1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed"
/>,
<AccountSelector
name="account"
hideExternalAccounts={true}
chainIds={['foo:bar']}
hideExternalAccounts
switchGlobalAccount
selectedAccount="1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed"
/>,
])('validates an account picker element', (value) => {
Expand All @@ -995,7 +1003,7 @@ describe('AccountSelectorStruct', () => {
<Text>foo</Text>
</AccountSelector>,
// @ts-expect-error - Invalid props.
<AccountSelector name="account" />,
<AccountSelector switchGlobalAccount="foo" />,
// @ts-expect-error - Invalid props.
<AccountSelector chainIds={['bip122:000000000019d6689c085ae165831e93']} />,
// @ts-expect-error - Invalid props.
Expand Down
3 changes: 2 additions & 1 deletion packages/snaps-sdk/src/jsx/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,8 @@ export const AccountSelectorStruct: Describe<AccountSelectorElement> = element(
null
>,
hideExternalAccounts: optional(boolean()),
selectedAccount: string(),
switchGlobalAccount: optional(boolean()),
selectedAccount: optional(string()),
},
);

Expand Down
Loading

0 comments on commit e76ad10

Please sign in to comment.