Skip to content

Commit 0850375

Browse files
committed
fix(tree-explorer): update tests to use inspect and check for global and workspace settings
1 parent 0128835 commit 0850375

File tree

2 files changed

+67
-35
lines changed

2 files changed

+67
-35
lines changed

src/storage/connectionStorage.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,14 +191,14 @@ export class ConnectionStorage {
191191
}
192192

193193
const combinedPresetSavedConnections: PresetSavedConnectionWithSource[] = [
194-
...(presetSavedConnectionsInfo?.workspaceValue ?? []).map((preset) => ({
195-
...preset,
196-
source: 'workspaceSettings' as const,
197-
})),
198194
...(presetSavedConnectionsInfo?.globalValue ?? []).map((preset) => ({
199195
...preset,
200196
source: 'globalSettings' as const,
201197
})),
198+
...(presetSavedConnectionsInfo?.workspaceValue ?? []).map((preset) => ({
199+
...preset,
200+
source: 'workspaceSettings' as const,
201+
})),
202202
];
203203

204204
return combinedPresetSavedConnections.map((presetConnection) => ({
@@ -252,7 +252,7 @@ export class ConnectionStorage {
252252

253253
const presetSavedConnections = this._loadPresetSavedConnections();
254254

255-
return [...presetSavedConnections, ...loadedConnections];
255+
return [...loadedConnections, ...presetSavedConnections];
256256
}
257257

258258
async removeConnection(connectionId: string): Promise<void> {

src/test/suite/storage/connectionStorage.test.ts

Lines changed: 62 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,12 @@ suite('Connection Storage Test Suite', function () {
324324

325325
suite('when there are preset connections', () => {
326326
const presetConnections = {
327+
globalValue: [
328+
{
329+
name: 'Global Connection 1',
330+
connectionString: 'mongodb://localhost:27017/',
331+
},
332+
],
327333
workspaceValue: [
328334
{
329335
name: 'Preset Connection 1',
@@ -343,11 +349,11 @@ suite('Connection Storage Test Suite', function () {
343349
],
344350
vscode.WorkspaceConfiguration
345351
>;
346-
let getPresetSavedConnectionsStub: sinon.SinonStub;
352+
let inspectPresetSavedConnectionsStub: sinon.SinonStub;
347353

348354
beforeEach(() => {
349355
testSandbox.restore();
350-
getPresetSavedConnectionsStub = testSandbox.stub();
356+
inspectPresetSavedConnectionsStub = testSandbox.stub();
351357
});
352358

353359
test('loads the preset connections', async () => {
@@ -356,25 +362,39 @@ suite('Connection Storage Test Suite', function () {
356362
'getConfiguration'
357363
);
358364
getConfigurationStub.returns({
359-
inspect: getPresetSavedConnectionsStub,
365+
inspect: inspectPresetSavedConnectionsStub,
366+
get: () => undefined,
360367
} as any);
361368

362-
getPresetSavedConnectionsStub
369+
inspectPresetSavedConnectionsStub
363370
.withArgs('presetSavedConnections')
364371
.returns(presetConnections);
365372

366-
const connections = await testConnectionStorage.loadConnections();
373+
const loadedConnections = await testConnectionStorage.loadConnections();
367374

368-
expect(connections.length).to.equal(2);
375+
const expectedConnectionValues = [
376+
...presetConnections.globalValue.map((connection) => ({
377+
...connection,
378+
source: 'globalSettings',
379+
})),
380+
...presetConnections.workspaceValue.map((connection) => ({
381+
...connection,
382+
source: 'workspaceSettings',
383+
})),
384+
];
385+
386+
expect(loadedConnections.length).equals(
387+
expectedConnectionValues.length
388+
);
369389

370-
for (let i = 0; i < connections.length; i++) {
371-
const connection = connections[i];
372-
const presetConnection = presetConnections[i];
373-
expect(connection.name).equals(presetConnection.name);
374-
expect(connection.connectionOptions.connectionString).equals(
375-
presetConnection.connectionString
390+
for (let i = 0; i < expectedConnectionValues.length; i++) {
391+
const connection = loadedConnections[i];
392+
const expected = expectedConnectionValues[i];
393+
expect(connection.name).equals(expected.name);
394+
expect(connection.connectionOptions.connectionString).contains(
395+
expected.connectionString
376396
);
377-
expect(connection.source).equals('workspaceSettings');
397+
expect(connection.source).equals(expected.source);
378398
}
379399
});
380400

@@ -387,34 +407,46 @@ suite('Connection Storage Test Suite', function () {
387407
'getConfiguration'
388408
);
389409
getConfigurationStub.returns({
390-
get: getPresetSavedConnectionsStub,
410+
inspect: inspectPresetSavedConnectionsStub,
411+
get: () => undefined,
391412
} as any);
392413

393-
getPresetSavedConnectionsStub
414+
inspectPresetSavedConnectionsStub
394415
.withArgs('presetSavedConnections')
395416
.returns(presetConnections);
396417

397418
const loadedConnections = await testConnectionStorage.loadConnections();
398419

399-
expect(loadedConnections.length).equals(3);
420+
const expectedConnectionValues = [
421+
{
422+
name: savedConnection.name,
423+
source: 'user',
424+
connectionString:
425+
savedConnection.connectionOptions.connectionString,
426+
},
427+
...presetConnections.globalValue.map((connection) => ({
428+
...connection,
429+
source: 'globalSettings',
430+
})),
431+
...presetConnections.workspaceValue.map((connection) => ({
432+
...connection,
433+
source: 'workspaceSettings',
434+
})),
435+
];
436+
437+
expect(loadedConnections.length).equals(
438+
expectedConnectionValues.length
439+
);
400440

401-
for (let i = 0; i < presetConnections.workspaceValue.length; i++) {
441+
for (let i = 0; i < expectedConnectionValues.length; i++) {
402442
const connection = loadedConnections[i];
403-
const presetConnection = presetConnections[i];
404-
expect(connection.name).equals(presetConnection.name);
405-
expect(connection.connectionOptions.connectionString).equals(
406-
presetConnection.connectionString
443+
const expected = expectedConnectionValues[i];
444+
expect(connection.name).equals(expected.name);
445+
expect(connection.connectionOptions.connectionString).contains(
446+
expected.connectionString
407447
);
408-
expect(connection.source).equals('workspaceSettings');
448+
expect(connection.source).equals(expected.source);
409449
}
410-
411-
const savedLoadedConnection = loadedConnections[2];
412-
413-
expect(savedLoadedConnection.name).equals(savedConnection.name);
414-
expect(
415-
savedLoadedConnection.connectionOptions.connectionString
416-
).contains(savedConnection.connectionOptions.connectionString);
417-
expect(savedLoadedConnection.source).equals('workspaceSettings');
418450
});
419451
});
420452

0 commit comments

Comments
 (0)