Skip to content

Commit 22eff56

Browse files
authored
refactor(account-api): split tests (#345)
Splitting the big `index.test.ts` into several test files.
1 parent 4083743 commit 22eff56

6 files changed

Lines changed: 207 additions & 158 deletions

File tree

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import type { KeyringAccountEntropyMnemonicOptions } from '@metamask/keyring-api';
2+
import { KeyringAccountEntropyTypeOption } from '@metamask/keyring-api';
3+
4+
import { isBip44Account, assertIsBip44Account } from './bip44';
5+
import { MOCK_ENTROPY_SOURCE_1, MOCK_HD_ACCOUNT_1 } from '../mocks';
6+
7+
describe('bip44', () => {
8+
describe('isBip44Account', () => {
9+
it('returns true if the account is BIP-44 compatible', () => {
10+
expect(isBip44Account(MOCK_HD_ACCOUNT_1)).toBe(true);
11+
expect(() => assertIsBip44Account(MOCK_HD_ACCOUNT_1)).not.toThrow();
12+
});
13+
14+
it.each([
15+
{
16+
tc: 'missing type',
17+
options: {
18+
entropy: {
19+
// Missing type.
20+
id: MOCK_ENTROPY_SOURCE_1,
21+
groupIndex: 0,
22+
derivationPath: '',
23+
},
24+
},
25+
},
26+
{
27+
tc: 'missing id',
28+
options: {
29+
entropy: {
30+
type: KeyringAccountEntropyTypeOption.Mnemonic,
31+
// Missing id.
32+
groupIndex: 0,
33+
derivationPath: '',
34+
},
35+
},
36+
},
37+
{
38+
tc: 'missing groupIndex',
39+
options: {
40+
entropy: {
41+
type: 'mnemonic',
42+
id: MOCK_ENTROPY_SOURCE_1,
43+
// Missing groupIndex.
44+
derivationPath: '',
45+
},
46+
},
47+
},
48+
{
49+
tc: 'missing derivationPath',
50+
options: {
51+
entropy: {
52+
type: 'mnemonic',
53+
id: MOCK_ENTROPY_SOURCE_1,
54+
groupIndex: 0,
55+
// Missing derivationPath.
56+
},
57+
},
58+
},
59+
])(
60+
'returns false if the account is not BIP-44 compatible with: $tc',
61+
({ options }) => {
62+
const account = {
63+
...MOCK_HD_ACCOUNT_1,
64+
options: {
65+
entropy:
66+
// Force the error case here.
67+
options as unknown as KeyringAccountEntropyMnemonicOptions,
68+
},
69+
};
70+
71+
expect(isBip44Account(account)).toBe(false);
72+
expect(() => assertIsBip44Account(account)).toThrow(
73+
'Account is not BIP-44 compatible',
74+
);
75+
},
76+
);
77+
});
78+
});
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import {
2+
DEFAULT_ACCOUNT_GROUP_UNIQUE_ID,
3+
toAccountGroupId,
4+
toDefaultAccountGroupId,
5+
} from './group';
6+
import { AccountWalletType, toAccountWalletId } from './wallet';
7+
8+
describe('group', () => {
9+
describe('toAccountGroupId', () => {
10+
it('converts an account wallet id and a unique id to a group id', () => {
11+
const walletId = toAccountWalletId(AccountWalletType.Keyring, 'test');
12+
const groupId = toAccountGroupId(walletId, 'test');
13+
14+
expect(groupId.startsWith(walletId)).toBe(true);
15+
expect(groupId.endsWith('/test')).toBe(true);
16+
});
17+
});
18+
19+
describe('toDefaultAccountGroupId', () => {
20+
it('converts an account wallet id and to the default group id', () => {
21+
const walletId = toAccountWalletId(AccountWalletType.Keyring, 'test');
22+
const groupId = toDefaultAccountGroupId(walletId);
23+
24+
expect(groupId.startsWith(walletId)).toBe(true);
25+
expect(groupId.endsWith(`/${DEFAULT_ACCOUNT_GROUP_UNIQUE_ID}`)).toBe(
26+
true,
27+
);
28+
});
29+
});
30+
});
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import type { MultichainAccountGroupId } from './group';
2+
import {
3+
getGroupIndexFromMultichainAccountGroupId,
4+
isMultichainAccountGroupId,
5+
toMultichainAccountGroupId,
6+
} from './group';
7+
import { toMultichainAccountWalletId } from './wallet';
8+
import { MOCK_ENTROPY_SOURCE_1 } from '../../mocks';
9+
import { toAccountGroupId } from '../group';
10+
import { AccountWalletType, toAccountWalletId } from '../wallet';
11+
12+
describe('multichain group', () => {
13+
describe('toMultichainAccountGroupId', () => {
14+
it('computes a multichain account group id with a group index', () => {
15+
const groupIndex = 1;
16+
17+
const walletId = toMultichainAccountWalletId(MOCK_ENTROPY_SOURCE_1);
18+
const groupId = toMultichainAccountGroupId(walletId, groupIndex);
19+
20+
expect(groupId.startsWith(walletId)).toBe(true);
21+
expect(groupId.endsWith(`/${groupIndex}`)).toBe(true);
22+
});
23+
});
24+
25+
describe('isMultichainAccountGroupId', () => {
26+
it('returns true if a group id is a multichain group id', () => {
27+
const walletId = toMultichainAccountWalletId(MOCK_ENTROPY_SOURCE_1);
28+
const groupId = toMultichainAccountGroupId(walletId, 0);
29+
30+
expect(isMultichainAccountGroupId(groupId)).toBe(true);
31+
});
32+
33+
it('fails if a group id is not a multichain group id', () => {
34+
const walletId = toAccountWalletId(
35+
AccountWalletType.Keyring,
36+
MOCK_ENTROPY_SOURCE_1,
37+
);
38+
const groupId = toAccountGroupId(walletId, 'test');
39+
40+
expect(isMultichainAccountGroupId(groupId)).toBe(false);
41+
});
42+
});
43+
44+
describe('getGroupIndexFromMultichainAccountGroupId', () => {
45+
it('extracts the group index from its group id', () => {
46+
const groupIndex = 2;
47+
48+
const walletId = toMultichainAccountWalletId(MOCK_ENTROPY_SOURCE_1);
49+
const groupId = toMultichainAccountGroupId(walletId, groupIndex);
50+
51+
expect(getGroupIndexFromMultichainAccountGroupId(groupId)).toBe(
52+
groupIndex,
53+
);
54+
});
55+
56+
it('throws if it cannot extract group index', () => {
57+
const walletId = toAccountWalletId(
58+
AccountWalletType.Keyring,
59+
MOCK_ENTROPY_SOURCE_1,
60+
);
61+
const groupId = toAccountGroupId(walletId, 'test');
62+
63+
expect(() =>
64+
getGroupIndexFromMultichainAccountGroupId(
65+
// Force the error case even though, type wise, this should not
66+
// be possible!
67+
groupId as unknown as MultichainAccountGroupId,
68+
),
69+
).toThrow('Unable to extract group index');
70+
});
71+
});
72+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { toMultichainAccountWalletId } from './wallet';
2+
import { MOCK_ENTROPY_SOURCE_1 } from '../../mocks';
3+
4+
describe('multichain wallet', () => {
5+
describe('toMultichainAccountWalletId', () => {
6+
it('computes a multichain account wallet id with an entropy source', () => {
7+
const walletId = toMultichainAccountWalletId(MOCK_ENTROPY_SOURCE_1);
8+
9+
expect(walletId.endsWith(`:${MOCK_ENTROPY_SOURCE_1}`)).toBe(true);
10+
});
11+
});
12+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { AccountWalletType, toAccountWalletId } from './wallet';
2+
3+
describe('wallet', () => {
4+
describe('toAccountWalletId', () => {
5+
it.each(Object.values(AccountWalletType))(
6+
'computes a wallet id for: %s',
7+
(type: AccountWalletType) => {
8+
const walletId = toAccountWalletId(type, 'test');
9+
10+
expect(walletId.startsWith(type)).toBe(true);
11+
expect(walletId.endsWith(':test')).toBe(true);
12+
},
13+
);
14+
});
15+
});

packages/account-api/src/index.test.ts

Lines changed: 0 additions & 158 deletions
This file was deleted.

0 commit comments

Comments
 (0)