@@ -41,6 +41,7 @@ import MockEncryptor, {
41
41
} from '../tests/mocks/mockEncryptor' ;
42
42
import { MockErc4337Keyring } from '../tests/mocks/mockErc4337Keyring' ;
43
43
import { MockKeyring } from '../tests/mocks/mockKeyring' ;
44
+ import { MockMutableKeyring } from '../tests/mocks/mockMutableKeyring' ;
44
45
import MockShallowGetAccountsKeyring from '../tests/mocks/mockShallowGetAccountsKeyring' ;
45
46
import { buildMockTransaction } from '../tests/mocks/mockTransaction' ;
46
47
@@ -3080,6 +3081,71 @@ describe('KeyringController', () => {
3080
3081
} ,
3081
3082
) ;
3082
3083
} ) ;
3084
+
3085
+ it ( 'should update the vault if the keyring is being updated' , async ( ) => {
3086
+ await withController (
3087
+ { keyringBuilders : [ keyringBuilderFactory ( MockMutableKeyring ) ] } ,
3088
+ async ( { controller, messenger } ) => {
3089
+ const selector = { type : MockMutableKeyring . type } ;
3090
+
3091
+ const mockStateChange = jest . fn ( ) ;
3092
+ messenger . subscribe (
3093
+ 'KeyringController:stateChange' ,
3094
+ mockStateChange ,
3095
+ ) ;
3096
+
3097
+ await controller . withKeyring (
3098
+ selector ,
3099
+ async ( { keyring } ) => {
3100
+ ( keyring as MockMutableKeyring ) . update ( ) ; // Update the keyring state.
3101
+ } ,
3102
+ {
3103
+ createIfMissing : true ,
3104
+ } ,
3105
+ ) ;
3106
+
3107
+ expect ( mockStateChange ) . toHaveBeenCalled ( ) ;
3108
+ expect ( controller . state . keyrings ) . toHaveLength ( 2 ) ;
3109
+ } ,
3110
+ ) ;
3111
+ } ) ;
3112
+
3113
+ it ( 'should not update the vault if the keyring has not been updated' , async ( ) => {
3114
+ await withController (
3115
+ { keyringBuilders : [ keyringBuilderFactory ( MockMutableKeyring ) ] } ,
3116
+ async ( { controller, messenger } ) => {
3117
+ const selector = { type : MockMutableKeyring . type } ;
3118
+
3119
+ const mockStateChange = jest . fn ( ) ;
3120
+ messenger . subscribe (
3121
+ 'KeyringController:stateChange' ,
3122
+ mockStateChange ,
3123
+ ) ;
3124
+
3125
+ // First create the reading before reading from it.
3126
+ await controller . withKeyring (
3127
+ selector ,
3128
+ async ( ) => {
3129
+ // No-op, but the keyring will still be persisted since we
3130
+ // used `createIfMissing`.
3131
+ } ,
3132
+ {
3133
+ createIfMissing : true ,
3134
+ } ,
3135
+ ) ;
3136
+
3137
+ expect ( mockStateChange ) . toHaveBeenCalled ( ) ;
3138
+ expect ( controller . state . keyrings ) . toHaveLength ( 2 ) ;
3139
+
3140
+ mockStateChange . mockReset ( ) ;
3141
+ await controller . withKeyring ( selector , async ( ) => {
3142
+ // No-op, keyring state won't be updated.
3143
+ } ) ;
3144
+
3145
+ expect ( mockStateChange ) . not . toHaveBeenCalled ( ) ;
3146
+ } ,
3147
+ ) ;
3148
+ } ) ;
3083
3149
} ) ;
3084
3150
} ) ;
3085
3151
0 commit comments