Skip to content

Commit fc705a0

Browse files
authored
fix(connections): make sure that autoconnect info is provided in connection hooks COMPASS-8044 (#5978)
* fix(connections): make sure that autoconnect info is accounted for in the connections lists * chore(e2e): update autoconnect test to check that connection succeeded
1 parent f1cc072 commit fc705a0

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

packages/compass-connections/src/hooks/use-connection-repository.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ function sortedAlphabetically(a: ConnectionInfo, b: ConnectionInfo): number {
4343
export type ConnectionRepository = {
4444
favoriteConnections: ConnectionInfo[];
4545
nonFavoriteConnections: ConnectionInfo[];
46+
autoConnectInfo?: ConnectionInfo;
4647
saveConnection: (info: PartialConnectionInfo) => Promise<ConnectionInfo>;
4748
deleteConnection: (info: ConnectionInfo) => Promise<void>;
4849
getConnectionInfoById: (
@@ -189,6 +190,7 @@ export function useConnectionRepository(): ConnectionRepository {
189190
getConnectionTitleById,
190191
favoriteConnections,
191192
nonFavoriteConnections,
193+
autoConnectInfo,
192194
saveConnection,
193195
deleteConnection,
194196
};

packages/compass-connections/src/hooks/use-connections-with-status.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@ export function useConnectionsWithStatus(): ConnectionInfoWithStatus[] {
1717
// when this code is refactored to use the hadron plugin interface, storage
1818
// should be handled through the plugin activation lifecycle
1919
const connectionsManager = useConnectionsManagerContext();
20-
const { favoriteConnections, nonFavoriteConnections } =
20+
const { favoriteConnections, nonFavoriteConnections, autoConnectInfo } =
2121
useConnectionRepository();
2222
const allConnections = useMemo(() => {
23-
return [...favoriteConnections, ...nonFavoriteConnections];
24-
}, [favoriteConnections, nonFavoriteConnections]);
23+
return favoriteConnections.concat(
24+
nonFavoriteConnections,
25+
autoConnectInfo ? autoConnectInfo : []
26+
);
27+
}, [favoriteConnections, nonFavoriteConnections, autoConnectInfo]);
2528

2629
const [connectionsWithStatus, setConnectionsWithStatus] = useState<
2730
ConnectionInfoWithStatus[]
@@ -60,7 +63,7 @@ export function useConnectionsWithStatus(): ConnectionInfoWithStatus[] {
6063

6164
useEffect(() => {
6265
updateListRef.current();
63-
}, [favoriteConnections, nonFavoriteConnections]);
66+
}, [favoriteConnections, nonFavoriteConnections, autoConnectInfo]);
6467

6568
useEffect(() => {
6669
const updateOnStatusChange = () => {

packages/compass-e2e-tests/tests/auto-connect.test.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { expect } from 'chai';
2+
import type { Compass } from '../helpers/compass';
23
import {
34
init,
45
cleanup,
@@ -13,6 +14,7 @@ import path from 'path';
1314
import { promises as fs } from 'fs';
1415

1516
const connectionStringSuccess = 'mongodb://127.0.0.1:27091/test';
17+
const connectionStringSuccessTitle = '127.0.0.1:27091';
1618
const connectionStringUnreachable =
1719
'mongodb://127.0.0.1:27091/test?tls=true&serverSelectionTimeoutMS=10';
1820
const connectionStringInvalid = 'http://example.com';
@@ -81,13 +83,29 @@ describe('Automatically connecting from the command line', function () {
8183
await fs.rmdir(tmpdir, { recursive: true });
8284
});
8385

86+
async function waitForConnectionSuccessAndCheckConnection(
87+
compass: Compass,
88+
expectedTitle = connectionStringSuccessTitle
89+
) {
90+
await compass.browser.waitForConnectionResult('success');
91+
const sidebarTitle = await compass.browser
92+
.$(Selectors.SidebarTitle)
93+
.getText();
94+
expect(sidebarTitle).to.eq(expectedTitle);
95+
const result = await compass.browser.shellEval(
96+
'db.runCommand({ connectionStatus: 1 })',
97+
true
98+
);
99+
expect(result).to.have.property('ok', 1);
100+
}
101+
84102
it('works with a connection string on the command line', async function () {
85103
const compass = await init(this.test?.fullTitle(), {
86104
wrapBinary: positionalArgs([connectionStringSuccess]),
87105
noWaitForConnectionScreen: true,
88106
});
89107
try {
90-
await compass.browser.waitForConnectionResult('success');
108+
await waitForConnectionSuccessAndCheckConnection(compass);
91109
} finally {
92110
await cleanup(compass);
93111
}
@@ -105,7 +123,7 @@ describe('Automatically connecting from the command line', function () {
105123
noWaitForConnectionScreen: true,
106124
});
107125
try {
108-
await compass.browser.waitForConnectionResult('success');
126+
await waitForConnectionSuccessAndCheckConnection(compass, 'Success');
109127
} finally {
110128
await cleanup(compass);
111129
}

0 commit comments

Comments
 (0)