@@ -88,12 +88,12 @@ describe( 'LiveRange', () => {
8888 range . detach ( ) ;
8989 } ) ;
9090
91- it ( 'should fire change event with proper data when it is changed' , ( ) => {
91+ it ( 'should fire change:range event with proper data when its boundaries are changed' , ( ) => {
9292 const live = new LiveRange ( new Position ( root , [ 0 , 1 , 4 ] ) , new Position ( root , [ 0 , 2 , 2 ] ) ) ;
9393 const copy = Range . createFromRange ( live ) ;
9494
9595 const spy = sinon . spy ( ) ;
96- live . on ( 'change' , spy ) ;
96+ live . on ( 'change:range ' , spy ) ;
9797
9898 const moveSource = new Position ( root , [ 2 ] ) ;
9999 const moveRange = new Range ( new Position ( root , [ 0 , 2 ] ) , new Position ( root , [ 0 , 3 ] ) ) ;
@@ -118,18 +118,48 @@ describe( 'LiveRange', () => {
118118 expect ( spy . args [ 0 ] [ 2 ] . sourcePosition . isEqual ( moveSource ) ) . to . be . true ;
119119 } ) ;
120120
121+ it ( 'should fire change:content event with proper data when content inside the range has changed' , ( ) => {
122+ const live = new LiveRange ( new Position ( root , [ 1 ] ) , new Position ( root , [ 3 ] ) ) ;
123+
124+ const spy = sinon . spy ( ) ;
125+ live . on ( 'change:content' , spy ) ;
126+
127+ const moveSource = new Position ( root , [ 2 , 0 ] ) ;
128+ const moveRange = new Range ( new Position ( root , [ 4 , 0 ] ) , new Position ( root , [ 4 , 2 ] ) ) ;
129+
130+ const changes = {
131+ range : moveRange ,
132+ sourcePosition : moveSource
133+ } ;
134+ const batch = { } ;
135+
136+ doc . fire ( 'change' , 'move' , changes , batch ) ;
137+
138+ expect ( spy . calledOnce ) . to . be . true ;
139+
140+ // First parameter available in event should be a range that is equal to the live range before the live range changed.
141+ // We compare to the `live` range, because boundaries should not have changed.
142+ expect ( spy . args [ 0 ] [ 1 ] . isEqual ( live ) ) . to . be . true ;
143+
144+ // Second parameter is an object with data about model changes that caused the live range to change.
145+ expect ( spy . args [ 0 ] [ 2 ] . type ) . to . equal ( 'move' ) ;
146+ expect ( spy . args [ 0 ] [ 2 ] . batch ) . to . equal ( batch ) ;
147+ expect ( spy . args [ 0 ] [ 2 ] . range . isEqual ( moveRange ) ) . to . be . true ;
148+ expect ( spy . args [ 0 ] [ 2 ] . sourcePosition . isEqual ( moveSource ) ) . to . be . true ;
149+ } ) ;
150+
121151 // Examples may seem weird when you compare them with the tree structure generated at the beginning of tests.
122152 // Since change event is fired _after_ operation is executed on tree model, you have to imagine that generated
123153 // structure is representing what is _after_ operation is executed. So live LiveRange properties are describing
124154 // virtual tree that is not existing anymore and event ranges are operating on the tree generated above.
125- describe ( 'should get transformed if' , ( ) => {
155+ describe ( 'should get transformed and fire change:range if' , ( ) => {
126156 let live , spy ;
127157
128158 beforeEach ( ( ) => {
129159 live = new LiveRange ( new Position ( root , [ 0 , 1 , 4 ] ) , new Position ( root , [ 0 , 2 , 2 ] ) ) ;
130160
131161 spy = sinon . spy ( ) ;
132- live . on ( 'change' , spy ) ;
162+ live . on ( 'change:range ' , spy ) ;
133163 } ) ;
134164
135165 afterEach ( ( ) => {
@@ -556,35 +586,114 @@ describe( 'LiveRange', () => {
556586 } ) ;
557587 } ) ;
558588
559- describe ( 'should not get transformed if' , ( ) => {
560- let otherRoot , spy , live , clone ;
561-
562- before ( ( ) => {
563- otherRoot = doc . createRoot ( '$root' , 'otherRoot' ) ;
564- } ) ;
589+ describe ( 'should not get transformed but fire change:content' , ( ) => {
590+ let spy , live , clone ;
565591
566592 beforeEach ( ( ) => {
567593 live = new LiveRange ( new Position ( root , [ 0 , 1 , 4 ] ) , new Position ( root , [ 0 , 2 , 2 ] ) ) ;
568594 clone = Range . createFromRange ( live ) ;
569595
570596 spy = sinon . spy ( ) ;
571- live . on ( 'change' , spy ) ;
597+ live . on ( 'change:content ' , spy ) ;
572598 } ) ;
573599
574600 afterEach ( ( ) => {
575601 live . detach ( ) ;
576602 } ) ;
577603
578604 describe ( 'insertion' , ( ) => {
579- it ( 'is in the same parent as range start and after it ' , ( ) => {
605+ it ( 'inside the range' , ( ) => {
580606 const insertRange = new Range ( new Position ( root , [ 0 , 1 , 7 ] ) , new Position ( root , [ 0 , 1 , 9 ] ) ) ;
581607
582608 doc . fire ( 'change' , 'insert' , { range : insertRange } , null ) ;
583609
584610 expect ( live . isEqual ( clone ) ) . to . be . true ;
585- expect ( spy . called ) . to . be . false ;
611+ expect ( spy . calledOnce ) . to . be . true ;
612+ } ) ;
613+ } ) ;
614+
615+ describe ( 'range move' , ( ) => {
616+ it ( 'inside the range' , ( ) => {
617+ const moveSource = new Position ( root , [ 4 ] ) ;
618+ const moveRange = new Range ( new Position ( root , [ 0 , 1 , 7 ] ) , new Position ( root , [ 0 , 1 , 9 ] ) ) ;
619+
620+ const changes = {
621+ range : moveRange ,
622+ sourcePosition : moveSource
623+ } ;
624+ doc . fire ( 'change' , 'move' , changes , null ) ;
625+
626+ expect ( live . isEqual ( clone ) ) . to . be . true ;
627+ expect ( spy . calledOnce ) . to . be . true ;
628+ } ) ;
629+
630+ it ( 'from the range' , ( ) => {
631+ const moveSource = new Position ( root , [ 0 , 1 , 6 ] ) ;
632+ const moveRange = new Range ( new Position ( root , [ 4 , 0 ] ) , new Position ( root , [ 4 , 3 ] ) ) ;
633+
634+ const changes = {
635+ range : moveRange ,
636+ sourcePosition : moveSource
637+ } ;
638+ doc . fire ( 'change' , 'move' , changes , null ) ;
639+
640+ expect ( live . isEqual ( clone ) ) . to . be . true ;
641+ expect ( spy . calledOnce ) . to . be . true ;
586642 } ) ;
587643
644+ it ( 'from the beginning of range' , ( ) => {
645+ const moveSource = new Position ( root , [ 0 , 1 , 4 ] ) ;
646+ const moveRange = new Range ( new Position ( root , [ 4 , 0 ] ) , new Position ( root , [ 4 , 3 ] ) ) ;
647+
648+ const changes = {
649+ range : moveRange ,
650+ sourcePosition : moveSource
651+ } ;
652+ doc . fire ( 'change' , 'move' , changes , null ) ;
653+
654+ expect ( live . isEqual ( clone ) ) . to . be . true ;
655+ expect ( spy . calledOnce ) . to . be . true ;
656+ } ) ;
657+
658+ it ( 'from the range to the range' , ( ) => {
659+ live . end . path = [ 0 , 1 , 12 ] ;
660+
661+ const moveSource = new Position ( root , [ 0 , 1 , 6 ] ) ;
662+ const moveRange = new Range ( new Position ( root , [ 0 , 1 , 8 ] ) , new Position ( root , [ 0 , 1 , 10 ] ) ) ;
663+
664+ const changes = {
665+ range : moveRange ,
666+ sourcePosition : moveSource
667+ } ;
668+ doc . fire ( 'change' , 'move' , changes , null ) ;
669+
670+ expect ( live . start . path ) . to . deep . equal ( [ 0 , 1 , 4 ] ) ;
671+ expect ( live . end . path ) . to . deep . equal ( [ 0 , 1 , 12 ] ) ;
672+ expect ( spy . calledOnce ) . to . be . true ;
673+ } ) ;
674+ } ) ;
675+ } ) ;
676+
677+ describe ( 'should not get transformed and not fire change event if' , ( ) => {
678+ let otherRoot , spy , live , clone ;
679+
680+ before ( ( ) => {
681+ otherRoot = doc . createRoot ( '$root' , 'otherRoot' ) ;
682+ } ) ;
683+
684+ beforeEach ( ( ) => {
685+ live = new LiveRange ( new Position ( root , [ 0 , 1 , 4 ] ) , new Position ( root , [ 0 , 2 , 2 ] ) ) ;
686+ clone = Range . createFromRange ( live ) ;
687+
688+ spy = sinon . spy ( ) ;
689+ live . on ( 'change' , spy ) ;
690+ } ) ;
691+
692+ afterEach ( ( ) => {
693+ live . detach ( ) ;
694+ } ) ;
695+
696+ describe ( 'insertion' , ( ) => {
588697 it ( 'is in the same parent as range end and after it' , ( ) => {
589698 const insertRange = new Range ( new Position ( root , [ 0 , 2 , 7 ] ) , new Position ( root , [ 0 , 2 , 9 ] ) ) ;
590699
@@ -614,20 +723,6 @@ describe( 'LiveRange', () => {
614723 } ) ;
615724
616725 describe ( 'range move' , ( ) => {
617- it ( 'is to the same parent as range start and after it' , ( ) => {
618- const moveSource = new Position ( root , [ 4 ] ) ;
619- const moveRange = new Range ( new Position ( root , [ 0 , 1 , 7 ] ) , new Position ( root , [ 0 , 1 , 9 ] ) ) ;
620-
621- const changes = {
622- range : moveRange ,
623- sourcePosition : moveSource
624- } ;
625- doc . fire ( 'change' , 'move' , changes , null ) ;
626-
627- expect ( live . isEqual ( clone ) ) . to . be . true ;
628- expect ( spy . called ) . to . be . false ;
629- } ) ;
630-
631726 it ( 'is to the same parent as range end and after it' , ( ) => {
632727 const moveSource = new Position ( root , [ 4 ] ) ;
633728 const moveRange = new Range ( new Position ( root , [ 0 , 2 , 3 ] ) , new Position ( root , [ 0 , 2 , 5 ] ) ) ;
@@ -656,20 +751,6 @@ describe( 'LiveRange', () => {
656751 expect ( spy . called ) . to . be . false ;
657752 } ) ;
658753
659- it ( 'is from the same parent as range start and after it' , ( ) => {
660- const moveSource = new Position ( root , [ 0 , 1 , 6 ] ) ;
661- const moveRange = new Range ( new Position ( root , [ 4 , 0 ] ) , new Position ( root , [ 4 , 3 ] ) ) ;
662-
663- const changes = {
664- range : moveRange ,
665- sourcePosition : moveSource
666- } ;
667- doc . fire ( 'change' , 'move' , changes , null ) ;
668-
669- expect ( live . isEqual ( clone ) ) . to . be . true ;
670- expect ( spy . called ) . to . be . false ;
671- } ) ;
672-
673754 it ( 'is from the same parent as range end and after it' , ( ) => {
674755 const moveSource = new Position ( root , [ 0 , 2 , 4 ] ) ;
675756 const moveRange = new Range ( new Position ( root , [ 4 , 0 ] ) , new Position ( root , [ 4 , 2 ] ) ) ;
@@ -725,23 +806,6 @@ describe( 'LiveRange', () => {
725806 expect ( live . isEqual ( clone ) ) . to . be . true ;
726807 expect ( spy . called ) . to . be . false ;
727808 } ) ;
728-
729- it ( 'is inside live range and points to live range' , ( ) => {
730- live . end . path = [ 0 , 1 , 12 ] ;
731-
732- const moveSource = new Position ( root , [ 0 , 1 , 6 ] ) ;
733- const moveRange = new Range ( new Position ( root , [ 0 , 1 , 8 ] ) , new Position ( root , [ 0 , 1 , 10 ] ) ) ;
734-
735- const changes = {
736- range : moveRange ,
737- sourcePosition : moveSource
738- } ;
739- doc . fire ( 'change' , 'move' , changes , null ) ;
740-
741- expect ( live . start . path ) . to . deep . equal ( [ 0 , 1 , 4 ] ) ;
742- expect ( live . end . path ) . to . deep . equal ( [ 0 , 1 , 12 ] ) ;
743- expect ( spy . calledOnce ) . to . be . false ;
744- } ) ;
745809 } ) ;
746810 } ) ;
747811} ) ;
0 commit comments