From d6bcde4540b69a8ad1083b2aad6519315df2d56c Mon Sep 17 00:00:00 2001 From: Szymon Cofalik Date: Mon, 6 Mar 2017 15:18:07 +0100 Subject: [PATCH] Tests: Added tests for MarkerDelta transformations. --- tests/model/delta/transform/markerdelta.js | 153 +++++++++++++++++++-- 1 file changed, 145 insertions(+), 8 deletions(-) diff --git a/tests/model/delta/transform/markerdelta.js b/tests/model/delta/transform/markerdelta.js index 7b344864d..f5332c1cb 100644 --- a/tests/model/delta/transform/markerdelta.js +++ b/tests/model/delta/transform/markerdelta.js @@ -19,7 +19,10 @@ import { expectDelta, getFilledDocument, getMarkerDelta, - getSplitDelta + getSplitDelta, + getMergeDelta, + getWrapDelta, + getUnwrapDelta } from '../../../model/delta/transform/_utils/utils'; describe( 'transform', () => { @@ -33,16 +36,16 @@ describe( 'transform', () => { } ); describe( 'MarkerDelta by', () => { - let markerDelta; + describe( 'SplitDelta', () => { + let markerDelta; - beforeEach( () => { - const oldRange = new Range( new Position( root, [ 3, 0 ] ), new Position( root, [ 3, 3 ] ) ); - const newRange = new Range( new Position( root, [ 3, 3, 3, 2 ] ), new Position( root, [ 3, 3, 3, 6 ] ) ); + beforeEach( () => { + const oldRange = new Range( new Position( root, [ 3, 0 ] ), new Position( root, [ 3, 3 ] ) ); + const newRange = new Range( new Position( root, [ 3, 3, 3, 2 ] ), new Position( root, [ 3, 3, 3, 6 ] ) ); - markerDelta = getMarkerDelta( 'name', oldRange, newRange, baseVersion ); - } ); + markerDelta = getMarkerDelta( 'name', oldRange, newRange, baseVersion ); + } ); - describe( 'SplitDelta', () => { it( 'split inside oldRange', () => { let splitDelta = getSplitDelta( new Position( root, [ 3, 1 ] ), new Element( 'div' ), 3, baseVersion ); let transformed = transform( markerDelta, splitDelta ); @@ -93,5 +96,139 @@ describe( 'transform', () => { } ); } ); } ); + + describe( 'MergeDelta', () => { + it( 'collapsed marker in merged element', () => { + // MarkerDelta with collapsed range, which changes from the beginning of merged element to the end. + const oldRange = new Range( new Position( root, [ 3, 3, 3, 0 ] ) ); + const newRange = new Range( new Position( root, [ 3, 3, 3, 12 ] ) ); + + const markerDelta = getMarkerDelta( 'name', oldRange, newRange, baseVersion ); + + // MergeDelta merges the element in which is collapsed marker range with the previous element. + const mergeDelta = getMergeDelta( new Position( root, [ 3, 3, 3 ] ), 4, 12, baseVersion ); + + const transformed = transform( markerDelta, mergeDelta ); + + // It is expected, that ranges in MarkerDelta got correctly transformed: + // from start of merged element to the place where merged nodes where moved in the previous element, + // from end of merged element to the end of previous element. + const expectedOldRange = new Range( new Position( root, [ 3, 3, 2, 4 ] ), new Position( root, [ 3, 3, 2, 4 ] ) ); + const expectedNewRange = new Range( new Position( root, [ 3, 3, 2, 16 ] ), new Position( root, [ 3, 3, 2, 16 ] ) ); + + expectDelta( transformed[ 0 ], { + type: MarkerDelta, + operations: [ + { + type: MarkerOperation, + name: 'name', + oldRange: expectedOldRange, + newRange: expectedNewRange, + baseVersion: baseVersion + 2 + } + ] + } ); + } ); + } ); + + describe( 'WrapDelta', () => { + it( 'ranges intersecting with wrapped range', () => { + // MarkerDelta with ranges that intersects with wrapped range. + const oldRange = new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 1, 2 ] ) ); + const newRange = new Range( new Position( root, [ 1, 2 ] ), new Position( root, [ 2, 2 ] ) ); + + const markerDelta = getMarkerDelta( 'name', oldRange, newRange, baseVersion ); + + // Wrap delta wraps element on position ( root [ 1 ] ), which intersects with both `oldRange` and `newRange`. + const wrapElement = new Element( 'w' ); + const wrapRange = new Range( new Position( root, [ 1 ] ), new Position( root, [ 2 ] ) ); + const wrapDelta = getWrapDelta( wrapRange, wrapElement, baseVersion ); + + const transformed = transform( markerDelta, wrapDelta ); + + // It is expected, that ranges in MarkerDelta got correctly transformed: + // `oldRange` end is in wrapped element, + // `newRange` start is in wrapped element. + const expectedOldRange = new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 1, 0, 2 ] ) ); + const expectedNewRange = new Range( new Position( root, [ 1, 0, 2 ] ), new Position( root, [ 2, 2 ] ) ); + + expectDelta( transformed[ 0 ], { + type: MarkerDelta, + operations: [ + { + type: MarkerOperation, + name: 'name', + oldRange: expectedOldRange, + newRange: expectedNewRange, + baseVersion: baseVersion + 2 + } + ] + } ); + } ); + } ); + + describe( 'UnwrapDelta', () => { + it( 'ranges intersecting with unwrapped element', () => { + // MarkerDelta with ranges that intersects with unwrapped element. + const oldRange = new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 1, 0, 2 ] ) ); + const newRange = new Range( new Position( root, [ 1, 0, 2 ] ), new Position( root, [ 2, 2 ] ) ); + + const markerDelta = getMarkerDelta( 'name', oldRange, newRange, baseVersion ); + + // Unwrap delta unwraps element on position ( root [ 1, 0 ] ), which intersects with both `oldRange` and `newRange`. + const unwrapPosition = new Position( root, [ 1, 0 ] ); + const unwrapDelta = getUnwrapDelta( unwrapPosition, 4, baseVersion ); + + const transformed = transform( markerDelta, unwrapDelta ); + + // It is expected, that ranges in MarkerDelta got correctly transformed. + const expectedOldRange = new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 1, 2 ] ) ); + const expectedNewRange = new Range( new Position( root, [ 1, 2 ] ), new Position( root, [ 2, 2 ] ) ); + + expectDelta( transformed[ 0 ], { + type: MarkerDelta, + operations: [ + { + type: MarkerOperation, + name: 'name', + oldRange: expectedOldRange, + newRange: expectedNewRange, + baseVersion: baseVersion + 2 + } + ] + } ); + } ); + + it( 'ranges intersecting with unwrapped element #2', () => { + // MarkerDelta with ranges that intersects with unwrapped element. + const oldRange = new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 1, 2 ] ) ); + const newRange = new Range( new Position( root, [ 1, 2 ] ), new Position( root, [ 2, 2 ] ) ); + + const markerDelta = getMarkerDelta( 'name', oldRange, newRange, baseVersion ); + + // Unwrap delta unwraps element on position ( root [ 1 ] ), which intersects with both `oldRange` and `newRange`. + const unwrapPosition = new Position( root, [ 1 ] ); + const unwrapDelta = getUnwrapDelta( unwrapPosition, 4, baseVersion ); + + const transformed = transform( markerDelta, unwrapDelta ); + + // It is expected, that ranges in MarkerDelta got correctly transformed. + const expectedOldRange = new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 3 ] ) ); + const expectedNewRange = new Range( new Position( root, [ 3 ] ), new Position( root, [ 5, 2 ] ) ); + + expectDelta( transformed[ 0 ], { + type: MarkerDelta, + operations: [ + { + type: MarkerOperation, + name: 'name', + oldRange: expectedOldRange, + newRange: expectedNewRange, + baseVersion: baseVersion + 2 + } + ] + } ); + } ); + } ); } ); } );