Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ describe('CollectionHeaderActions [Component]', function () {
namespace="test.test"
isReadonly={false}
onOpenMockDataModal={sinon.stub()}
hasData={true}
maxNestingDepth={2}
{...props}
/>
</PreferencesProvider>
Expand Down Expand Up @@ -374,5 +376,80 @@ describe('CollectionHeaderActions [Component]', function () {

expect(onOpenMockDataModal).to.have.been.calledOnce;
});

it('should disable Mock Data Generator button when hasData is false', async function () {
mockUseAssignment.returns({
assignment: {
assignmentData: {
variant: 'mockDataGeneratorVariant',
},
},
});

await renderCollectionHeaderActions(
{
namespace: 'test.collection',
isReadonly: false,
hasData: false,
},
{},
atlasConnectionInfo
);

const button = screen.getByTestId(
'collection-header-generate-mock-data-button'
);
expect(button).to.have.attribute('aria-disabled', 'true');
});

it('should enable Mock Data Generator button when hasData is true', async function () {
mockUseAssignment.returns({
assignment: {
assignmentData: {
variant: 'mockDataGeneratorVariant',
},
},
});

await renderCollectionHeaderActions(
{
namespace: 'test.collection',
isReadonly: false,
hasData: true,
},
{},
atlasConnectionInfo
);

const button = screen.getByTestId(
'collection-header-generate-mock-data-button'
);
expect(button).to.not.have.attribute('aria-disabled', 'true');
});

it('should not show Mock Data Generator button for collections with excessive nesting depth', async function () {
mockUseAssignment.returns({
assignment: {
assignmentData: {
variant: 'mockDataGeneratorVariant',
},
},
});

await renderCollectionHeaderActions(
{
namespace: 'test.collection',
isReadonly: false,
hasData: true,
maxNestingDepth: 4, // Exceeds the limit of 3
},
{},
atlasConnectionInfo
);

expect(
screen.queryByTestId('collection-header-generate-mock-data-button')
).to.not.exist;
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ type CollectionHeaderActionsProps = {
sourceName?: string;
sourcePipeline?: unknown[];
onOpenMockDataModal: () => void;
hasData: boolean;
maxNestingDepth: number;
};

const CollectionHeaderActions: React.FunctionComponent<
Expand All @@ -58,6 +60,8 @@ const CollectionHeaderActions: React.FunctionComponent<
sourceName,
sourcePipeline,
onOpenMockDataModal,
hasData,
maxNestingDepth,
}: CollectionHeaderActionsProps) => {
const connectionInfo = useConnectionInfo();
const { id: connectionId, atlasMetadata } = connectionInfo;
Expand All @@ -84,10 +88,8 @@ const CollectionHeaderActions: React.FunctionComponent<
isInMockDataTreatmentVariant &&
atlasMetadata && // Only show in Atlas
!isReadonly && // Don't show for readonly collections (views)
!sourceName; // sourceName indicates it's a view
// TODO: CLOUDP-337090: also filter out overly nested collections

const hasData = true; // TODO: CLOUDP-337090
!sourceName && // sourceName indicates it's a view
maxNestingDepth < 4; // Filter out overly nested collections (4+ levels)
Copy link
Collaborator

@kpamaran kpamaran Sep 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name maxNestingDepth sounds like a configuration that should be holding the 4, but maxNestingDepth is computed from calculateSchemaDepth . It should be renamed to schemaDepth while the name maxNestingDepth should be re-purposed *(or renamed using "limit") to hold the magic value

edit*


return (
<div
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import { getConnectionTitle } from '@mongodb-js/connection-info';
import MockDataGeneratorModal from '../mock-data-generator-modal/mock-data-generator-modal';
import { connect } from 'react-redux';
import { openMockDataGeneratorModal } from '../../modules/collection-tab';
import type { CollectionState } from '../../modules/collection-tab';
import { SCHEMA_ANALYSIS_STATE_COMPLETE } from '../../schema-analysis-types';

const collectionHeaderStyles = css({
padding: spacing[400],
Expand Down Expand Up @@ -62,6 +64,8 @@ type CollectionHeaderProps = {
editViewName?: string;
sourcePipeline?: unknown[];
onOpenMockDataModal: () => void;
hasData: boolean;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename hasData to hasSchemaAnalysisData and maxNestingDepth to something like analyzedSchemaDepth (see my other comment)

maxNestingDepth: number;
};

const getInsightsForPipeline = (pipeline: any[], isAtlas: boolean) => {
Expand Down Expand Up @@ -97,6 +101,8 @@ const CollectionHeader: React.FunctionComponent<CollectionHeaderProps> = ({
editViewName,
sourcePipeline,
onOpenMockDataModal,
hasData,
maxNestingDepth,
}) => {
const darkMode = useDarkMode();
const showInsights = usePreference('showInsights');
Expand Down Expand Up @@ -174,14 +180,32 @@ const CollectionHeader: React.FunctionComponent<CollectionHeaderProps> = ({
sourceName={sourceName}
sourcePipeline={sourcePipeline}
onOpenMockDataModal={onOpenMockDataModal}
hasData={hasData}
maxNestingDepth={maxNestingDepth}
/>
</div>
<MockDataGeneratorModal />
</div>
);
};

const ConnectedCollectionHeader = connect(undefined, {
const mapStateToProps = (state: CollectionState) => {
const { schemaAnalysis } = state;

return {
hasData:
schemaAnalysis.status === SCHEMA_ANALYSIS_STATE_COMPLETE &&
schemaAnalysis.processedSchema &&
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] Remove the check for schemaAnalysis.processedSchema. The typing of schemaAnalysis dictates if the status is complete then processedSchema will always exist. This suggestion applies to schemaAnalysis.schemaMetadata as well

Object.keys(schemaAnalysis.processedSchema).length > 0,
maxNestingDepth:
schemaAnalysis.status === SCHEMA_ANALYSIS_STATE_COMPLETE &&
schemaAnalysis.schemaMetadata
? schemaAnalysis.schemaMetadata.maxNestingDepth
: 0,
};
};

const ConnectedCollectionHeader = connect(mapStateToProps, {
onOpenMockDataModal: openMockDataGeneratorModal,
})(CollectionHeader);

Expand Down
Loading