Skip to content

Commit b5ed64c

Browse files
authored
VSCODE-85: Add default button to tree view (#59)
1 parent a6644bc commit b5ed64c

8 files changed

+105
-25
lines changed

package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@
9191
}
9292
]
9393
},
94+
"viewsWelcome": [
95+
{
96+
"view": "mongoDB",
97+
"contents": "No connections found.\n[Add Connection](command:mdb.connect)"
98+
}
99+
],
94100
"languages": [
95101
{
96102
"id": "mongodb",
@@ -249,6 +255,10 @@
249255
],
250256
"menus": {
251257
"view/title": [
258+
{
259+
"command": "mdb.createPlayground",
260+
"when": "view == mongoDB"
261+
},
252262
{
253263
"command": "mdb.addConnection",
254264
"when": "view == mongoDB"

src/connectionController.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const MAX_CONNECTION_NAME_LENGTH = 512;
1818

1919
export enum DataServiceEventTypes {
2020
CONNECTIONS_DID_CHANGE = 'CONNECTIONS_DID_CHANGE',
21-
ACTIVE_CONNECTION_CHANGED = 'ACTIVE_CONNECTION_CHANGED'
21+
ACTIVE_CONNECTION_CHANGED = 'ACTIVE_CONNECTION_CHANGED',
2222
}
2323

2424
export default class ConnectionController {

src/explorer/explorerController.ts

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,49 @@
11
import * as vscode from 'vscode';
22

3-
import ConnectionController from '../connectionController';
3+
import ConnectionController, {
4+
DataServiceEventTypes,
5+
} from '../connectionController';
46
import ExplorerTreeController from './explorerTreeController';
57

68
import { createLogger } from '../logging';
79

810
const log = createLogger('explorer controller');
911

1012
export default class ExplorerController {
13+
private _connectionController: ConnectionController;
1114
private _treeController: ExplorerTreeController;
1215
private _treeView?: vscode.TreeView<vscode.TreeItem>;
1316

1417
constructor(connectionController: ConnectionController) {
1518
log.info('activate explorer controller');
1619

20+
this._connectionController = connectionController;
1721
this._treeController = new ExplorerTreeController(connectionController);
1822
}
1923

20-
createTreeView(): void {
21-
this._treeView = vscode.window.createTreeView('mongoDB', {
22-
treeDataProvider: this._treeController
23-
});
24+
createTreeView = (): void => {
25+
// Remove the listener that called this function.
26+
this._connectionController.removeEventListener(
27+
DataServiceEventTypes.CONNECTIONS_DID_CHANGE,
28+
this.createTreeView
29+
);
2430

25-
this._treeController.activateTreeViewEventHandlers(this._treeView);
31+
if (!this._treeView) {
32+
this._treeView = vscode.window.createTreeView('mongoDB', {
33+
treeDataProvider: this._treeController,
34+
});
35+
36+
this._treeController.activateTreeViewEventHandlers(this._treeView);
37+
}
38+
};
39+
40+
activateTreeView(): void {
41+
// Listen for a change in connections to occur before we create the tree
42+
// so that we show the `viewsWelcome` before any connections are added.
43+
this._connectionController.addEventListener(
44+
DataServiceEventTypes.CONNECTIONS_DID_CHANGE,
45+
this.createTreeView
46+
);
2647
}
2748

2849
deactivate(): void {

src/explorer/explorerTreeController.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ import { DOCUMENT_LIST_ITEM, CollectionTypes } from './documentListTreeItem';
1414

1515
const log = createLogger('explorer controller');
1616

17-
export default class ExplorerTreeController implements vscode.TreeDataProvider<vscode.TreeItem> {
17+
export default class ExplorerTreeController
18+
implements vscode.TreeDataProvider<vscode.TreeItem> {
1819
private _connectionController: ConnectionController;
1920
private _mdbConnectionsTreeItem: MDBConnectionsTreeItem;
2021

src/mdbExtensionController.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ export default class MDBExtensionController implements vscode.Disposable {
7474
}
7575

7676
activate(): void {
77+
this._explorerController.activateTreeView();
7778
this._connectionController.loadSavedConnections();
78-
this._explorerController.createTreeView();
7979
this._telemetryController.activate();
8080
this._languageServerController.activate();
8181

src/test/suite/connectionController.test.ts

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as assert from 'assert';
22
import * as vscode from 'vscode';
3-
import { afterEach } from 'mocha';
3+
import { afterEach, beforeEach } from 'mocha';
44
import * as sinon from 'sinon';
55
import Connection = require('mongodb-connection-model/lib/model');
66

@@ -26,6 +26,11 @@ suite('Connection Controller Test Suite', () => {
2626
const mockExtensionContext = new TestExtensionContext();
2727
const mockStorageController = new StorageController(mockExtensionContext);
2828

29+
beforeEach(() => {
30+
// Here we stub the showInformationMessage process because it is too much
31+
// for the render process and leads to crashes while testing.
32+
sinon.replace(vscode.window, 'showInformationMessage', sinon.stub());
33+
});
2934
afterEach(() => {
3035
// Reset our mock extension's state.
3136
mockExtensionContext._workspaceState = {};
@@ -68,7 +73,11 @@ suite('Connection Controller Test Suite', () => {
6873
);
6974
const dataService = testConnectionController.getActiveDataService();
7075
assert(dataService !== null);
71-
assert(testConnectionController._activeConnectionModel?.appname.startsWith('mongodb-vscode'));
76+
assert(
77+
testConnectionController._activeConnectionModel?.appname.startsWith(
78+
'mongodb-vscode'
79+
)
80+
);
7281
assert(testConnectionController.isCurrentlyConnected());
7382
})
7483
.then(done, done);
@@ -484,7 +493,7 @@ suite('Connection Controller Test Suite', () => {
484493
assert(
485494
Object.keys(connections).length === 4,
486495
`Expected 4 connection configurations found ${
487-
Object.keys(connections).length
496+
Object.keys(connections).length
488497
}`
489498
);
490499
assert(
@@ -493,7 +502,7 @@ suite('Connection Controller Test Suite', () => {
493502
);
494503
assert(
495504
Object.keys(connections).includes('testWorkspaceConnectionModel2') ===
496-
true,
505+
true,
497506
"Expected connection configurations to include 'testWorkspaceConnectionModel2'"
498507
);
499508
assert(
@@ -502,7 +511,7 @@ suite('Connection Controller Test Suite', () => {
502511
);
503512
assert(
504513
connections.testGlobalConnectionModel2.driverUrl ===
505-
'testGlobalConnectionModel2DriverUrl',
514+
'testGlobalConnectionModel2DriverUrl',
506515
"Expected loaded connection to include driver url 'testGlobalConnectionModel2DriverUrl'"
507516
);
508517
assert(
@@ -540,7 +549,7 @@ suite('Connection Controller Test Suite', () => {
540549
assert(
541550
Object.keys(globalStoreConnections).length === 1,
542551
`Expected global store connections to have 1 connection found ${
543-
Object.keys(globalStoreConnections).length
552+
Object.keys(globalStoreConnections).length
544553
}`
545554
);
546555
const id = Object.keys(globalStoreConnections)[0];
@@ -588,7 +597,7 @@ suite('Connection Controller Test Suite', () => {
588597
assert(
589598
Object.keys(workspaceStoreConnections).length === 1,
590599
`Expected workspace store connections to have 1 connection found ${
591-
Object.keys(workspaceStoreConnections).length
600+
Object.keys(workspaceStoreConnections).length
592601
}`
593602
);
594603
const id = Object.keys(workspaceStoreConnections)[0];
@@ -669,7 +678,7 @@ suite('Connection Controller Test Suite', () => {
669678
assert(
670679
Object.keys(workspaceStoreConnections).length === 1,
671680
`Expected workspace store connections to have 1 connection found ${
672-
Object.keys(workspaceStoreConnections).length
681+
Object.keys(workspaceStoreConnections).length
673682
}`
674683
);
675684

@@ -687,7 +696,7 @@ suite('Connection Controller Test Suite', () => {
687696
assert(
688697
testConnectionController.getSavedConnections().length === 1,
689698
`Expected 1 connection config, found ${
690-
testConnectionController.getSavedConnections().length
699+
testConnectionController.getSavedConnections().length
691700
}.`
692701
);
693702
const id = testConnectionController.getSavedConnections()[0].id;
@@ -839,7 +848,7 @@ suite('Connection Controller Test Suite', () => {
839848
assert(
840849
Object.keys(workspaceStoreConnections).length === 1,
841850
`Expected workspace store connections to have 1 connection found ${
842-
Object.keys(workspaceStoreConnections).length
851+
Object.keys(workspaceStoreConnections).length
843852
}`
844853
);
845854

@@ -855,7 +864,7 @@ suite('Connection Controller Test Suite', () => {
855864
assert(
856865
Object.keys(postWorkspaceStoreConnections).length === 0,
857866
`Expected workspace store connections to have 0 connections found ${
858-
Object.keys(postWorkspaceStoreConnections).length
867+
Object.keys(postWorkspaceStoreConnections).length
859868
}`
860869
);
861870
})
@@ -889,7 +898,7 @@ suite('Connection Controller Test Suite', () => {
889898
assert(
890899
Object.keys(globalStoreConnections).length === 1,
891900
`Expected workspace store connections to have 1 connection found ${
892-
Object.keys(globalStoreConnections).length
901+
Object.keys(globalStoreConnections).length
893902
}`
894903
);
895904

@@ -903,7 +912,7 @@ suite('Connection Controller Test Suite', () => {
903912
assert(
904913
Object.keys(postGlobalStoreConnections).length === 0,
905914
`Expected global store connections to have 0 connections found ${
906-
Object.keys(postGlobalStoreConnections).length
915+
Object.keys(postGlobalStoreConnections).length
907916
}`
908917
);
909918
});
@@ -939,7 +948,7 @@ suite('Connection Controller Test Suite', () => {
939948
assert(
940949
Object.keys(workspaceStoreConnections).length === 1,
941950
`Expected workspace store connections to have 1 connection found ${
942-
Object.keys(workspaceStoreConnections).length
951+
Object.keys(workspaceStoreConnections).length
943952
}`
944953
);
945954
const connectionId =
@@ -974,7 +983,7 @@ suite('Connection Controller Test Suite', () => {
974983
testConnectionController.getSavedConnections()
975984
.length === 1,
976985
`Expected 1 connection config, found ${
977-
testConnectionController.getSavedConnections().length
986+
testConnectionController.getSavedConnections().length
978987
}.`
979988
);
980989
const id = testConnectionController.getSavedConnections()[0]

src/test/suite/explorer/explorerController.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as assert from 'assert';
22
import * as vscode from 'vscode';
33
import { beforeEach, afterEach } from 'mocha';
44
import Connection = require('mongodb-connection-model/lib/model');
5+
import * as sinon from 'sinon';
56

67
import {
78
DefaultSavingLocations,
@@ -34,6 +35,7 @@ suite('Explorer Controller Test Suite', () => {
3435
);
3536
// Reset our connections.
3637
mdbTestExtension.testExtensionController._connectionController.clearAllConnections();
38+
sinon.restore();
3739
});
3840

3941
test('should have a connections root', (done) => {
@@ -325,4 +327,36 @@ suite('Explorer Controller Test Suite', () => {
325327
});
326328
});
327329
});
330+
331+
test('tree view should be not created by default (shows welcome view)', () => {
332+
const testExplorerController =
333+
mdbTestExtension.testExtensionController._explorerController;
334+
335+
assert(testExplorerController.getTreeView() === undefined);
336+
});
337+
338+
test('tree view should call create tree view after a "CONNECTIONS_DID_CHANGE" event', (done) => {
339+
const testExplorerController =
340+
mdbTestExtension.testExtensionController._explorerController;
341+
342+
testExplorerController.activateTreeView();
343+
344+
const treeControllerStub = sinon.stub().returns();
345+
sinon.replace(
346+
testExplorerController.getTreeController(),
347+
'activateTreeViewEventHandlers',
348+
treeControllerStub
349+
);
350+
351+
const vscodeCreateTreeViewStub = sinon.stub().returns('');
352+
sinon.replace(vscode.window, 'createTreeView', vscodeCreateTreeViewStub);
353+
354+
mdbTestExtension.testExtensionController._connectionController
355+
.addNewConnectionStringAndConnect(TEST_DATABASE_URI)
356+
.then(() => {
357+
mdbTestExtension.testExtensionController._connectionController.disconnect();
358+
assert(vscodeCreateTreeViewStub.called);
359+
})
360+
.then(done);
361+
});
328362
});

src/test/suite/mdbExtensionController.test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as assert from 'assert';
22
import * as vscode from 'vscode';
3-
import { afterEach } from 'mocha';
3+
import { afterEach, beforeEach } from 'mocha';
44
import Connection = require('mongodb-connection-model/lib/model');
55
const sinon = require('sinon');
66

@@ -22,6 +22,11 @@ import { StorageScope } from '../../storage/storageController';
2222
const testDatabaseURI = 'mongodb://localhost:27018';
2323

2424
suite('MDBExtensionController Test Suite', () => {
25+
beforeEach(() => {
26+
// Here we stub the showInformationMessage process because it is too much
27+
// for the render process and leads to crashes while testing.
28+
sinon.replace(vscode.window, 'showInformationMessage', sinon.stub());
29+
});
2530
afterEach(() => {
2631
sinon.restore();
2732
});

0 commit comments

Comments
 (0)