Skip to content

Commit 3aed795

Browse files
authored
chore(connections): autosave favorite info on connect for existing connections COMPASS-8185 (#6165)
chore(connections): autosave favorite info on connect for existing connections
1 parent 03dfb33 commit 3aed795

File tree

2 files changed

+43
-16
lines changed

2 files changed

+43
-16
lines changed

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -819,11 +819,23 @@ const reducer: Reducer<State, Action> = (state = INITIAL_STATE, action) => {
819819
state.connections,
820820
action.connectionInfo.id,
821821
{
822-
// For new connections, update the state with new info right away (we
823-
// will also save it to the storage at the end)
824-
...(isNewConnection(state, action.connectionInfo.id) && {
825-
info: action.connectionInfo,
826-
}),
822+
...(isNewConnection(state, action.connectionInfo.id)
823+
? {
824+
// For new connections, update the state with new info right
825+
// away (we will also save it to the storage at the end)
826+
info: action.connectionInfo,
827+
}
828+
: {
829+
info: {
830+
// For existing connections only update favorite info when
831+
// connection starts. That way it immediately updates in UI
832+
// and then also gets saved at the end of successfull
833+
// connection
834+
favorite: action.connectionInfo.favorite,
835+
savedConnectionType:
836+
action.connectionInfo.savedConnectionType,
837+
},
838+
}),
827839
status: 'connecting',
828840
error: null,
829841
}

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

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const mockConnections = [
1919
favorite: {
2020
name: 'turtles',
2121
},
22-
savedConnectionType: 'favorite',
22+
savedConnectionType: 'favorite' as const,
2323
},
2424
{
2525
id: 'oranges',
@@ -29,7 +29,7 @@ const mockConnections = [
2929
favorite: {
3030
name: 'peaches',
3131
},
32-
savedConnectionType: 'favorite',
32+
savedConnectionType: 'favorite' as const,
3333
},
3434
];
3535

@@ -242,7 +242,7 @@ describe('useConnections', function () {
242242
describe(`when multiple connections ${
243243
multipleConnectionsEnabled ? 'enabled' : 'disabled'
244244
}`, function () {
245-
it('should NOT update existing connection with new props when existing connection is successfull', async function () {
245+
it('should only update favorite info for existing connection with new props when existing connection is successfull', async function () {
246246
const { result, connectionStorage } = renderHookWithConnections(
247247
useConnections,
248248
{
@@ -256,13 +256,28 @@ describe('useConnections', function () {
256256

257257
await result.current.connect({
258258
...mockConnections[0],
259+
connectionOptions: {
260+
...mockConnections[0].connectionOptions,
261+
connectionString: 'mongodb://foobar',
262+
},
259263
favorite: { name: 'foobar' },
260264
});
261265

262-
// Connection in the storage wasn't updated
263-
expect(
264-
await connectionStorage.load({ id: mockConnections[0].id })
265-
).to.have.nested.property('favorite.name', 'turtles');
266+
const storedConnection = await connectionStorage.load({
267+
id: mockConnections[0].id,
268+
});
269+
270+
// Connection string in the storage wasn't updated
271+
expect(storedConnection).to.have.nested.property(
272+
'connectionOptions.connectionString',
273+
'mongodb://turtle'
274+
);
275+
276+
// Connection favorite name was updated
277+
expect(storedConnection).to.have.nested.property(
278+
'favorite.name',
279+
'foobar'
280+
);
266281
});
267282

268283
it('should not update existing connection if connection failed', async function () {
@@ -366,7 +381,7 @@ describe('useConnections', function () {
366381
favorite: {
367382
name: 'peaches (50) peaches',
368383
},
369-
savedConnectionType: 'favorite',
384+
savedConnectionType: 'favorite' as const,
370385
};
371386

372387
await result.current.saveEditedConnection(newConnection);
@@ -387,7 +402,7 @@ describe('useConnections', function () {
387402

388403
const updatedConnection = {
389404
...mockConnections[0],
390-
savedConnectionType: 'recent',
405+
savedConnectionType: 'recent' as const,
391406
};
392407

393408
await result.current.saveEditedConnection(updatedConnection);
@@ -487,7 +502,7 @@ describe('useConnections', function () {
487502
name: '',
488503
color: 'color2',
489504
},
490-
savedConnectionType: 'recent',
505+
savedConnectionType: 'recent' as const,
491506
},
492507
],
493508
preferences: defaultPreferences,
@@ -515,7 +530,7 @@ describe('useConnections', function () {
515530
favorite: {
516531
name: 'peaches (50) peaches',
517532
},
518-
savedConnectionType: 'favorite',
533+
savedConnectionType: 'favorite' as const,
519534
};
520535

521536
const { result, connectionsStore } = renderHookWithConnections(

0 commit comments

Comments
 (0)