Skip to content

Commit

Permalink
fix(compass-connections): recognize empty name during duplication (#6076
Browse files Browse the repository at this point in the history
)


---------

Co-authored-by: Sergey Petushkov <[email protected]>
  • Loading branch information
paula-stacho and gribnoysup authored Jul 31, 2024
1 parent 0a9492a commit 83dfacb
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
35 changes: 35 additions & 0 deletions packages/compass-connections/src/stores/connections-store.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,41 @@ describe('useConnections', function () {
).to.exist;
});

it('should create a name if there is none', async function () {
const connectionWithoutName: ConnectionInfo = {
id: '123',
connectionOptions: {
connectionString: 'mongodb://localhost:27017',
},
favorite: {
name: '',
color: 'color2',
},
savedConnectionType: 'recent',
};
mockConnectionStorage = new InMemoryConnectionStorage([
connectionWithoutName,
]);
const connections = renderHookWithContext();

// Waiting for connections to load first
await waitFor(() => {
expect(connections.current.recentConnections).to.have.lengthOf.gt(0);
});

await connections.current.duplicateConnection(connectionWithoutName.id, {
autoDuplicate: true,
});

await waitFor(() => {
expect(
connections.current.recentConnections.find((info) => {
return info.favorite.name === 'localhost:27017 (1)';
})
).to.exist.and.to.have.nested.property('favorite.color', 'color2');
});
});

it('should only look for copy number at the end of the connection name', async function () {
const connections = renderHookWithContext();

Expand Down
7 changes: 5 additions & 2 deletions packages/compass-connections/src/stores/connections-store.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -511,8 +511,11 @@ export function useConnections({
id: new UUID().toString(),
};

if (!duplicate.favorite) {
duplicate.favorite = { name: getConnectionTitle(duplicate) };
if (!duplicate.favorite || !duplicate.favorite.name) {
duplicate.favorite = {
...duplicate.favorite,
name: getConnectionTitle(duplicate),
};
}

const [nameWithoutCount, copyCount] = parseFavoriteNameToNameAndCopyCount(
Expand Down

0 comments on commit 83dfacb

Please sign in to comment.