@@ -31,6 +31,7 @@ import { AccountsController, EMPTY_ACCOUNT } from './AccountsController';
31
31
import {
32
32
createExpectedInternalAccount ,
33
33
createMockInternalAccount ,
34
+ createMockInternalAccountOptions ,
34
35
ETH_EOA_METHODS ,
35
36
} from './tests/mocks' ;
36
37
import {
@@ -279,7 +280,7 @@ describe('AccountsController', () => {
279
280
} ) ;
280
281
281
282
describe ( 'onSnapStateChange' , ( ) => {
282
- it ( 'be used enable an account if the Snap is enabled and not blocked' , async ( ) => {
283
+ it ( 'enables an account if the Snap is enabled and not blocked' , async ( ) => {
283
284
const messenger = buildMessenger ( ) ;
284
285
const mockSnapAccount = createMockInternalAccount ( {
285
286
id : 'mock-id' ,
@@ -325,7 +326,7 @@ describe('AccountsController', () => {
325
326
expect ( updatedAccount . metadata . snap ?. enabled ) . toBe ( true ) ;
326
327
} ) ;
327
328
328
- it ( 'be used disable an account if the Snap is disabled' , async ( ) => {
329
+ it ( 'disables an account if the Snap is disabled' , async ( ) => {
329
330
const messenger = buildMessenger ( ) ;
330
331
const mockSnapAccount = createMockInternalAccount ( {
331
332
id : 'mock-id' ,
@@ -371,7 +372,7 @@ describe('AccountsController', () => {
371
372
expect ( updatedAccount . metadata . snap ?. enabled ) . toBe ( false ) ;
372
373
} ) ;
373
374
374
- it ( 'be used disable an account if the Snap is blocked' , async ( ) => {
375
+ it ( 'disables an account if the Snap is blocked' , async ( ) => {
375
376
const messenger = buildMessenger ( ) ;
376
377
const mockSnapAccount = createMockInternalAccount ( {
377
378
id : 'mock-id' ,
@@ -416,6 +417,61 @@ describe('AccountsController', () => {
416
417
417
418
expect ( updatedAccount . metadata . snap ?. enabled ) . toBe ( false ) ;
418
419
} ) ;
420
+
421
+ it ( 'does not trigger any unnecessary updates' , async ( ) => {
422
+ const messenger = buildMessenger ( ) ;
423
+ const mockSnapAccount = createMockInternalAccount ( {
424
+ id : 'mock-id' ,
425
+ name : 'Snap Account 1' ,
426
+ address : '0x0' ,
427
+ keyringType : KeyringTypes . snap ,
428
+ snap : {
429
+ id : 'mock-snap' ,
430
+ name : 'mock-snap-name' ,
431
+ enabled : false , // Will be enabled later by a `SnapController:stateChange`.
432
+ } ,
433
+ } ) ;
434
+ const mockSnapChangeState = {
435
+ snaps : {
436
+ 'mock-snap' : {
437
+ enabled : true ,
438
+ id : 'mock-snap' ,
439
+ blocked : false ,
440
+ status : SnapStatus . Running ,
441
+ } ,
442
+ } ,
443
+ // TODO: Replace `any` with type
444
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
445
+ } as any as SnapControllerState ;
446
+ const mockStateChange = jest . fn ( ) ;
447
+ const { accountsController } = setupAccountsController ( {
448
+ initialState : {
449
+ internalAccounts : {
450
+ accounts : {
451
+ [ mockSnapAccount . id ] : mockSnapAccount ,
452
+ } ,
453
+ selectedAccount : mockSnapAccount . id ,
454
+ } ,
455
+ } ,
456
+ messenger,
457
+ } ) ;
458
+
459
+ messenger . subscribe ( 'AccountsController:stateChange' , mockStateChange ) ;
460
+
461
+ // First update will update the account's metadata, thus triggering a `AccountsController:stateChange`.
462
+ messenger . publish ( 'SnapController:stateChange' , mockSnapChangeState , [ ] ) ;
463
+ const updatedAccount = accountsController . getAccountExpect (
464
+ mockSnapAccount . id ,
465
+ ) ;
466
+ expect ( updatedAccount . metadata . snap ?. enabled ) . toBe ( true ) ;
467
+ expect ( mockStateChange ) . toHaveBeenCalled ( ) ;
468
+
469
+ // Second update is the same, thus the account does not need any update, and SHOULD NOT trigger a `AccountsController:stateChange`.
470
+ mockStateChange . mockReset ( ) ;
471
+ messenger . publish ( 'SnapController:stateChange' , mockSnapChangeState , [ ] ) ;
472
+ expect ( updatedAccount . metadata . snap ?. enabled ) . toBe ( true ) ;
473
+ expect ( mockStateChange ) . not . toHaveBeenCalled ( ) ;
474
+ } ) ;
419
475
} ) ;
420
476
421
477
describe ( 'onKeyringStateChange' , ( ) => {
@@ -901,6 +957,7 @@ describe('AccountsController', () => {
901
957
name : 'Account 3' ,
902
958
address : mockAccount3 . address ,
903
959
keyringType : KeyringTypes . hd ,
960
+ options : { } ,
904
961
} ) ,
905
962
] ) ;
906
963
} ) ;
@@ -1744,6 +1801,12 @@ describe('AccountsController', () => {
1744
1801
keyrings : [
1745
1802
{ type : KeyringTypes . hd , accounts : [ mockAddress1 , mockAddress2 ] } ,
1746
1803
] ,
1804
+ keyringsMetadata : [
1805
+ {
1806
+ id : 'mock-keyring-id-0' ,
1807
+ name : 'mock-keyring-id-name' ,
1808
+ } ,
1809
+ ] ,
1747
1810
} ) ,
1748
1811
) ;
1749
1812
@@ -1772,12 +1835,14 @@ describe('AccountsController', () => {
1772
1835
id : 'mock-id' ,
1773
1836
address : mockAddress1 ,
1774
1837
keyringType : KeyringTypes . hd ,
1838
+ options : createMockInternalAccountOptions ( 0 , KeyringTypes . hd , 0 ) ,
1775
1839
} ) ,
1776
1840
createExpectedInternalAccount ( {
1777
1841
name : 'Account 2' ,
1778
1842
id : 'mock-id2' ,
1779
1843
address : mockAddress2 ,
1780
1844
keyringType : KeyringTypes . hd ,
1845
+ options : createMockInternalAccountOptions ( 0 , KeyringTypes . hd , 1 ) ,
1781
1846
} ) ,
1782
1847
] ;
1783
1848
mockUUIDWithNormalAccounts ( expectedAccounts ) ;
@@ -1800,6 +1865,12 @@ describe('AccountsController', () => {
1800
1865
accounts : [ mockSnapAccount , mockSnapAccount2 ] ,
1801
1866
} ,
1802
1867
] ,
1868
+ keyringsMetadata : [
1869
+ {
1870
+ id : 'mock-keyring-id-1' ,
1871
+ name : 'mock-keyring-id-name' ,
1872
+ } ,
1873
+ ] ,
1803
1874
} ) ,
1804
1875
) ;
1805
1876
@@ -1893,6 +1964,12 @@ describe('AccountsController', () => {
1893
1964
keyrings : [
1894
1965
{ type : KeyringTypes . hd , accounts : [ mockAddress1 , mockAddress2 ] } ,
1895
1966
] ,
1967
+ keyringsMetadata : [
1968
+ {
1969
+ id : 'mock-keyring-id-0' ,
1970
+ name : 'mock-keyring-id-name' ,
1971
+ } ,
1972
+ ] ,
1896
1973
} ) ,
1897
1974
) ;
1898
1975
@@ -1918,12 +1995,16 @@ describe('AccountsController', () => {
1918
1995
messenger,
1919
1996
} ) ;
1920
1997
const expectedAccounts = [
1921
- mockAccount ,
1998
+ {
1999
+ ...mockAccount ,
2000
+ options : createMockInternalAccountOptions ( 0 , KeyringTypes . hd , 0 ) ,
2001
+ } ,
1922
2002
createExpectedInternalAccount ( {
1923
2003
name : 'Account 2' ,
1924
2004
id : 'mock-id2' ,
1925
2005
address : mockAddress2 ,
1926
2006
keyringType : KeyringTypes . hd ,
2007
+ options : createMockInternalAccountOptions ( 0 , KeyringTypes . hd , 1 ) ,
1927
2008
} ) ,
1928
2009
] ;
1929
2010
mockUUIDWithNormalAccounts ( expectedAccounts ) ;
@@ -1957,6 +2038,16 @@ describe('AccountsController', () => {
1957
2038
{ type : KeyringTypes . hd , accounts : [ mockAddress1 ] } ,
1958
2039
{ type : KeyringTypes . snap , accounts : [ '0x1234' ] } ,
1959
2040
] ,
2041
+ keyringsMetadata : [
2042
+ {
2043
+ id : 'mock-keyring-id-0' ,
2044
+ name : 'mock-keyring-id-name' ,
2045
+ } ,
2046
+ {
2047
+ id : 'mock-keyring-id-1' ,
2048
+ name : 'mock-keyring-id-name2' ,
2049
+ } ,
2050
+ ] ,
1960
2051
} ) ,
1961
2052
) ;
1962
2053
@@ -1975,6 +2066,7 @@ describe('AccountsController', () => {
1975
2066
id : 'mock-id' ,
1976
2067
address : mockAddress1 ,
1977
2068
keyringType : KeyringTypes . hd ,
2069
+ options : createMockInternalAccountOptions ( 0 , KeyringTypes . hd , 0 ) ,
1978
2070
} ) ,
1979
2071
createExpectedInternalAccount ( {
1980
2072
name : 'Snap Account 1' , // it is Snap Account 1 because it is the only snap account
@@ -2014,6 +2106,16 @@ describe('AccountsController', () => {
2014
2106
{ type : KeyringTypes . snap , accounts : [ '0x1234' ] } ,
2015
2107
{ type : KeyringTypes . hd , accounts : [ mockAddress1 ] } ,
2016
2108
] ,
2109
+ keyringsMetadata : [
2110
+ {
2111
+ id : 'mock-keyring-id-0' ,
2112
+ name : 'mock-keyring-id-name' ,
2113
+ } ,
2114
+ {
2115
+ id : 'mock-keyring-id-1' ,
2116
+ name : 'mock-keyring-id-name2' ,
2117
+ } ,
2118
+ ] ,
2017
2119
} ) ,
2018
2120
) ;
2019
2121
@@ -2032,6 +2134,7 @@ describe('AccountsController', () => {
2032
2134
id : 'mock-id' ,
2033
2135
address : mockAddress1 ,
2034
2136
keyringType : KeyringTypes . hd ,
2137
+ options : createMockInternalAccountOptions ( 1 , KeyringTypes . hd , 0 ) ,
2035
2138
} ) ,
2036
2139
createExpectedInternalAccount ( {
2037
2140
name : 'Snap Account 1' , // it is Snap Account 1 because it is the only snap account
@@ -2057,7 +2160,6 @@ describe('AccountsController', () => {
2057
2160
KeyringTypes . ledger ,
2058
2161
KeyringTypes . lattice ,
2059
2162
KeyringTypes . qr ,
2060
- 'Custody - JSON - RPC' ,
2061
2163
] ) ( 'should add accounts for %s type' , async ( keyringType ) => {
2062
2164
mockUUIDWithNormalAccounts ( [ mockAccount ] ) ;
2063
2165
@@ -2067,6 +2169,12 @@ describe('AccountsController', () => {
2067
2169
'KeyringController:getState' ,
2068
2170
mockGetState . mockReturnValue ( {
2069
2171
keyrings : [ { type : keyringType , accounts : [ mockAddress1 ] } ] ,
2172
+ keyringsMetadata : [
2173
+ {
2174
+ id : 'mock-keyring-id-0' ,
2175
+ name : 'mock-keyring-id-name' ,
2176
+ } ,
2177
+ ] ,
2070
2178
} ) ,
2071
2179
) ;
2072
2180
@@ -2096,6 +2204,7 @@ describe('AccountsController', () => {
2096
2204
id : 'mock-id' ,
2097
2205
address : mockAddress1 ,
2098
2206
keyringType : keyringType as KeyringTypes ,
2207
+ options : createMockInternalAccountOptions ( 0 , keyringType , 0 ) ,
2099
2208
} ) ,
2100
2209
] ;
2101
2210
@@ -2206,6 +2315,16 @@ describe('AccountsController', () => {
2206
2315
{ type : KeyringTypes . snap , accounts : [ '0x1234' ] } ,
2207
2316
{ type : KeyringTypes . hd , accounts : [ mockAddress1 ] } ,
2208
2317
] ,
2318
+ keyringsMetadata : [
2319
+ {
2320
+ id : 'mock-keyring-id-1' ,
2321
+ name : 'mock-keyring-id-name' ,
2322
+ } ,
2323
+ {
2324
+ id : 'mock-keyring-id-2' ,
2325
+ name : 'mock-keyring-id-name2' ,
2326
+ } ,
2327
+ ] ,
2209
2328
} ) ,
2210
2329
) ;
2211
2330
0 commit comments