Skip to content

Commit 2d1075c

Browse files
committed
Error banner
1 parent c6512f0 commit 2d1075c

File tree

2 files changed

+83
-3
lines changed

2 files changed

+83
-3
lines changed

packages/compass-collection/src/components/mock-data-generator-modal/mock-data-generator-modal.spec.tsx

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { expect } from 'chai';
2+
import sinon from 'sinon';
23
import React from 'react';
34
import {
45
screen,
@@ -18,6 +19,7 @@ import { default as collectionTabReducer } from '../../modules/collection-tab';
1819
import type { ConnectionInfo } from '@mongodb-js/connection-info';
1920
import type { MockDataSchemaResponse } from '@mongodb-js/compass-generative-ai';
2021
import type { SchemaAnalysisState } from '../../schema-analysis-types';
22+
import * as scriptGenerationUtils from './script-generation-utils';
2123

2224
const defaultSchemaAnalysisState: SchemaAnalysisState = {
2325
status: 'complete',
@@ -765,7 +767,76 @@ describe('MockDataGeneratorModal', () => {
765767
.to.not.exist;
766768
});
767769

768-
// todo: assert that the generated script is displayed in the code block (CLOUDP-333860)
770+
it('shows error banner when script generation fails', async () => {
771+
// Mock the generateScript function to return an error
772+
const generateScriptStub = sinon.stub(
773+
scriptGenerationUtils,
774+
'generateScript'
775+
);
776+
generateScriptStub.returns({
777+
success: false,
778+
error: 'Test error: Invalid faker schema format',
779+
});
780+
781+
try {
782+
await renderModal({
783+
currentStep: MockDataGeneratorStep.GENERATE_DATA,
784+
fakerSchemaGeneration: {
785+
status: 'completed',
786+
fakerSchema: {
787+
name: {
788+
fakerMethod: 'person.firstName',
789+
fakerArgs: [],
790+
probability: 1.0,
791+
mongoType: 'String',
792+
},
793+
},
794+
requestId: 'test-request-id',
795+
},
796+
});
797+
798+
expect(screen.getByRole('alert')).to.exist;
799+
expect(screen.getByText(/Script Generation Failed:/)).to.exist;
800+
expect(screen.getByText(/Test error: Invalid faker schema format/)).to
801+
.exist;
802+
expect(screen.getByText(/Please go back to the start screen/)).to.exist;
803+
804+
const codeBlock = screen.getByText('// Script generation failed.');
805+
expect(codeBlock).to.exist;
806+
} finally {
807+
generateScriptStub.restore();
808+
}
809+
});
810+
811+
it('displays the script when generation succeeds', async () => {
812+
await renderModal({
813+
currentStep: MockDataGeneratorStep.GENERATE_DATA,
814+
fakerSchemaGeneration: {
815+
status: 'completed',
816+
fakerSchema: {
817+
name: {
818+
fakerMethod: 'person.firstName',
819+
fakerArgs: [],
820+
probability: 1.0,
821+
mongoType: 'String',
822+
},
823+
email: {
824+
fakerMethod: 'internet.email',
825+
fakerArgs: [],
826+
probability: 1.0,
827+
mongoType: 'String',
828+
},
829+
},
830+
requestId: 'test-request-id',
831+
},
832+
});
833+
834+
// Check that no error banner is displayed
835+
expect(screen.queryByRole('alert')).to.not.exist;
836+
expect(screen.queryByText('Script generation failed')).to.not.exist;
837+
expect(screen.getByText('firstName')).to.exist; // faker method
838+
expect(screen.getByText('insertMany')).to.exist;
839+
});
769840
});
770841

771842
describe('when rendering the modal in a specific step', () => {

packages/compass-collection/src/components/mock-data-generator-modal/script-screen.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React, { useMemo } from 'react';
22
import {
3+
Banner,
34
Body,
45
Code,
56
Copyable,
@@ -130,10 +131,18 @@ const ScriptScreen = ({
130131
In the directory that you created, create a file named
131132
mockdatascript.js (or any name you'd like).
132133
</Body>
133-
<Code copyable language={Language.JavaScript}>
134+
{!scriptResult.success && (
135+
<Banner variant="danger">
136+
<strong>Script Generation Failed:</strong> {scriptResult.error}
137+
<br />
138+
Please go back to the start screen to re-submit the collection
139+
schema.
140+
</Banner>
141+
)}
142+
<Code copyable={scriptResult.success} language={Language.JavaScript}>
134143
{scriptResult.success
135144
? scriptResult.script
136-
: `// Error generating script: ${scriptResult.error}`}
145+
: '// Script generation failed.'}
137146
</Code>
138147
</section>
139148
<section>

0 commit comments

Comments
 (0)