Skip to content

Commit ab88fae

Browse files
committed
test(json-crdt-extensions): 💍 add .prevId() edge case tests
1 parent 2cf31d5 commit ab88fae

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,13 +171,11 @@ export class Point implements Pick<Stateful, 'refresh'>, Printable {
171171
* such character.
172172
*/
173173
public prevId(move: number = 1): ITimestampStruct | undefined {
174-
// TODO: add tests for when cursor is at the start.
175174
if (this.isStartOfStr()) return;
176175
let remaining: number = move;
177176
const {id, txt} = this;
178177
const str = txt.str;
179178
let chunk = this.chunk();
180-
// TODO: handle case when cursor starts from end of string.
181179
if (!chunk) return str.id;
182180
if (!chunk.del) {
183181
const offset = id.time - chunk.id.time;

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,28 @@ describe('.prevId()', () => {
539539
p2.nextId(0);
540540
expect(p2.rightChar()!.view()).toBe('4');
541541
});
542+
543+
test('returns undefined, when at start of str', () => {
544+
const {peritext} = setupWithChunkedText();
545+
const point = peritext.pointAtStart();
546+
expect(point.prevId()).toBe(undefined);
547+
});
548+
549+
test('returns undefined, when at first char', () => {
550+
const {peritext} = setupWithChunkedText();
551+
const point1 = peritext.pointAt(0, Anchor.Before);
552+
const point2 = peritext.pointAt(0, Anchor.After);
553+
expect(point1.prevId()).toBe(undefined);
554+
expect(point2.prevId()).toBe(undefined);
555+
});
556+
557+
test('returns last char, when at end of str', () => {
558+
const {peritext} = setupWithChunkedText();
559+
const point1 = peritext.pointAtEnd();
560+
const point2 = peritext.pointAt(9, Anchor.Before);
561+
const id = point1.prevId();
562+
expect(id).toEqual(point2.id);
563+
});
542564
});
543565

544566
describe('.rightChar()', () => {

0 commit comments

Comments
 (0)