@@ -441,6 +441,83 @@ describe('InputEditor', function() {
441441 } ) ;
442442 } ) ;
443443
444+
445+ it ( 'should not undo the initial fill' , async function ( ) {
446+
447+ // given - editor starts without value, then receives async prefill
448+ const onChangeSpy = sinon . spy ( ) ;
449+ const prefillValue = '{\n "foo": "bar"\n}' ;
450+
451+ const { container, getByRole, rerender } = renderWithProps ( {
452+ onChange : onChangeSpy
453+ } ) ;
454+
455+ // simulate async prefill arriving
456+ rerender (
457+ < InputEditor
458+ value = { prefillValue }
459+ onChange = { onChangeSpy }
460+ onErrorChange = { ( ) => { } }
461+ />
462+ ) ;
463+
464+ await waitFor ( ( ) => {
465+ expect ( container . textContent ) . to . contain ( '"foo": "bar"' ) ;
466+ } ) ;
467+
468+ // when - try to undo the prefill
469+ const textbox = getByRole ( 'textbox' ) ;
470+ await user . click ( textbox ) ;
471+ await user . keyboard ( undoKeys ) ;
472+
473+ // then - prefill should remain (not undoable)
474+ await new Promise ( resolve => setTimeout ( resolve , 100 ) ) ;
475+ expect ( onChangeSpy ) . to . not . have . been . called ;
476+ expect ( container . textContent ) . to . contain ( '"foo": "bar"' ) ;
477+ } ) ;
478+
479+
480+ it ( 'should not undo past the initial value' , async function ( ) {
481+
482+ // given - editor starts with value, user types something
483+ const onChangeSpy = sinon . spy ( ) ;
484+ const initialValue = '{\n "foo": "bar"\n}' ;
485+
486+ const { container, getByRole } = renderWithProps ( {
487+ value : initialValue ,
488+ onChange : onChangeSpy
489+ } ) ;
490+
491+ const textbox = getByRole ( 'textbox' ) ;
492+ await user . click ( textbox ) ;
493+
494+ // type something
495+ await user . keyboard ( '{ArrowRight}{Enter}"a": 1' ) ;
496+
497+ await waitFor ( ( ) => {
498+ expect ( onChangeSpy ) . to . have . been . called ;
499+ } ) ;
500+
501+ onChangeSpy . resetHistory ( ) ;
502+
503+ // when - undo typing
504+ await user . keyboard ( undoKeys ) ;
505+
506+ await waitFor ( ( ) => {
507+ expect ( onChangeSpy ) . to . have . been . calledWith ( initialValue ) ;
508+ } ) ;
509+
510+ onChangeSpy . resetHistory ( ) ;
511+
512+ // when - undo again (should not go to empty)
513+ await user . keyboard ( undoKeys ) ;
514+
515+ // then - should stay at initial value
516+ await new Promise ( resolve => setTimeout ( resolve , 100 ) ) ;
517+ expect ( onChangeSpy ) . to . not . have . been . called ;
518+ expect ( container . textContent ) . to . contain ( '"foo": "bar"' ) ;
519+ } ) ;
520+
444521 } ) ;
445522
446523} ) ;
0 commit comments