11import { expect } from 'chai' ;
2+ import sinon from 'sinon' ;
23import React from 'react' ;
34import {
45 screen ,
@@ -18,6 +19,7 @@ import { default as collectionTabReducer } from '../../modules/collection-tab';
1819import type { ConnectionInfo } from '@mongodb-js/connection-info' ;
1920import type { MockDataSchemaResponse } from '@mongodb-js/compass-generative-ai' ;
2021import type { SchemaAnalysisState } from '../../schema-analysis-types' ;
22+ import * as scriptGenerationUtils from './script-generation-utils' ;
2123
2224const 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 ( / S c r i p t G e n e r a t i o n F a i l e d : / ) ) . to . exist ;
800+ expect ( screen . getByText ( / T e s t e r r o r : I n v a l i d f a k e r s c h e m a f o r m a t / ) ) . to
801+ . exist ;
802+ expect ( screen . getByText ( / P l e a s e g o b a c k t o t h e s t a r t s c r e e n / ) ) . 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' , ( ) => {
0 commit comments