@@ -109,6 +109,55 @@ describe('Mermaid Preview Page', () => {
109109 expect ( toast . info ) . toHaveBeenCalledWith ( 'Cleared all content' ) ;
110110 } ) ;
111111
112+ it ( 'does not attempt to render when mermaid code is empty' , async ( ) => {
113+ const mermaid = jest . mocked ( ( await import ( 'mermaid' ) ) . default ) ;
114+
115+ render ( < MermaidViewerPage /> ) ;
116+
117+ const textarea = screen . getByPlaceholderText (
118+ 'Type your Mermaid code here...' ,
119+ ) ;
120+ const clearButton = screen . getByText ( 'Clear' ) ;
121+
122+ // Clear the initial content
123+ fireEvent . click ( clearButton ) ;
124+
125+ // Wait a bit for any potential render attempts
126+ await waitFor ( ( ) => {
127+ expect ( textarea ) . toHaveValue ( '' ) ;
128+ } ) ;
129+
130+ // Check that mermaid.render was not called with empty string
131+ const renderCalls = mermaid . render . mock . calls ;
132+ const emptyRenderCalls = renderCalls . filter ( ( call ) => ! call [ 1 ] ?. trim ( ) ) ;
133+ expect ( emptyRenderCalls . length ) . toBe ( 0 ) ;
134+ } ) ;
135+
136+ it ( 'does not attempt to render when mermaid code is only whitespace' , async ( ) => {
137+ const mermaid = jest . mocked ( ( await import ( 'mermaid' ) ) . default ) ;
138+
139+ render ( < MermaidViewerPage /> ) ;
140+
141+ const textarea = screen . getByPlaceholderText (
142+ 'Type your Mermaid code here...' ,
143+ ) ;
144+
145+ // Set to whitespace only
146+ fireEvent . change ( textarea , { target : { value : ' \n \n ' } } ) ;
147+
148+ // Wait a bit for any potential render attempts
149+ await waitFor ( ( ) => {
150+ expect ( textarea ) . toHaveValue ( ' \n \n ' ) ;
151+ } ) ;
152+
153+ // Check that mermaid.render was not called with whitespace-only string
154+ const renderCalls = mermaid . render . mock . calls ;
155+ const whitespaceRenderCalls = renderCalls . filter (
156+ ( call ) => call [ 1 ] && ! call [ 1 ] . trim ( ) ,
157+ ) ;
158+ expect ( whitespaceRenderCalls . length ) . toBe ( 0 ) ;
159+ } ) ;
160+
112161 it ( 'handles file upload correctly' , async ( ) => {
113162 const toast = jest . mocked ( ( await import ( 'sonner' ) ) . toast ) ;
114163
0 commit comments