@@ -8,17 +8,23 @@ import { CliServiceProvider } from '@mongosh/service-provider-server';
8
8
import { bson } from '@mongosh/service-provider-core' ;
9
9
import { ElectronRuntime } from './electron-runtime' ;
10
10
import { EventEmitter } from 'events' ;
11
+ import { EvaluationListener } from '@mongosh/shell-evaluator' ;
11
12
12
13
describe ( 'Electron runtime' , function ( ) {
13
14
let serviceProvider : SinonStubbedInstance < CliServiceProvider > ;
14
15
let messageBus : SinonStubbedInstance < EventEmitter > ;
16
+ let evaluationListener : SinonStubbedInstance < EvaluationListener > ;
15
17
let electronRuntime : ElectronRuntime ;
16
18
17
19
beforeEach ( async ( ) => {
18
20
serviceProvider = sinon . createStubInstance ( CliServiceProvider ) ;
19
21
serviceProvider . bsonLibrary = bson ;
22
+ serviceProvider . getConnectionInfo . resolves ( { extraInfo : { uri : '' } } ) ;
20
23
messageBus = sinon . createStubInstance ( EventEmitter ) ;
24
+ evaluationListener = sinon . createStubInstance ( class FakeListener { } ) ;
25
+ evaluationListener . onPrint = sinon . stub ( ) ;
21
26
electronRuntime = new ElectronRuntime ( serviceProvider , messageBus ) ;
27
+ electronRuntime . setEvaluationListener ( evaluationListener ) ;
22
28
} ) ;
23
29
24
30
it ( 'can evaluate simple js' , async ( ) => {
@@ -79,4 +85,36 @@ describe('Electron runtime', function() {
79
85
await electronRuntime . evaluate ( 'use db1' ) ;
80
86
expect ( messageBus . emit ) . to . have . been . calledWith ( 'mongosh:use' ) ;
81
87
} ) ;
88
+
89
+ describe ( 'onPrint' , ( ) => {
90
+ it ( 'allows getting the output of print() statements' , async ( ) => {
91
+ await electronRuntime . evaluate ( 'print("foo");' ) ;
92
+ expect ( evaluationListener . onPrint ) . to . have . been . calledWithMatch (
93
+ sinon . match ( ( array ) => (
94
+ array . length === 1 &&
95
+ array [ 0 ] . type === null &&
96
+ array [ 0 ] . printable === 'foo' ) ) ) ;
97
+ } ) ;
98
+
99
+ it ( 'allows getting the output of console.log() statements' , async ( ) => {
100
+ await electronRuntime . evaluate ( 'console.log("foo");' ) ;
101
+ expect ( evaluationListener . onPrint ) . to . have . been . calledWithMatch (
102
+ sinon . match ( ( array ) => (
103
+ array . length === 1 &&
104
+ array [ 0 ] . type === null &&
105
+ array [ 0 ] . printable === 'foo' ) ) ) ;
106
+ } ) ;
107
+
108
+ it ( 'allows getting the output of multi-arg console.log() statements' , async ( ) => {
109
+ await electronRuntime . evaluate ( 'console.log("foo", "bar");' ) ;
110
+ expect ( evaluationListener . onPrint ) . to . have . been . calledWithMatch (
111
+ sinon . match ( ( array ) => (
112
+ array . length === 2 &&
113
+ array [ 0 ] . type === null &&
114
+ array [ 0 ] . printable === 'foo' &&
115
+ array [ 1 ] . type === null &&
116
+ array [ 1 ] . printable === 'bar' ) ) ) ;
117
+ expect ( evaluationListener . onPrint ) . to . have . been . calledOnce ;
118
+ } ) ;
119
+ } ) ;
82
120
} ) ;
0 commit comments