@@ -121,6 +121,9 @@ describe('InstanceStore [Store]', function () {
121121 let connectedInstance : MongoDBInstance ;
122122 let initialInstanceRefreshedPromise : Promise < unknown > ;
123123 beforeEach ( function ( ) {
124+ sinon
125+ . stub ( connectionsManager , 'getDataServiceForConnection' )
126+ . returns ( dataService ) ;
124127 connectionsManager . emit (
125128 ConnectionsManagerEvents . ConnectionAttemptSuccessful ,
126129 connectedConnectionInfoId ,
@@ -330,35 +333,35 @@ describe('InstanceStore [Store]', function () {
330333 } ) ;
331334 } ) ;
332335
333- const createdEvents = [ 'agg-pipeline-out-executed' ] ;
334-
335- for ( const evt of createdEvents ) {
336- context ( `on '${ evt } ' event` , function ( ) {
337- it ( 'should add collection to the databases collections' , function ( ) {
338- globalAppRegistry . emit ( evt , 'foo.qux' ) ;
339- expect (
340- connectedInstance . databases . get ( 'foo' ) ?. collections
341- ) . to . have . lengthOf ( 3 ) ;
342- expect (
343- connectedInstance . databases
344- . get ( 'foo' )
345- ?. collections . get ( 'foo.qux' , '_id' )
346- ) . to . exist ;
336+ context ( `on 'agg-pipeline-out-executed' event` , function ( ) {
337+ it ( 'should add collection to the databases collections' , function ( ) {
338+ globalAppRegistry . emit ( 'agg-pipeline-out-executed' , 'foo.qux' , {
339+ connectionId : connectedConnectionInfoId ,
347340 } ) ;
341+ expect (
342+ connectedInstance . databases . get ( 'foo' ) ?. collections
343+ ) . to . have . lengthOf ( 3 ) ;
344+ expect (
345+ connectedInstance . databases
346+ . get ( 'foo' )
347+ ?. collections . get ( 'foo.qux' , '_id' )
348+ ) . to . exist ;
349+ } ) ;
348350
349- it ( "should add new database and add collection to its collections if database doesn't exist yet" , function ( ) {
350- globalAppRegistry . emit ( evt , 'bar.qux' ) ;
351- expect ( connectedInstance . databases ) . to . have . lengthOf ( 2 ) ;
352- expect ( connectedInstance . databases . get ( 'bar' ) ) . to . exist ;
353- expect (
354- connectedInstance . databases . get ( 'bar' ) ?. collections
355- ) . to . have . lengthOf ( 1 ) ;
356- expect (
357- connectedInstance . databases . get ( 'bar' ) ?. collections . get ( 'bar.qux' )
358- ) . to . exist ;
351+ it ( "should add new database and add collection to its collections if database doesn't exist yet" , function ( ) {
352+ globalAppRegistry . emit ( 'agg-pipeline-out-executed' , 'bar.qux' , {
353+ connectionId : connectedConnectionInfoId ,
359354 } ) ;
355+ expect ( connectedInstance . databases ) . to . have . lengthOf ( 2 ) ;
356+ expect ( connectedInstance . databases . get ( 'bar' ) ) . to . exist ;
357+ expect (
358+ connectedInstance . databases . get ( 'bar' ) ?. collections
359+ ) . to . have . lengthOf ( 1 ) ;
360+ expect (
361+ connectedInstance . databases . get ( 'bar' ) ?. collections . get ( 'bar.qux' )
362+ ) . to . exist ;
360363 } ) ;
361- }
364+ } ) ;
362365
363366 context ( `on 'collection-created' event` , function ( ) {
364367 it ( 'should not respond when the connectionId does not matches the connectionId for the instance' , function ( ) {
@@ -469,4 +472,68 @@ describe('InstanceStore [Store]', function () {
469472 } ) ;
470473 } ) ;
471474 } ) ;
475+
476+ context ( 'when disconnected' , function ( ) {
477+ it ( 'should remove the instance from InstancesManager and should not perform any actions on the stale instance' , async function ( ) {
478+ // first connect
479+ connectionsManager . emit (
480+ ConnectionsManagerEvents . ConnectionAttemptSuccessful ,
481+ connectedConnectionInfoId ,
482+ dataService
483+ ) ;
484+
485+ // setup a spy on old instance
486+ const oldInstance = instancesManager . getMongoDBInstanceForConnection (
487+ connectedConnectionInfoId
488+ ) ;
489+ const oldFetchDatabasesSpy = sinon . spy ( oldInstance , 'fetchDatabases' ) ;
490+
491+ // now disconnect
492+ connectionsManager . emit (
493+ ConnectionsManagerEvents . ConnectionDisconnected ,
494+ connectedConnectionInfoId
495+ ) ;
496+
497+ // there is no instance in store InstancesManager now
498+ expect ( ( ) =>
499+ instancesManager . getMongoDBInstanceForConnection (
500+ connectedConnectionInfoId
501+ )
502+ ) . to . throw ;
503+
504+ // lets connect again and ensure that old instance does not receive events anymore
505+ const newDataService = createDataService ( ) ;
506+ sinon
507+ . stub ( connectionsManager , 'getDataServiceForConnection' )
508+ . returns ( dataService ) ;
509+ connectionsManager . emit (
510+ ConnectionsManagerEvents . ConnectionAttemptSuccessful ,
511+ connectedConnectionInfoId ,
512+ newDataService
513+ ) ;
514+
515+ // setup a spy on new instance
516+ const newInstance = instancesManager . getMongoDBInstanceForConnection (
517+ connectedConnectionInfoId
518+ ) ;
519+
520+ let resolveFetchDatabasePromise : ( ( ) => void ) | undefined ;
521+ const fetchDatabasePromise = new Promise < void > ( ( resolve ) => {
522+ resolveFetchDatabasePromise = resolve ;
523+ } ) ;
524+ const newFetchDatabasesSpy = sinon
525+ . stub ( newInstance , 'fetchDatabases' )
526+ . callsFake ( ( ) => {
527+ resolveFetchDatabasePromise ?.( ) ;
528+ return Promise . resolve ( ) ;
529+ } ) ;
530+
531+ globalAppRegistry . emit ( 'refresh-databases' , {
532+ connectionId : connectedConnectionInfoId ,
533+ } ) ;
534+ await fetchDatabasePromise ;
535+ expect ( oldFetchDatabasesSpy ) . to . not . be . called ;
536+ expect ( newFetchDatabasesSpy ) . to . be . called ;
537+ } ) ;
538+ } ) ;
472539} ) ;
0 commit comments