@@ -117,8 +117,11 @@ describe("DynamicJsonForm Complex Fields", () => {
117117 const input = screen . getByRole ( "textbox" ) ;
118118 expect ( input ) . toHaveProperty ( "type" , "textarea" ) ;
119119 const buttons = screen . getAllByRole ( "button" ) ;
120- expect ( buttons ) . toHaveLength ( 1 ) ;
121- expect ( buttons [ 0 ] ) . toHaveProperty ( "textContent" , "Format JSON" ) ;
120+ expect ( buttons ) . toHaveLength ( 2 ) ; // Copy JSON + Format JSON
121+ const copyButton = screen . getByRole ( "button" , { name : / c o p y j s o n / i } ) ;
122+ const formatButton = screen . getByRole ( "button" , { name : / f o r m a t j s o n / i } ) ;
123+ expect ( copyButton ) . toBeTruthy ( ) ;
124+ expect ( formatButton ) . toBeTruthy ( ) ;
122125 } ) ;
123126
124127 it ( "should pass changed values to onChange" , ( ) => {
@@ -137,3 +140,72 @@ describe("DynamicJsonForm Complex Fields", () => {
137140 } ) ;
138141 } ) ;
139142} ) ;
143+
144+ describe ( "DynamicJsonForm Copy JSON Functionality" , ( ) => {
145+ const mockWriteText = jest . fn ( ( ) => Promise . resolve ( ) ) ;
146+
147+ beforeEach ( ( ) => {
148+ jest . clearAllMocks ( ) ;
149+ Object . assign ( navigator , {
150+ clipboard : {
151+ writeText : mockWriteText ,
152+ } ,
153+ } ) ;
154+ } ) ;
155+
156+ const renderFormInJsonMode = ( props = { } ) => {
157+ const defaultProps = {
158+ schema : {
159+ type : "object" ,
160+ properties : {
161+ nested : { oneOf : [ { type : "string" } , { type : "integer" } ] } ,
162+ } ,
163+ } as unknown as JsonSchemaType ,
164+ value : { nested : "test value" } ,
165+ onChange : jest . fn ( ) ,
166+ } ;
167+ return render ( < DynamicJsonForm { ...defaultProps } { ...props } /> ) ;
168+ } ;
169+
170+ describe ( "Copy JSON Button" , ( ) => {
171+ it ( "should render Copy JSON button when in JSON mode" , ( ) => {
172+ renderFormInJsonMode ( ) ;
173+
174+ const copyButton = screen . getByRole ( "button" , { name : "Copy JSON" } ) ;
175+ expect ( copyButton ) . toBeTruthy ( ) ;
176+ } ) ;
177+
178+ it ( "should not render Copy JSON button when not in JSON mode" , ( ) => {
179+ const simpleSchema = {
180+ type : "string" as const ,
181+ description : "Test string field" ,
182+ } ;
183+
184+ render (
185+ < DynamicJsonForm
186+ schema = { simpleSchema }
187+ value = "test"
188+ onChange = { jest . fn ( ) }
189+ /> ,
190+ ) ;
191+
192+ const copyButton = screen . queryByRole ( "button" , { name : "Copy JSON" } ) ;
193+ expect ( copyButton ) . toBeNull ( ) ;
194+ } ) ;
195+
196+ it ( "should copy JSON to clipboard when clicked" , async ( ) => {
197+ const testValue = { nested : "test value" , number : 42 } ;
198+
199+ renderFormInJsonMode ( { value : testValue } ) ;
200+
201+ const copyButton = screen . getByRole ( "button" , { name : "Copy JSON" } ) ;
202+ fireEvent . click ( copyButton ) ;
203+
204+ await waitFor ( ( ) => {
205+ expect ( mockWriteText ) . toHaveBeenCalledWith (
206+ JSON . stringify ( testValue , null , 2 ) ,
207+ ) ;
208+ } ) ;
209+ } ) ;
210+ } ) ;
211+ } ) ;
0 commit comments