Skip to content

Commit d1001b6

Browse files
committed
chore(testing-library, sidebar): make preload skip interface more explicit in test helpers; extend the test to check for disabled elements
1 parent e13c3d4 commit d1001b6

File tree

5 files changed

+45
-30
lines changed

5 files changed

+45
-30
lines changed

configs/testing-library-compass/src/index.tsx

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,11 @@ type TestConnectionsOptions = {
8282
preferences?: Partial<AllPreferences>;
8383
/**
8484
* Initial list of connections to be "loaded" to the application. Empty list
85-
* by default. You can explicitly pass `null` to disable initial preloading of
86-
* the connections
85+
* by default. You can explicitly pass `no-preload` to disable initial
86+
* preloading of the connections, otherwise connections are always preloaded
87+
* before rendering when using helper methods
8788
*/
88-
connections?: ConnectionInfo[] | null;
89+
connections?: ConnectionInfo[] | 'no-preload';
8990
/**
9091
* Connection function that returns DataService when connecting to a
9192
* connection with the connections store. Second argument is a constructor
@@ -247,6 +248,12 @@ const EmptyWrapper = ({ children }: { children: React.ReactElement }) => {
247248
return <>{children}</>;
248249
};
249250

251+
function getConnectionsFromConnectionsOption(
252+
connections: TestConnectionsOptions['connections']
253+
): Exclude<TestConnectionsOptions['connections'], 'no-preload'> {
254+
return connections === 'no-preload' ? undefined : connections ?? [];
255+
}
256+
250257
const TEST_ENV_CURRENT_CONNECTION = {
251258
info: {
252259
id: 'TEST',
@@ -268,9 +275,7 @@ function createWrapper(
268275
TestingLibraryWrapper: ComponentWithChildren = EmptyWrapper,
269276
container?: HTMLElement
270277
) {
271-
const connections =
272-
options.connections === null ? undefined : options.connections ?? [];
273-
278+
const connections = getConnectionsFromConnectionsOption(options.connections);
274279
const wrapperState = {
275280
globalAppRegistry: new AppRegistry(),
276281
localAppRegistry: new AppRegistry(),
@@ -421,7 +426,9 @@ function renderWithConnections(
421426
hydrate,
422427
});
423428
expect(
424-
(connectionsOptions.connections ?? []).every((info) => {
429+
(
430+
getConnectionsFromConnectionsOption(connectionsOptions.connections) ?? []
431+
).every((info) => {
425432
return !!wrapperState.connectionsStore.getState().connections.byId[
426433
info.id
427434
];
@@ -515,7 +522,10 @@ async function renderWithActiveConnection(
515522
const renderResult = renderWithConnections(ui, {
516523
...options,
517524
wrapper: ConnectionInfoWrapper,
518-
connections: [connectionInfo, ...(connections ?? [])],
525+
connections: [
526+
connectionInfo,
527+
...(getConnectionsFromConnectionsOption(connections) ?? []),
528+
],
519529
});
520530
await waitForConnect(renderResult.connectionsStore, connectionInfo);
521531
return renderResult;
@@ -537,7 +547,10 @@ async function renderHookWithActiveConnection<HookProps, HookResult>(
537547
const renderHookResult = renderHookWithConnections(cb, {
538548
...options,
539549
wrapper: ConnectionInfoWrapper,
540-
connections: [connectionInfo, ...(connections ?? [])],
550+
connections: [
551+
connectionInfo,
552+
...(getConnectionsFromConnectionsOption(connections) ?? []),
553+
],
541554
});
542555
await waitForConnect(renderHookResult.connectionsStore, connectionInfo);
543556
return renderHookResult;

packages/compass-connections/src/index.tsx

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -85,20 +85,17 @@ const CompassConnectionsPlugin = registerHadronPlugin(
8585
{ logger, preferences, connectionStorage, track, globalAppRegistry },
8686
{ addCleanup, cleanup }
8787
) {
88-
const store = configureStore(
89-
initialProps.preloadStorageConnectionInfos ?? null,
90-
{
91-
logger,
92-
preferences,
93-
connectionStorage,
94-
track,
95-
getExtraConnectionData: initialProps.onExtraConnectionDataRequest,
96-
appName: initialProps.appName,
97-
connectFn: initialProps.connectFn,
98-
globalAppRegistry,
99-
onFailToLoadConnections: initialProps.onFailToLoadConnections,
100-
}
101-
);
88+
const store = configureStore(initialProps.preloadStorageConnectionInfos, {
89+
logger,
90+
preferences,
91+
connectionStorage,
92+
track,
93+
getExtraConnectionData: initialProps.onExtraConnectionDataRequest,
94+
appName: initialProps.appName,
95+
connectFn: initialProps.connectFn,
96+
globalAppRegistry,
97+
onFailToLoadConnections: initialProps.onFailToLoadConnections,
98+
});
10299

103100
setTimeout(() => {
104101
void store.dispatch(loadConnections());

packages/compass-connections/src/stores/connections-store-redux.spec.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ describe('CompassConnections store', function () {
6767
.rejects(new Error('loadAll failed'));
6868

6969
renderCompassConnections({
70-
connections: null,
70+
connections: 'no-preload',
7171
connectionStorage,
7272
onFailToLoadConnections: onFailToLoadConnectionsSpy,
7373
});

packages/compass-connections/src/stores/connections-store-redux.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ const INITIAL_STATE: State = {
469469
};
470470

471471
export function getInitialConnectionsStateForConnectionInfos(
472-
connectionInfos: ConnectionInfo[] | null
472+
connectionInfos?: ConnectionInfo[]
473473
): State['connections'] {
474474
if (!connectionInfos) {
475475
// Keep initial state if we're not preloading any connections
@@ -2134,7 +2134,7 @@ export const openSettingsModal = (
21342134
};
21352135

21362136
export function configureStore(
2137-
preloadConnectionInfos: ConnectionInfo[] | null,
2137+
preloadConnectionInfos: ConnectionInfo[] | undefined,
21382138
thunkArg: ThunkExtraArg
21392139
) {
21402140
return createStore(

packages/compass-sidebar/src/components/multiple-connections/sidebar.spec.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import type { RenderWithConnectionsHookResult } from '@mongodb-js/testing-librar
55
import {
66
createPluginTestHelpers,
77
screen,
8-
cleanup,
98
waitFor,
109
within,
1110
userEvent,
@@ -94,7 +93,7 @@ describe('Multiple Connections Sidebar Component', function () {
9493

9594
function doRender(
9695
activeWorkspace: WorkspaceTab | null = null,
97-
connections: ConnectionInfo[] | null = [savedFavoriteConnection],
96+
connections: ConnectionInfo[] | 'no-preload' = [savedFavoriteConnection],
9897
atlasClusterConnectionsOnly: boolean | undefined = undefined
9998
) {
10099
workspace = sinon.spy({
@@ -153,7 +152,6 @@ describe('Multiple Connections Sidebar Component', function () {
153152
}
154153

155154
afterEach(function () {
156-
cleanup();
157155
sinon.restore();
158156
});
159157

@@ -236,8 +234,15 @@ describe('Multiple Connections Sidebar Component', function () {
236234

237235
describe('connections list', function () {
238236
it('should display a loading state while connections are not loaded yet', function () {
239-
doRender(null, null);
237+
doRender(null, 'no-preload');
240238
expect(screen.getByTestId('connections-placeholder')).to.be.visible;
239+
expect(screen.getByRole('searchbox', { name: 'Search' })).to.have.attr(
240+
'aria-disabled',
241+
'true'
242+
);
243+
expect(
244+
screen.getByRole('button', { name: 'Filter connections' })
245+
).to.have.attr('aria-disabled', 'true');
241246
});
242247

243248
context('when there are no connections', function () {

0 commit comments

Comments
 (0)