@@ -9889,6 +9889,127 @@ describe('SnapController', () => {
98899889 } ) ;
98909890
98919891 describe ( 'SnapController actions' , ( ) => {
9892+ describe ( 'SnapController:init' , ( ) => {
9893+ it ( 'calls `onStart` for all Snaps with the `endowment:lifecycle-hooks` permission' , async ( ) => {
9894+ const rootMessenger = getControllerMessenger ( ) ;
9895+ const messenger = getSnapControllerMessenger ( rootMessenger ) ;
9896+
9897+ rootMessenger . registerActionHandler (
9898+ 'PermissionController:getPermissions' ,
9899+ ( origin ) => {
9900+ if ( origin === MOCK_SNAP_ID ) {
9901+ return {
9902+ [ SnapEndowments . LifecycleHooks ] :
9903+ MOCK_LIFECYCLE_HOOKS_PERMISSION ,
9904+ } ;
9905+ }
9906+
9907+ return { } ;
9908+ } ,
9909+ ) ;
9910+
9911+ rootMessenger . registerActionHandler (
9912+ 'PermissionController:hasPermission' ,
9913+ ( origin ) => {
9914+ return origin === MOCK_SNAP_ID ;
9915+ } ,
9916+ ) ;
9917+
9918+ const snapController = getSnapController (
9919+ getSnapControllerOptions ( {
9920+ messenger,
9921+ state : {
9922+ snaps : getPersistedSnapsState (
9923+ getPersistedSnapObject ( {
9924+ id : MOCK_SNAP_ID ,
9925+ } ) ,
9926+ getPersistedSnapObject ( {
9927+ id : MOCK_LOCAL_SNAP_ID ,
9928+ } ) ,
9929+ ) ,
9930+ } ,
9931+ } ) ,
9932+ ) ;
9933+
9934+ const call = jest . spyOn ( messenger , 'call' ) ;
9935+ messenger . call ( 'SnapController:init' ) ;
9936+ await sleep ( 10 ) ;
9937+
9938+ expect ( call ) . toHaveBeenNthCalledWith (
9939+ 2 ,
9940+ 'PermissionController:hasPermission' ,
9941+ MOCK_SNAP_ID ,
9942+ 'endowment:lifecycle-hooks' ,
9943+ ) ;
9944+
9945+ expect ( call ) . toHaveBeenNthCalledWith (
9946+ 6 ,
9947+ 'ExecutionService:executeSnap' ,
9948+ expect . any ( Object ) ,
9949+ ) ;
9950+
9951+ expect ( messenger . call ) . toHaveBeenNthCalledWith (
9952+ 7 ,
9953+ 'ExecutionService:handleRpcRequest' ,
9954+ MOCK_SNAP_ID ,
9955+ {
9956+ handler : HandlerType . OnStart ,
9957+ origin : METAMASK_ORIGIN ,
9958+ request : {
9959+ jsonrpc : '2.0' ,
9960+ id : expect . any ( String ) ,
9961+ method : HandlerType . OnStart ,
9962+ } ,
9963+ } ,
9964+ ) ;
9965+
9966+ snapController . destroy ( ) ;
9967+ } ) ;
9968+
9969+ it ( 'logs an error if the lifecycle hook throws' , async ( ) => {
9970+ const consoleErrorSpy = jest
9971+ . spyOn ( console , 'error' )
9972+ . mockImplementation ( ) ;
9973+
9974+ const rootMessenger = getControllerMessenger ( ) ;
9975+ const messenger = getSnapControllerMessenger ( rootMessenger ) ;
9976+
9977+ rootMessenger . registerActionHandler (
9978+ 'PermissionController:getPermissions' ,
9979+ ( ) => {
9980+ return {
9981+ [ SnapEndowments . LifecycleHooks ] : MOCK_LIFECYCLE_HOOKS_PERMISSION ,
9982+ } ;
9983+ } ,
9984+ ) ;
9985+
9986+ rootMessenger . registerActionHandler (
9987+ 'ExecutionService:handleRpcRequest' ,
9988+ ( ) => {
9989+ throw new Error ( 'Test error in lifecycle hook.' ) ;
9990+ } ,
9991+ ) ;
9992+
9993+ const snapController = getSnapController (
9994+ getSnapControllerOptions ( {
9995+ messenger,
9996+ state : {
9997+ snaps : getPersistedSnapsState ( ) ,
9998+ } ,
9999+ } ) ,
10000+ ) ;
10001+
10002+ messenger . call ( 'SnapController:init' ) ;
10003+ await sleep ( 10 ) ;
10004+
10005+ expect ( consoleErrorSpy ) . toHaveBeenCalledWith (
10006+ `Error when calling \`onStart\` lifecycle hook for Snap "npm:@metamask/example-snap": Test error in lifecycle hook.` ,
10007+ ) ;
10008+
10009+ snapController . destroy ( ) ;
10010+ } ) ;
10011+ } ) ;
10012+
989210013 describe ( 'SnapController:get' , ( ) => {
989310014 it ( 'gets a snap' , ( ) => {
989410015 const messenger = getSnapControllerMessenger ( ) ;
0 commit comments