@@ -98,7 +98,11 @@ const extensionConnector = {
9898 subscribers . push ( f )
9999 return ( ) => { }
100100 } ) ,
101- unsubscribe : vi . fn ( ) ,
101+ unsubscribe : vi . fn ( ( ) => {
102+ connectionMap . delete (
103+ areNameUndefinedMapsNeeded ? options . testConnectionId : key ,
104+ )
105+ } ) ,
102106 send : vi . fn ( ) ,
103107 init : vi . fn ( ) ,
104108 error : vi . fn ( ) ,
@@ -2448,3 +2452,40 @@ describe('when create devtools was called multiple times with `name` and `store`
24482452 } )
24492453 } )
24502454} )
2455+
2456+ describe ( 'cleanup' , ( ) => {
2457+ it ( 'should unsubscribe from devtools when cleanup is called' , async ( ) => {
2458+ const options = { name : 'test' }
2459+ const store = createStore ( devtools ( ( ) => ( { count : 0 } ) , options ) )
2460+ const [ connection ] = getNamedConnectionApis ( options . name )
2461+ store . devtools . cleanup ( )
2462+
2463+ expect ( connection . unsubscribe ) . toHaveBeenCalledTimes ( 1 )
2464+ } )
2465+
2466+ it ( 'should remove store from tracked connection after cleanup' , async ( ) => {
2467+ const options = {
2468+ name : 'test-store-name' ,
2469+ store : 'test-store-id' ,
2470+ enabled : true ,
2471+ }
2472+
2473+ const store1 = createStore ( devtools ( ( ) => ( { count : 0 } ) , options ) )
2474+ store1 . devtools . cleanup ( )
2475+ const store2 = createStore ( devtools ( ( ) => ( { count : 0 } ) , options ) )
2476+
2477+ const [ connection ] = getNamedConnectionApis ( options . name )
2478+
2479+ store2 . setState ( { count : 15 } , false , 'updateCount' )
2480+ expect ( connection . send ) . toHaveBeenLastCalledWith (
2481+ { type : `${ options . store } /updateCount` } ,
2482+ { [ options . store ] : { count : 15 } } ,
2483+ )
2484+
2485+ store1 . setState ( { count : 20 } , false , 'ignoredAction' )
2486+ expect ( connection . send ) . not . toHaveBeenLastCalledWith (
2487+ { type : `${ options . store } /ignoredAction` } ,
2488+ expect . anything ( ) ,
2489+ )
2490+ } )
2491+ } )
0 commit comments