1
1
import { AccountsControllerState } from '@metamask/accounts-controller' ;
2
2
import { captureException } from '@sentry/react-native' ;
3
3
import { Hex , isValidChecksumAddress } from '@metamask/utils' ;
4
+ import { InternalAccount } from '@metamask/keyring-api' ;
4
5
import DefaultPreference from 'react-native-default-preference' ;
5
6
import {
6
7
selectSelectedInternalAccount ,
@@ -13,9 +14,48 @@ import {
13
14
expectedUuid2 ,
14
15
internalAccount1 ,
15
16
MOCK_ADDRESS_2 ,
17
+ createMockInternalAccount ,
18
+ createMockUuidFromAddress ,
16
19
} from '../util/test/accountsControllerTestUtils' ;
17
20
import { RootState } from '../reducers' ;
18
21
import { AGREED } from '../constants/storage' ;
22
+ import {
23
+ MOCK_KEYRINGS ,
24
+ MOCK_KEYRING_CONTROLLER ,
25
+ } from './keyringController/testUtils' ;
26
+
27
+ /**
28
+ * Generates a mocked AccountsController state
29
+ * The internal accounts are generated in reverse order relative to the mock keyrings that are used for generation
30
+ *
31
+ * @returns - A mocked state of AccountsController
32
+ */
33
+ const MOCK_GENERATED_ACCOUNTS_CONTROLLER_REVERSED =
34
+ ( ) : AccountsControllerState => {
35
+ const reversedKeyringAccounts = [ ...MOCK_KEYRINGS ]
36
+ . reverse ( )
37
+ . flatMap ( ( keyring ) => [ ...keyring . accounts ] . reverse ( ) ) ;
38
+ const accountsForInternalAccounts = reversedKeyringAccounts . reduce (
39
+ ( record , keyringAccount , index ) => {
40
+ const lowercasedKeyringAccount = keyringAccount . toLowerCase ( ) ;
41
+ const accountName = `Account ${ index } ` ;
42
+ const uuid = createMockUuidFromAddress ( lowercasedKeyringAccount ) ;
43
+ const internalAccount = createMockInternalAccount (
44
+ lowercasedKeyringAccount ,
45
+ accountName ,
46
+ ) ;
47
+ record [ uuid ] = internalAccount ;
48
+ return record ;
49
+ } ,
50
+ { } as Record < string , InternalAccount > ,
51
+ ) ;
52
+ return {
53
+ internalAccounts : {
54
+ accounts : accountsForInternalAccounts ,
55
+ selectedAccount : Object . values ( accountsForInternalAccounts ) [ 0 ] . id ,
56
+ } ,
57
+ } ;
58
+ } ;
19
59
20
60
jest . mock ( '@sentry/react-native' , ( ) => ( {
21
61
captureException : jest . fn ( ) ,
@@ -84,47 +124,35 @@ describe('Accounts Controller Selectors', () => {
84
124
} ) ;
85
125
} ) ;
86
126
describe ( 'selectInternalAccounts' , ( ) => {
87
- it ( 'returns all internal accounts in the accounts controller state' , ( ) => {
88
- expect (
89
- selectInternalAccounts ( {
90
- engine : {
91
- backgroundState : {
92
- AccountsController : MOCK_ACCOUNTS_CONTROLLER_STATE ,
93
- } ,
127
+ it ( `returns internal accounts of the accounts controller sorted by the keyring controller's accounts` , ( ) => {
128
+ const mockAccountsControllerReversed =
129
+ MOCK_GENERATED_ACCOUNTS_CONTROLLER_REVERSED ( ) ;
130
+ const internalAccountsResult = selectInternalAccounts ( {
131
+ engine : {
132
+ backgroundState : {
133
+ KeyringController : MOCK_KEYRING_CONTROLLER ,
134
+ AccountsController : mockAccountsControllerReversed ,
94
135
} ,
95
- } as RootState ) ,
96
- ) . toEqual ( [
97
- {
98
- address : '0xc4955c0d639d99699bfd7ec54d9fafee40e4d272' ,
99
- id : expectedUuid ,
100
- metadata : { name : 'Account 1' , keyring : { type : 'HD Key Tree' } } ,
101
- options : { } ,
102
- methods : [
103
- 'personal_sign' ,
104
- 'eth_sign' ,
105
- 'eth_signTransaction' ,
106
- 'eth_signTypedData_v1' ,
107
- 'eth_signTypedData_v3' ,
108
- 'eth_signTypedData_v4' ,
109
- ] ,
110
- type : 'eip155:eoa' ,
111
- } ,
112
- {
113
- address : '0xc4966c0d659d99699bfd7eb54d8fafee40e4a756' ,
114
- id : expectedUuid2 ,
115
- metadata : { name : 'Account 2' , keyring : { type : 'HD Key Tree' } } ,
116
- options : { } ,
117
- methods : [
118
- 'personal_sign' ,
119
- 'eth_sign' ,
120
- 'eth_signTransaction' ,
121
- 'eth_signTypedData_v1' ,
122
- 'eth_signTypedData_v3' ,
123
- 'eth_signTypedData_v4' ,
124
- ] ,
125
- type : 'eip155:eoa' ,
126
136
} ,
127
- ] ) ;
137
+ } as RootState ) ;
138
+ const expectedInteralAccountsResult = Object . values (
139
+ mockAccountsControllerReversed . internalAccounts . accounts ,
140
+ ) . reverse ( ) ;
141
+
142
+ const internalAccountAddressesResult = internalAccountsResult . map (
143
+ ( account ) => account . address ,
144
+ ) ;
145
+ const expectedAccountAddressesResult = [ ...MOCK_KEYRINGS ] . flatMap (
146
+ ( keyring ) => keyring . accounts ,
147
+ ) ;
148
+
149
+ // Ensure accounts are correct
150
+ expect ( internalAccountsResult ) . toEqual ( expectedInteralAccountsResult ) ;
151
+
152
+ // Ensure that order of internal accounts match order of keyring accounts
153
+ expect ( internalAccountAddressesResult ) . toEqual (
154
+ expectedAccountAddressesResult ,
155
+ ) ;
128
156
} ) ;
129
157
} ) ;
130
158
describe ( 'selectSelectedInternalAccountChecksummedAddress' , ( ) => {
0 commit comments