Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions configs/testing-library-compass/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ type TestConnectionsOptions = {
connectFn?: (
connectionOptions: ConnectionInfo['connectionOptions']
) => Partial<DataService> | Promise<Partial<DataService>>;
/**
* Connection storage mock
*/
connectionStorage?: ConnectionStorage;
} & Partial<
Omit<
React.ComponentProps<typeof CompassConnections>,
Expand Down Expand Up @@ -258,9 +262,9 @@ function createWrapper(
preferences: new InMemoryPreferencesAccess(options.preferences),
track: Sinon.stub(),
logger: createNoopLogger(),
connectionStorage: new InMemoryConnectionStorage(
options.connections
) as ConnectionStorage,
connectionStorage:
options.connectionStorage ??
(new InMemoryConnectionStorage(options.connections) as ConnectionStorage),
connectionsStore: {
getState: undefined as unknown as () => State,
actions: {} as ReturnType<typeof useConnectionActions>,
Expand Down Expand Up @@ -330,6 +334,12 @@ function createWrapper(
<ConnectFnProvider connect={wrapperState.connect}>
<CompassConnections
appName={options.appName ?? 'TEST'}
onFailToLoadConnections={
options.onFailToLoadConnections ??
(() => {
// noop
})
}
onExtraConnectionDataRequest={
options.onExtraConnectionDataRequest ??
(() => {
Expand Down
5 changes: 5 additions & 0 deletions packages/compass-connections/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ const ConnectionsComponent: React.FunctionComponent<{
* connections on plugin activate
*/
preloadStorageConnectionInfos?: ConnectionInfo[];
/**
* When connections fail to load, this callback will be called
*/
onFailToLoadConnections: (error: Error) => void;
}> = ({ children }) => {
return (
<ConnectionActionsProvider>
Expand Down Expand Up @@ -90,6 +94,7 @@ const CompassConnectionsPlugin = registerHadronPlugin(
appName: initialProps.appName,
connectFn: initialProps.connectFn,
globalAppRegistry,
onFailToLoadConnections: initialProps.onFailToLoadConnections,
});

setTimeout(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
render,
} from '@mongodb-js/testing-library-compass';
import React from 'react';
import { InMemoryConnectionStorage } from '@mongodb-js/connection-storage/provider';

const mockConnections = [
{
Expand Down Expand Up @@ -48,6 +49,30 @@ describe('CompassConnections store', function () {
sinon.restore();
});

describe('#loadAll', function () {
it('calls onFailToLoadConnections when it fails to loadAll connections', async function () {
const onFailToLoadConnectionsSpy = sinon.spy();
const connectionStorage = new InMemoryConnectionStorage();
connectionStorage.loadAll = sinon
.stub()
.rejects(new Error('loadAll failed'));

renderCompassConnections({
connectionStorage,
onFailToLoadConnections: onFailToLoadConnectionsSpy,
});

await waitFor(() => {
expect(onFailToLoadConnectionsSpy).to.have.been.calledOnce;
});

expect(onFailToLoadConnectionsSpy.firstCall.firstArg).to.have.property(
'message',
'loadAll failed'
);
});
});

describe('#connect', function () {
it('should show notifications throughout connection flow and save connection to persistent store', async function () {
const { connectionsStore, connectionStorage, track } =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ type ThunkExtraArg = {
) => Promise<[ExtraConnectionDataForTelemetry, string | null]>;
connectFn?: typeof devtoolsConnect;
globalAppRegistry: Pick<AppRegistry, 'on' | 'emit' | 'removeListener'>;
onFailToLoadConnections: (error: Error) => void;
};

export type ConnectionsThunkAction<
Expand Down Expand Up @@ -1266,7 +1267,11 @@ export const loadConnections = (): ConnectionsThunkAction<
| ConnectionsLoadSuccessAction
| ConnectionsLoadErrorAction
> => {
return async (dispatch, getState, { connectionStorage }) => {
return async (
dispatch,
getState,
{ connectionStorage, onFailToLoadConnections }
) => {
if (getState().connections.status !== 'initial') {
return;
}
Expand All @@ -1276,6 +1281,7 @@ export const loadConnections = (): ConnectionsThunkAction<
dispatch({ type: ActionTypes.ConnectionsLoadSuccess, connections });
} catch (err) {
dispatch({ type: ActionTypes.ConnectionsLoadError, error: err as any });
onFailToLoadConnections(err as Error);
}
};
};
Expand Down
18 changes: 16 additions & 2 deletions packages/compass-web/sandbox/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import React, { useLayoutEffect, useRef } from 'react';
import React, { useCallback, useLayoutEffect, useRef } from 'react';
import ReactDOM from 'react-dom';
import { resetGlobalCSS, css, Body } from '@mongodb-js/compass-components';
import {
resetGlobalCSS,
css,
Body,
openToast,
} from '@mongodb-js/compass-components';
import type { AllPreferences } from 'compass-preferences-model';
import { CompassWeb } from '../src/index';
import { SandboxConnectionStorageProvider } from '../src/connection-storage';
Expand Down Expand Up @@ -86,6 +91,14 @@ const App = () => {
getMetaEl('csrf-time').setAttribute('content', csrfTime ?? '');
}, [csrfToken, csrfTime]);

const onFailToLoadConnections = useCallback((error: Error) => {
openToast('failed-to-load-connections', {
title: 'Failed to load connections',
description: error.message,
variant: 'warning',
});
}, []);

if (status === 'checking') {
return null;
}
Expand Down Expand Up @@ -132,6 +145,7 @@ const App = () => {
onTrack={sandboxTelemetry.track}
onDebug={sandboxLogger.debug}
onLog={sandboxLogger.log}
onFailToLoadConnections={onFailToLoadConnections}
></CompassWeb>
</Body>
</SandboxPreferencesUpdateProvider>
Expand Down
1 change: 1 addition & 0 deletions packages/compass-web/src/entrypoint.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ describe('CompassWeb', function () {
enableCreatingNewConnections: true,
...props.initialPreferences,
}}
onFailToLoadConnections={() => {}}
></CompassWeb>
</ConnectFnProvider>
);
Expand Down
7 changes: 7 additions & 0 deletions packages/compass-web/src/entrypoint.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ type CompassWebProps = {
onOpenConnectViaModal?: (
atlasMetadata: ConnectionInfo['atlasMetadata']
) => void;

/**
* Callback prop called when connections fail to load
*/
onFailToLoadConnections: (err: Error) => void;
};

function CompassWorkspace({
Expand Down Expand Up @@ -253,6 +258,7 @@ const CompassWeb = ({
onDebug,
onTrack,
onOpenConnectViaModal,
onFailToLoadConnections,
}: CompassWebProps) => {
const appRegistry = useRef(new AppRegistry());
const logger = useCompassWebLoggerAndTelemetry({
Expand Down Expand Up @@ -335,6 +341,7 @@ const CompassWeb = ({
>
<CompassConnections
appName={appName ?? 'Compass Web'}
onFailToLoadConnections={onFailToLoadConnections}
onExtraConnectionDataRequest={() => {
return Promise.resolve([{}, null] as [
Record<string, unknown>,
Expand Down
8 changes: 8 additions & 0 deletions packages/compass/src/app/components/home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
css,
cx,
getScrollbarStyles,
openToast,
palette,
resetGlobalCSS,
} from '@mongodb-js/compass-components';
Expand Down Expand Up @@ -149,6 +150,13 @@ function HomeWithConnections({
onExtraConnectionDataRequest={getExtraConnectionData}
onAutoconnectInfoRequest={onAutoconnectInfoRequest}
doNotReconnectDisconnectedAutoconnectInfo
onFailToLoadConnections={(error) => {
openToast('failed-to-load-connections', {
title: 'Failed to load connections',
description: error.message,
variant: 'warning',
});
}}
>
<Home {...props}></Home>
</CompassConnections>
Expand Down
Loading