-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(compass-connections): useConnectionSupports(connectionId, connec…
…tionFeature) hook COMPASS-8213 (#6185) * useConnectionSupports hook * tests * use a selector
- Loading branch information
Showing
3 changed files
with
207 additions
and
1 deletion.
There are no files selected for viewing
161 changes: 161 additions & 0 deletions
161
packages/compass-connections/src/hooks/use-connection-supports.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
import { useConnectionSupports } from './use-connection-supports'; | ||
import { type ConnectionInfo } from '@mongodb-js/connection-storage/provider'; | ||
import { expect } from 'chai'; | ||
import { renderHookWithConnections } from '../test'; | ||
|
||
const mockConnections: ConnectionInfo[] = [ | ||
{ | ||
id: 'no-atlasMetadata', | ||
connectionOptions: { | ||
connectionString: 'mongodb://foo', | ||
}, | ||
}, | ||
{ | ||
id: 'host-cluster', | ||
connectionOptions: { | ||
connectionString: 'mongodb://foo', | ||
}, | ||
atlasMetadata: { | ||
orgId: 'orgId', | ||
projectId: 'projectId', | ||
clusterName: 'clusterName', | ||
regionalBaseUrl: 'https://example.com', | ||
clusterId: 'clusterId', | ||
clusterType: 'host', | ||
instanceSize: 'M10', | ||
}, | ||
}, | ||
{ | ||
id: 'free-cluster', | ||
connectionOptions: { | ||
connectionString: 'mongodb://foo', | ||
}, | ||
atlasMetadata: { | ||
orgId: 'orgId', | ||
projectId: 'projectId', | ||
clusterName: 'clusterName', | ||
regionalBaseUrl: 'https://example.com', | ||
clusterId: 'clusterId', | ||
clusterType: 'replicaSet', | ||
instanceSize: 'M0', | ||
}, | ||
}, | ||
{ | ||
id: 'serverless-cluster', | ||
connectionOptions: { | ||
connectionString: 'mongodb://foo', | ||
}, | ||
atlasMetadata: { | ||
orgId: 'orgId', | ||
projectId: 'projectId', | ||
clusterName: 'clusterName', | ||
regionalBaseUrl: 'https://example.com', | ||
clusterId: 'clusterId', | ||
clusterType: 'serverless', | ||
instanceSize: 'SERVERLESS_V2', | ||
}, | ||
}, | ||
{ | ||
id: 'dedicated-replicaSet', | ||
connectionOptions: { | ||
connectionString: 'mongodb://foo', | ||
}, | ||
atlasMetadata: { | ||
orgId: 'orgId', | ||
projectId: 'projectId', | ||
clusterName: 'clusterName', | ||
regionalBaseUrl: 'https://example.com', | ||
clusterId: 'clusterId', | ||
clusterType: 'replicaSet', | ||
instanceSize: 'M10', | ||
}, | ||
}, | ||
{ | ||
id: 'dedicated-sharded', | ||
connectionOptions: { | ||
connectionString: 'mongodb://foo', | ||
}, | ||
atlasMetadata: { | ||
orgId: 'orgId', | ||
projectId: 'projectId', | ||
clusterName: 'clusterName', | ||
regionalBaseUrl: 'https://example.com', | ||
clusterId: 'clusterId', | ||
clusterType: 'cluster', | ||
instanceSize: 'M10', | ||
}, | ||
}, | ||
]; | ||
|
||
describe('useConnectionSupports', function () { | ||
it('should return false if the connection does not exist', function () { | ||
const { result } = renderHookWithConnections( | ||
() => useConnectionSupports('does-not-exist', 'rollingIndexCreation'), | ||
{ | ||
connections: mockConnections, | ||
} | ||
); | ||
expect(result.current).to.equal(false); | ||
}); | ||
|
||
it('should return false if the connection has no atlasMetadata', function () { | ||
const { result } = renderHookWithConnections( | ||
() => useConnectionSupports('no-atlasMetadata', 'rollingIndexCreation'), | ||
{ | ||
connections: mockConnections, | ||
} | ||
); | ||
expect(result.current).to.equal(false); | ||
}); | ||
|
||
it('should return false for host cluster type', function () { | ||
const { result } = renderHookWithConnections( | ||
() => useConnectionSupports('host-cluster', 'rollingIndexCreation'), | ||
{ | ||
connections: mockConnections, | ||
} | ||
); | ||
expect(result.current).to.equal(false); | ||
}); | ||
|
||
it('should return false for serverless cluster type', function () { | ||
const { result } = renderHookWithConnections( | ||
() => useConnectionSupports('serverless-cluster', 'rollingIndexCreation'), | ||
{ | ||
connections: mockConnections, | ||
} | ||
); | ||
expect(result.current).to.equal(false); | ||
}); | ||
|
||
it('should return false for free/shared tier clusters', function () { | ||
const { result } = renderHookWithConnections( | ||
() => useConnectionSupports('free-cluster', 'rollingIndexCreation'), | ||
{ | ||
connections: mockConnections, | ||
} | ||
); | ||
expect(result.current).to.equal(false); | ||
}); | ||
|
||
it('should return true for dedicated replicaSet clusters', function () { | ||
const { result } = renderHookWithConnections( | ||
() => | ||
useConnectionSupports('dedicated-replicaSet', 'rollingIndexCreation'), | ||
{ | ||
connections: mockConnections, | ||
} | ||
); | ||
expect(result.current).to.equal(true); | ||
}); | ||
|
||
it('should return true for dedicated sharded clusters', function () { | ||
const { result } = renderHookWithConnections( | ||
() => useConnectionSupports('dedicated-sharded', 'rollingIndexCreation'), | ||
{ | ||
connections: mockConnections, | ||
} | ||
); | ||
expect(result.current).to.equal(true); | ||
}); | ||
}); |
45 changes: 45 additions & 0 deletions
45
packages/compass-connections/src/hooks/use-connection-supports.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { useSelector } from '../stores/store-context'; | ||
import type { ConnectionState } from '../stores/connections-store-redux'; | ||
|
||
// only one for now | ||
type ConnectionFeature = 'rollingIndexCreation'; | ||
|
||
function isFreeOrSharedTierCluster(instanceSize: string | undefined): boolean { | ||
if (!instanceSize) { | ||
return false; | ||
} | ||
|
||
return ['M0', 'M2', 'M5'].includes(instanceSize); | ||
} | ||
|
||
function supportsRollingIndexCreation(connection: ConnectionState) { | ||
const atlasMetadata = connection.info?.atlasMetadata; | ||
|
||
if (!atlasMetadata) { | ||
return false; | ||
} | ||
|
||
const { clusterType, instanceSize } = atlasMetadata; | ||
return ( | ||
!isFreeOrSharedTierCluster(instanceSize) && | ||
(clusterType === 'cluster' || clusterType === 'replicaSet') | ||
); | ||
} | ||
export function useConnectionSupports( | ||
connectionId: string, | ||
connectionFeature: ConnectionFeature | ||
): boolean { | ||
return useSelector((state) => { | ||
const connection = state.connections.byId[connectionId]; | ||
|
||
if (!connection) { | ||
return false; | ||
} | ||
|
||
if (connectionFeature === 'rollingIndexCreation') { | ||
return supportsRollingIndexCreation(connection); | ||
} | ||
|
||
return false; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters