Skip to content

Commit 5472802

Browse files
committed
test(json-crdt-extensions): 💍 add .nextId() edge case tests
1 parent 3c6831f commit 5472802

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

src/json-crdt-extensions/peritext/Peritext.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ export class Peritext implements Printable {
2424
return this.point(id, anchor);
2525
}
2626

27+
public pointAtStart(): Point {
28+
return this.point(this.str.id, Anchor.After);
29+
}
30+
31+
public pointAtEnd(): Point {
32+
return this.point(this.str.id, Anchor.Before);
33+
}
34+
2735
// ---------------------------------------------------------------- Printable
2836

2937
public toString(tab: string = ''): string {

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,11 @@ export class Point implements Pick<Stateful, 'refresh'>, Printable {
125125
* @returns Next visible ID in string.
126126
*/
127127
public nextId(move: number = 1): ITimestampStruct | undefined {
128-
// TODO: add tests for when cursor is at the end.
129128
if (this.isEndOfStr()) return;
130129
let remaining: number = move;
131130
const {id, txt} = this;
132131
const str = txt.str;
133132
let chunk: StringChunk | undefined;
134-
// TODO: add tests for when cursor starts from start of string.
135133
if (this.isStartOfStr()) {
136134
chunk = str.first();
137135
while (chunk && chunk.del) chunk = str.next(chunk);

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,25 @@ describe('.nextId()', () => {
416416
p2.prevId(0);
417417
expect(p2.leftChar()!.view()).toBe('3');
418418
});
419+
420+
test('returns undefined, when at end of str', () => {
421+
const {peritext} = setupWithChunkedText();
422+
const point = peritext.pointAtEnd();
423+
expect(point.nextId()).toBe(undefined);
424+
});
425+
426+
test('returns undefined, when at last char', () => {
427+
const {peritext} = setupWithChunkedText();
428+
const point = peritext.pointAt(8, Anchor.Before);
429+
expect(point.nextId()).toBe(undefined);
430+
});
431+
432+
test('returns first char, when at start of str', () => {
433+
const {peritext, chunk1} = setupWithChunkedText();
434+
const point = peritext.pointAtStart();
435+
const id = point.nextId();
436+
expect(id).toEqual(chunk1.id);
437+
});
419438
});
420439

421440
describe('.prevId()', () => {

0 commit comments

Comments
 (0)