1
1
import CompassShellStore from 'stores' ;
2
2
import { EventEmitter } from 'events' ;
3
- import { ElectronRuntime } from '@mongosh/browser-runtime-electron' ;
3
+ import { WorkerRuntime } from '../modules/worker-runtime' ;
4
+
5
+ function createMockDataService ( ) {
6
+ return {
7
+ getConnectionOptions ( ) {
8
+ return {
9
+ url : 'mongodb://nodb/' ,
10
+ options : { } ,
11
+ cliOptions : { nodb : true } ,
12
+ } ;
13
+ } ,
14
+ client : {
15
+ client : { } ,
16
+ } ,
17
+ } ;
18
+ }
4
19
5
20
describe ( 'CompassShellStore [Store]' , ( ) => {
6
21
let store ;
7
22
let appRegistry ;
8
23
24
+ const getRuntimeState = ( ) => store . reduxStore . getState ( ) . runtime ;
25
+
9
26
beforeEach ( ( ) => {
10
27
store = new CompassShellStore ( ) ;
11
28
appRegistry = new EventEmitter ( ) ;
12
29
store . onActivated ( appRegistry ) ;
13
30
} ) ;
14
31
32
+ afterEach ( async ( ) => {
33
+ const { runtime } = getRuntimeState ( ) ;
34
+
35
+ if ( runtime && runtime . terminate ) {
36
+ await runtime . terminate ( ) ;
37
+ }
38
+ } ) ;
39
+
15
40
describe ( 'appRegistry' , ( ) => {
16
41
it ( 'sets the global appRegistry' , ( ) => {
17
42
expect ( store . reduxStore . getState ( ) . appRegistry ) . to . not . equal ( null ) ;
@@ -20,8 +45,6 @@ describe('CompassShellStore [Store]', () => {
20
45
} ) ;
21
46
22
47
describe ( 'runtime' , ( ) => {
23
- const getRuntimeState = ( ) => store . reduxStore . getState ( ) . runtime ;
24
-
25
48
it ( 'has initialized runtime state' , ( ) => {
26
49
const runtimeState = getRuntimeState ( ) ;
27
50
@@ -30,36 +53,26 @@ describe('CompassShellStore [Store]', () => {
30
53
} ) ;
31
54
32
55
it ( 'sets runtime on data-service-connected' , ( ) => {
33
- appRegistry . emit ( 'data-service-connected' , null , { client : { client : { } } } ) ;
56
+ appRegistry . emit ( 'data-service-connected' , null , createMockDataService ( ) ) ;
34
57
35
58
const runtimeState = getRuntimeState ( ) ;
36
59
37
60
expect ( runtimeState . error ) . to . equal ( null ) ;
38
- expect ( runtimeState . runtime ) . to . be . instanceOf ( ElectronRuntime ) ;
61
+ expect ( runtimeState . runtime ) . to . be . instanceOf ( WorkerRuntime ) ;
39
62
} ) ;
40
63
41
- it ( 'emits mongosh events to the appRegistry' , ( ) => {
42
- appRegistry . emit ( 'data-service-connected' , null , { client : { client : {
43
- db : ( ) => ( {
44
- admin : ( ) => ( {
45
- listDatabases : ( ) => Promise . resolve ( {
46
- databases : [
47
- { name : 'db1' , sizeOnDisk : 10000 , empty : false }
48
- ] ,
49
- totalSize : 50000 ,
50
- ok : 1
51
- } )
52
- } )
53
- } )
54
- } } } ) ;
64
+ it ( 'emits mongosh events to the appRegistry' , async ( ) => {
65
+ appRegistry . emit ( 'data-service-connected' , null , createMockDataService ( ) ) ;
55
66
let eventRecieved = false ;
56
- appRegistry . on ( 'mongosh:show ' , ( ) => {
67
+ appRegistry . on ( 'mongosh:setCtx ' , ( ) => {
57
68
eventRecieved = true ;
58
69
} ) ;
59
70
60
71
const runtimeState = getRuntimeState ( ) ;
61
72
62
- runtimeState . runtime . evaluate ( 'show dbs;' ) ;
73
+ // Any command will do, just making sure we waited for the runtime to
74
+ // become available
75
+ await runtimeState . runtime . evaluate ( 'help' ) ;
63
76
64
77
expect ( eventRecieved ) . to . equal ( true ) ;
65
78
} ) ;
@@ -75,7 +88,7 @@ describe('CompassShellStore [Store]', () => {
75
88
} ) ;
76
89
77
90
it ( 'does not change state if dataService is the same' , ( ) => {
78
- const fakeDataService = { client : { client : { } } } ;
91
+ const fakeDataService = createMockDataService ( ) ;
79
92
80
93
appRegistry . emit ( 'data-service-connected' , null , fakeDataService ) ;
81
94
const runtimeState1 = getRuntimeState ( ) ;
0 commit comments