Skip to content

Commit

Permalink
test(json-crdt-extensions): 💍 add test for collapsed slice insertion
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed May 9, 2024
1 parent 178381a commit c5962d6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
6 changes: 1 addition & 5 deletions src/json-crdt-extensions/peritext/overlay/Overlay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,6 @@ export class Overlay<T = string> implements Printable, Stateful {
}

private insSlice(slice: Slice<T>): [start: OverlayPoint<T>, end: OverlayPoint<T>] {
// TODO: Test cases where the inserted slice is collapsed to one point.
const x0 = slice.start;
const x1 = slice.end;
const [start, isStartNew] = this.upsertPoint(x0);
Expand All @@ -395,10 +394,7 @@ export class Overlay<T = string> implements Printable, Stateful {
let curr: OverlayPoint<T> | undefined = start;
do curr.addLayer(slice);
while ((curr = next(curr)) && curr !== end);
} else {
// TODO: review if this is needed:
start.addMarker(slice);
}
} else start.addMarker(slice);
return [start, end];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,18 @@ describe('slices', () => {
expect(points.length).toBe(4);
});

test('can insert a slice, which is collapsed to a point', () => {
const {peritext} = setup();
peritext.editor.cursor.setAt(3);
const [slice] = peritext.editor.saved.insStack('em', {emphasis: true});
peritext.overlay.refresh();
const [point] = [...peritext.overlay.points()];
expect(point.layers.length).toBe(0);
expect(point.markers.length).toBe(2);
expect(point.markers.find((m) => m === peritext.editor.cursor)).toBe(peritext.editor.cursor);
expect(point.markers.find((m) => m === slice)).toBe(slice);
});

test('intersecting slice chunks point to two slices', () => {
const {peritext} = setup();
peritext.editor.cursor.setAt(2, 2);
Expand Down

0 comments on commit c5962d6

Please sign in to comment.