Skip to content

Commit 3b740fe

Browse files
VSCODE-113: Add tests for the new connection event (#87)
* test: add tests for the new connection event * test: mock sendTelemetry * test: use done * test: mock connect * test: check connecting types * test: always pass telemetry to controllers * test: disable metrics for all tests by default
1 parent bf98239 commit 3b740fe

17 files changed

+636
-418
lines changed

src/connectionController.ts

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,15 @@ export default class ConnectionController {
6565

6666
private _statusView: StatusView;
6767
private _storageController: StorageController;
68-
public _telemetryController?: TelemetryController;
68+
private _telemetryController: TelemetryController;
6969

7070
// Used by other parts of the extension that respond to changes in the connections.
7171
private eventEmitter: EventEmitter = new EventEmitter();
7272

7373
constructor(
7474
_statusView: StatusView,
7575
storageController: StorageController,
76-
telemetryController?: TelemetryController
76+
telemetryController: TelemetryController
7777
) {
7878
this._statusView = _statusView;
7979
this._storageController = storageController;
@@ -102,6 +102,7 @@ export default class ConnectionController {
102102
}
103103

104104
let loadedSavedConnection: LoadedConnection;
105+
105106
try {
106107
const unparsedConnectionInformation = await this._keytar.getPassword(
107108
this._serviceName,
@@ -134,6 +135,7 @@ export default class ConnectionController {
134135
// connections have become corrupted.
135136
return Promise.resolve();
136137
}
138+
137139
this._connections[connectionId] = loadedSavedConnection;
138140
this.eventEmitter.emit(DataServiceEventTypes.CONNECTIONS_DID_CHANGE);
139141

@@ -167,6 +169,7 @@ export default class ConnectionController {
167169
StorageVariables.WORKSPACE_SAVED_CONNECTIONS,
168170
StorageScope.WORKSPACE
169171
) || {};
172+
170173
if (Object.keys(existingWorkspaceConnections).length > 0) {
171174
// Try to pull in the connections previously saved on the workspace.
172175
await Promise.all(
@@ -181,9 +184,10 @@ export default class ConnectionController {
181184
};
182185

183186
public async connectWithURI(): Promise<boolean> {
187+
let connectionString: any;
188+
184189
log.info('connectWithURI command called');
185190

186-
let connectionString;
187191
try {
188192
connectionString = await vscode.window.showInputBox({
189193
value: '',
@@ -268,7 +272,6 @@ export default class ConnectionController {
268272
const { driverUrl, instanceId } = connectionModel.getAttributes({
269273
derived: true
270274
});
271-
272275
const connectionId = uuidv4();
273276
const connectionInformation: SavedConnectionInformation = {
274277
connectionModel,
@@ -285,6 +288,7 @@ export default class ConnectionController {
285288
...savedConnection,
286289
...connectionInformation
287290
};
291+
288292
this._connections[connectionId] = newLoadedConnection;
289293

290294
if (this._keytar) {
@@ -312,7 +316,7 @@ export default class ConnectionController {
312316
});
313317
};
314318

315-
public async getCloudInfoFromDataService(firstServerHostname) {
319+
public async getCloudInfoFromDataService(firstServerHostname: string) {
316320
const cloudInfo = await getCloudInfo(firstServerHostname);
317321
let isPublicCloud = false;
318322
let publicCloudName: string | null = null;
@@ -339,6 +343,7 @@ export default class ConnectionController {
339343
if (error) {
340344
log.error('TELEMETRY data service error', error);
341345
}
346+
342347
if (data) {
343348
try {
344349
const firstServerHostname = dataService.client.model.hosts[0].host;
@@ -369,7 +374,7 @@ export default class ConnectionController {
369374
};
370375

371376
// Send metrics to Segment
372-
this._telemetryController?.track(
377+
this._telemetryController.track(
373378
TelemetryEventTypes.NEW_CONNECTION,
374379
telemetryData
375380
);
@@ -396,7 +401,9 @@ export default class ConnectionController {
396401
return Promise.reject(
397402
new Error('Unable to connect: already connecting.')
398403
);
399-
} else if (this._disconnecting) {
404+
}
405+
406+
if (this._disconnecting) {
400407
return Promise.reject(
401408
new Error('Unable to connect: currently disconnecting.')
402409
);
@@ -427,6 +434,7 @@ export default class ConnectionController {
427434
this._connecting = false;
428435
log.info('Failed to connect');
429436
this.eventEmitter.emit(DataServiceEventTypes.CONNECTIONS_DID_CHANGE);
437+
430438
return reject(new Error(`Failed to connect: ${err.message}`));
431439
}
432440

@@ -441,7 +449,7 @@ export default class ConnectionController {
441449
this.eventEmitter.emit(DataServiceEventTypes.CONNECTIONS_DID_CHANGE);
442450
this.eventEmitter.emit(DataServiceEventTypes.ACTIVE_CONNECTION_CHANGED);
443451

444-
if (this._telemetryController) {
452+
if (this._telemetryController.needTelemetry()) {
445453
this.sendTelemetry(newDataService, connectionType);
446454
}
447455

@@ -452,11 +460,12 @@ export default class ConnectionController {
452460

453461
public connectWithConnectionId = (connectionId: string): Promise<boolean> => {
454462
if (this._connections[connectionId]) {
455-
let connectionModel;
463+
let connectionModel: any;
456464

457465
try {
458466
const savedConnectionModel = this._connections[connectionId]
459467
.connectionModel;
468+
460469
// Here we rebuild the connection model to ensure it's up to date and
461470
// contains the connection model class methods (not just attributes).
462471
connectionModel = new Connection(
@@ -466,15 +475,18 @@ export default class ConnectionController {
466475
);
467476
} catch (error) {
468477
vscode.window.showErrorMessage(`Unable to load connection: ${error}`);
478+
469479
return Promise.resolve(false);
470480
}
481+
471482
return new Promise((resolve) => {
472483
this.connect(
473484
connectionId,
474485
connectionModel,
475486
ConnectionTypes.CONNECTION_ID
476487
).then(resolve, (err: Error) => {
477488
vscode.window.showErrorMessage(err.message);
489+
478490
return resolve(false);
479491
});
480492
});
@@ -493,6 +505,7 @@ export default class ConnectionController {
493505
vscode.window.showErrorMessage(
494506
'Unable to disconnect: already disconnecting from an instance.'
495507
);
508+
496509
return Promise.resolve(false);
497510
}
498511

@@ -501,6 +514,7 @@ export default class ConnectionController {
501514
vscode.window.showErrorMessage(
502515
'Unable to disconnect: currently connecting to an instance.'
503516
);
517+
504518
return Promise.resolve(false);
505519
}
506520

@@ -510,6 +524,7 @@ export default class ConnectionController {
510524
vscode.window.showErrorMessage(
511525
'Unable to disconnect: no active connection.'
512526
);
527+
513528
return resolve(false);
514529
}
515530

@@ -544,6 +559,7 @@ export default class ConnectionController {
544559
connectionId: string
545560
): Promise<void> => {
546561
delete this._connections[connectionId];
562+
547563
if (this._keytar) {
548564
await this._keytar.deletePassword(this._serviceName, connectionId);
549565
// We only remove the connection from the saved connections if we
@@ -563,19 +579,23 @@ export default class ConnectionController {
563579
vscode.window.showErrorMessage(
564580
'Unable to remove connection: currently connecting.'
565581
);
582+
566583
return Promise.resolve(false);
567584
}
585+
568586
// Ensure we aren't currently disconnecting.
569587
if (this._disconnecting) {
570588
vscode.window.showErrorMessage(
571589
'Unable to remove connection: currently disconnecting.'
572590
);
591+
573592
return Promise.resolve(false);
574593
}
575594

576595
if (!this._connections[connectionId]) {
577596
// No active connection(s) to remove.
578597
vscode.window.showErrorMessage('Connection does not exist.');
598+
579599
return Promise.resolve(false);
580600
}
581601

@@ -596,6 +616,7 @@ export default class ConnectionController {
596616
await this.removeSavedConnection(connectionId);
597617

598618
vscode.window.showInformationMessage('MongoDB connection removed.');
619+
599620
return Promise.resolve(true);
600621
}
601622

@@ -607,13 +628,16 @@ export default class ConnectionController {
607628
vscode.window.showErrorMessage(
608629
'Unable to remove connection: currently connecting.'
609630
);
631+
610632
return Promise.resolve(false);
611633
}
634+
612635
// Ensure we aren't currently disconnecting.
613636
if (this._disconnecting) {
614637
vscode.window.showErrorMessage(
615638
'Unable to remove connection: currently disconnecting.'
616639
);
640+
617641
return Promise.resolve(false);
618642
}
619643

@@ -622,6 +646,7 @@ export default class ConnectionController {
622646
if (connectionIds.length === 0) {
623647
// No active connection(s) to remove.
624648
vscode.window.showErrorMessage('No connections to remove.');
649+
625650
return Promise.resolve(false);
626651
}
627652

@@ -655,7 +680,8 @@ export default class ConnectionController {
655680
}
656681

657682
public async renameConnection(connectionId: string): Promise<boolean> {
658-
let inputtedConnectionName;
683+
let inputtedConnectionName: any;
684+
659685
try {
660686
inputtedConnectionName = await vscode.window.showInputBox({
661687
value: this._connections[connectionId].name,
@@ -694,7 +720,9 @@ export default class ConnectionController {
694720
return this._storageController
695721
.saveConnectionToGlobalStore(this._connections[connectionId])
696722
.then(() => resolve(true), reject);
697-
} else if (
723+
}
724+
725+
if (
698726
this._connections[connectionId].storageLocation ===
699727
StorageScope.WORKSPACE
700728
) {
@@ -806,15 +834,19 @@ export default class ConnectionController {
806834
this._disconnecting = false;
807835
this._connectingConnectionId = '';
808836
}
837+
809838
public setActiveConnection(newActiveConnection: any): void {
810839
this._activeDataService = newActiveConnection;
811840
}
841+
812842
public setConnnecting(connecting: boolean): void {
813843
this._connecting = connecting;
814844
}
845+
815846
public setConnnectingConnectionId(connectingConnectionId: string): void {
816847
this._connectingConnectionId = connectingConnectionId;
817848
}
849+
818850
public setDisconnecting(disconnecting: boolean): void {
819851
this._disconnecting = disconnecting;
820852
}

src/editors/playgroundController.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export default class PlaygroundController {
2222
_context: vscode.ExtensionContext;
2323
_connectionController: ConnectionController;
2424
_languageServerController: LanguageServerController;
25-
_telemetryController?: TelemetryController;
25+
_telemetryController: TelemetryController;
2626
_activeConnectionCodeLensProvider?: ActiveConnectionCodeLensProvider;
2727
_outputChannel: OutputChannel;
2828
_connectionString?: string;
@@ -33,7 +33,7 @@ export default class PlaygroundController {
3333
context: vscode.ExtensionContext,
3434
connectionController: ConnectionController,
3535
languageServerController: LanguageServerController,
36-
telemetryController?: TelemetryController
36+
telemetryController: TelemetryController
3737
) {
3838
this._context = context;
3939
this._connectionController = connectionController;
@@ -168,9 +168,9 @@ export default class PlaygroundController {
168168
codeToEvaluate
169169
);
170170

171-
if (result) {
171+
if (result && this._telemetryController.needTelemetry()) {
172172
// Send metrics to Segment
173-
this._telemetryController?.track(
173+
this._telemetryController.track(
174174
TelemetryEventTypes.PLAYGROUND_CODE_EXECUTED,
175175
this.prepareTelemetry(result)
176176
);

src/mdbExtensionController.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ export default class MDBExtensionController implements vscode.Disposable {
137137
): void => {
138138
const commandHandlerWithTelemetry = (args: any[]) => {
139139
// Send metrics to Segment
140-
this._telemetryController?.track(
140+
this._telemetryController.track(
141141
TelemetryEventTypes.EXTENSION_COMMAND_RUN,
142142
{
143143
command

src/telemetry/telemetryController.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -110,21 +110,21 @@ export default class TelemetryController {
110110
this._segmentAnalytics?.flush();
111111
}
112112

113+
public needTelemetry() {
114+
return vscode.workspace.getConfiguration('mdb').get('sendTelemetry');
115+
}
116+
113117
public track(
114118
eventType: TelemetryEventTypes,
115119
properties: TelemetryEventProperties
116120
): void {
117-
const shouldSendTelemetry = vscode.workspace
118-
.getConfiguration('mdb')
119-
.get('sendTelemetry');
120-
121-
log.info('TELEMETRY track', {
122-
eventType,
123-
segmentUserID: this._segmentUserID,
124-
properties
125-
});
121+
if (this.needTelemetry()) {
122+
log.info('TELEMETRY track', {
123+
eventType,
124+
segmentUserID: this._segmentUserID,
125+
properties
126+
});
126127

127-
if (shouldSendTelemetry) {
128128
this._segmentAnalytics?.track(
129129
{
130130
userId: this._segmentUserID,

0 commit comments

Comments
 (0)