From fc705a032f5b9c8f17fe0f039e28032458d86e02 Mon Sep 17 00:00:00 2001 From: Sergey Petushkov Date: Thu, 27 Jun 2024 13:48:55 +0200 Subject: [PATCH] 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 --- .../src/hooks/use-connection-repository.ts | 2 ++ .../src/hooks/use-connections-with-status.ts | 11 ++++++---- .../tests/auto-connect.test.ts | 22 +++++++++++++++++-- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/packages/compass-connections/src/hooks/use-connection-repository.ts b/packages/compass-connections/src/hooks/use-connection-repository.ts index 2dbd17f87c1..286cffedfbf 100644 --- a/packages/compass-connections/src/hooks/use-connection-repository.ts +++ b/packages/compass-connections/src/hooks/use-connection-repository.ts @@ -43,6 +43,7 @@ function sortedAlphabetically(a: ConnectionInfo, b: ConnectionInfo): number { export type ConnectionRepository = { favoriteConnections: ConnectionInfo[]; nonFavoriteConnections: ConnectionInfo[]; + autoConnectInfo?: ConnectionInfo; saveConnection: (info: PartialConnectionInfo) => Promise; deleteConnection: (info: ConnectionInfo) => Promise; getConnectionInfoById: ( @@ -189,6 +190,7 @@ export function useConnectionRepository(): ConnectionRepository { getConnectionTitleById, favoriteConnections, nonFavoriteConnections, + autoConnectInfo, saveConnection, deleteConnection, }; diff --git a/packages/compass-connections/src/hooks/use-connections-with-status.ts b/packages/compass-connections/src/hooks/use-connections-with-status.ts index 09036a12dbd..59aecf6d88e 100644 --- a/packages/compass-connections/src/hooks/use-connections-with-status.ts +++ b/packages/compass-connections/src/hooks/use-connections-with-status.ts @@ -17,11 +17,14 @@ export function useConnectionsWithStatus(): ConnectionInfoWithStatus[] { // when this code is refactored to use the hadron plugin interface, storage // should be handled through the plugin activation lifecycle const connectionsManager = useConnectionsManagerContext(); - const { favoriteConnections, nonFavoriteConnections } = + const { favoriteConnections, nonFavoriteConnections, autoConnectInfo } = useConnectionRepository(); const allConnections = useMemo(() => { - return [...favoriteConnections, ...nonFavoriteConnections]; - }, [favoriteConnections, nonFavoriteConnections]); + return favoriteConnections.concat( + nonFavoriteConnections, + autoConnectInfo ? autoConnectInfo : [] + ); + }, [favoriteConnections, nonFavoriteConnections, autoConnectInfo]); const [connectionsWithStatus, setConnectionsWithStatus] = useState< ConnectionInfoWithStatus[] @@ -60,7 +63,7 @@ export function useConnectionsWithStatus(): ConnectionInfoWithStatus[] { useEffect(() => { updateListRef.current(); - }, [favoriteConnections, nonFavoriteConnections]); + }, [favoriteConnections, nonFavoriteConnections, autoConnectInfo]); useEffect(() => { const updateOnStatusChange = () => { diff --git a/packages/compass-e2e-tests/tests/auto-connect.test.ts b/packages/compass-e2e-tests/tests/auto-connect.test.ts index 8726e81de87..a25c0bf6696 100644 --- a/packages/compass-e2e-tests/tests/auto-connect.test.ts +++ b/packages/compass-e2e-tests/tests/auto-connect.test.ts @@ -1,4 +1,5 @@ import { expect } from 'chai'; +import type { Compass } from '../helpers/compass'; import { init, cleanup, @@ -13,6 +14,7 @@ import path from 'path'; import { promises as fs } from 'fs'; const connectionStringSuccess = 'mongodb://127.0.0.1:27091/test'; +const connectionStringSuccessTitle = '127.0.0.1:27091'; const connectionStringUnreachable = 'mongodb://127.0.0.1:27091/test?tls=true&serverSelectionTimeoutMS=10'; const connectionStringInvalid = 'http://example.com'; @@ -81,13 +83,29 @@ describe('Automatically connecting from the command line', function () { await fs.rmdir(tmpdir, { recursive: true }); }); + async function waitForConnectionSuccessAndCheckConnection( + compass: Compass, + expectedTitle = connectionStringSuccessTitle + ) { + await compass.browser.waitForConnectionResult('success'); + const sidebarTitle = await compass.browser + .$(Selectors.SidebarTitle) + .getText(); + expect(sidebarTitle).to.eq(expectedTitle); + const result = await compass.browser.shellEval( + 'db.runCommand({ connectionStatus: 1 })', + true + ); + expect(result).to.have.property('ok', 1); + } + it('works with a connection string on the command line', async function () { const compass = await init(this.test?.fullTitle(), { wrapBinary: positionalArgs([connectionStringSuccess]), noWaitForConnectionScreen: true, }); try { - await compass.browser.waitForConnectionResult('success'); + await waitForConnectionSuccessAndCheckConnection(compass); } finally { await cleanup(compass); } @@ -105,7 +123,7 @@ describe('Automatically connecting from the command line', function () { noWaitForConnectionScreen: true, }); try { - await compass.browser.waitForConnectionResult('success'); + await waitForConnectionSuccessAndCheckConnection(compass, 'Success'); } finally { await cleanup(compass); }