Skip to content

Commit ad95b7f

Browse files
committed
test(json-crdt-extensions): 💍 add .refVisible() test
1 parent 0f95026 commit ad95b7f

File tree

2 files changed

+50
-10
lines changed

2 files changed

+50
-10
lines changed

src/json-crdt-extensions/peritext/point/Point.ts

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,12 @@ export class Point implements Pick<Stateful, 'refresh'>, Printable {
237237
return;
238238
}
239239

240+
/**
241+
* Returns one character to the left of the point, or `undefined` if there
242+
* is no such character. Skips any deleted characters. Handles absolute points.
243+
*
244+
* @returns A character slice to the left of the point.
245+
*/
240246
public leftChar(): ChunkSlice | undefined {
241247
const str = this.txt.str;
242248
if (this.isAbsEnd()) {
@@ -253,6 +259,12 @@ export class Point implements Pick<Stateful, 'refresh'>, Printable {
253259
return new ChunkSlice(chunk, off, 1);
254260
}
255261

262+
/**
263+
* Returns one character to the right of the point, or `undefined` if there
264+
* is no such character. Skips any deleted characters. Handles absolute points.
265+
*
266+
* @returns A character slice to the right of the point.
267+
*/
256268
public rightChar(): ChunkSlice | undefined {
257269
const str = this.txt.str;
258270
if (this.isAbsStart()) {
@@ -321,16 +333,6 @@ export class Point implements Pick<Stateful, 'refresh'>, Printable {
321333
return !!id && equal(this.id, id);
322334
}
323335

324-
/**
325-
* Modifies the location of the point, such that the spatial location remains
326-
* and anchor remains the same, but ensures that the point references a
327-
* visible (non-deleted) character.
328-
*/
329-
public refVisible(): void {
330-
if (this.anchor === Anchor.Before) this.refBefore();
331-
else this.refAfter();
332-
}
333-
334336
/**
335337
* Sets the point to the absolute start of the string.
336338
*/
@@ -415,6 +417,16 @@ export class Point implements Pick<Stateful, 'refresh'>, Printable {
415417
this.id = this.prevId() || this.txt.str.id;
416418
}
417419

420+
/**
421+
* Modifies the location of the point, such that the spatial location remains
422+
* the same and tries to preserve anchor location, but ensures that the point
423+
* references a visible (not deleted) character.
424+
*/
425+
public refVisible(): void {
426+
if (this.anchor === Anchor.Before) this.refBefore();
427+
else this.refAfter();
428+
}
429+
418430
/**
419431
* Moves point past given number of visible characters. Accepts positive
420432
* and negative distances.

src/json-crdt-extensions/peritext/point/__tests__/Point.spec.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,6 +1019,34 @@ describe('.refAfter()', () => {
10191019
});
10201020
});
10211021

1022+
1023+
describe('.refVisible()', () => {
1024+
test('skips deleted chars, attaches to visible char', () => {
1025+
const {peritext} = setupWithChunkedText();
1026+
peritext.strApi().del(0, peritext.str.length());
1027+
peritext.strApi().ins(0, '123456789');
1028+
const mid1 = peritext.pointAt(4, Anchor.After);
1029+
const mid2 = peritext.pointAt(5, Anchor.Before);
1030+
expect(mid1.leftChar()!.view()).toBe('5');
1031+
expect(mid1.rightChar()!.view()).toBe('6');
1032+
expect(mid2.leftChar()!.view()).toBe('5');
1033+
expect(mid2.rightChar()!.view()).toBe('6');
1034+
const left = peritext.pointAt(2, Anchor.After);
1035+
expect(left.leftChar()!.view()).toBe('3');
1036+
const right = peritext.pointAt(6, Anchor.Before);
1037+
expect(right.rightChar()!.view()).toBe('7');
1038+
peritext.strApi().del(3, 3);
1039+
expect(left.leftChar()!.view()).toBe('3');
1040+
expect(right.rightChar()!.view()).toBe('7');
1041+
expect(mid1.compare(left) > 0).toBe(true);
1042+
mid1.refVisible();
1043+
expect(mid1.compare(left) === 0).toBe(true);
1044+
expect(mid2.compare(right) < 0).toBe(true);
1045+
mid2.refVisible();
1046+
expect(mid2.compare(right) === 0).toBe(true);
1047+
});
1048+
});
1049+
10221050
describe('.move()', () => {
10231051
test('smoke test', () => {
10241052
const {peritext} = setupWithChunkedText();

0 commit comments

Comments
 (0)