Skip to content

Commit d650489

Browse files
committed
test(json-crdt): 💍 add node recrusion tests
1 parent b2e28f5 commit d650489

File tree

2 files changed

+146
-0
lines changed

2 files changed

+146
-0
lines changed

src/json-crdt-patch/PatchBuilder.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ export class PatchBuilder {
210210
* Set value of a "val" object.
211211
*
212212
* @returns ID of the new operation.
213+
* @todo Rename to "insVal".
213214
*/
214215
public setVal(obj: ITimestampStruct, val: ITimestampStruct): ITimestampStruct {
215216
this.pad();
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
import {Model} from '../model';
2+
3+
describe('recursive node references are not allowed', () => {
4+
describe('arr', () => {
5+
describe('ins_arr', () => {
6+
test('reference to parent object in same patch', () => {
7+
const model = Model.withLogicalClock();
8+
const builder = model.api.builder;
9+
const objId = builder.obj();
10+
const arrId = builder.arr();
11+
builder.insObj(objId, [['arr', arrId]]);
12+
builder.insArr(arrId, arrId, [builder.const(1), objId]);
13+
builder.root(objId);
14+
const patch1 = builder.flush();
15+
model.applyPatch(patch1);
16+
expect(model.view()).toStrictEqual({arr: [1]});
17+
});
18+
19+
test('reference to parent object in second patch', () => {
20+
const model = Model.withLogicalClock();
21+
const builder = model.api.builder;
22+
const objId = builder.obj();
23+
const arrId = builder.arr();
24+
builder.insObj(objId, [['arr', arrId]]);
25+
builder.insArr(arrId, arrId, [builder.const(1)]);
26+
builder.root(objId);
27+
const patch1 = builder.flush();
28+
model.applyPatch(patch1);
29+
builder.insArr(arrId, arrId, [objId]);
30+
const patch2 = builder.flush();
31+
model.applyPatch(patch2);
32+
expect(model.view()).toStrictEqual({arr: [1]});
33+
});
34+
});
35+
});
36+
37+
describe('obj', () => {
38+
describe('ins_obj', () => {
39+
test('reference to object self in own key', () => {
40+
const model = Model.withLogicalClock();
41+
const builder = model.api.builder;
42+
const objId = builder.obj();
43+
builder.insObj(objId, [
44+
['con', builder.const(2)],
45+
['obj', objId],
46+
]);
47+
builder.root(objId);
48+
const patch1 = builder.flush();
49+
model.applyPatch(patch1);
50+
expect(model.view()).toStrictEqual({con: 2});
51+
});
52+
53+
test('reference to object self in own key in separate patch', () => {
54+
const model = Model.withLogicalClock();
55+
const builder = model.api.builder;
56+
const objId = builder.obj();
57+
builder.insObj(objId, [
58+
['con', builder.const(2)],
59+
]);
60+
builder.root(objId);
61+
const patch1 = builder.flush();
62+
model.applyPatch(patch1);
63+
builder.insObj(objId, [
64+
['obj', objId],
65+
]);
66+
const patch2 = builder.flush();
67+
model.applyPatch(patch2);
68+
expect(model.view()).toStrictEqual({con: 2});
69+
});
70+
});
71+
});
72+
73+
describe('vec', () => {
74+
describe('ins_vec', () => {
75+
test('reference to parent object in same patch', () => {
76+
const model = Model.withLogicalClock();
77+
const builder = model.api.builder;
78+
const vecId = builder.vec();
79+
builder.insVec(vecId, [
80+
[0, builder.const(1)],
81+
[1, vecId],
82+
]);
83+
builder.root(vecId);
84+
const patch1 = builder.flush();
85+
model.applyPatch(patch1);
86+
expect(model.view()).toStrictEqual([1]);
87+
});
88+
89+
test('reference to parent object in second patch', () => {
90+
const model = Model.withLogicalClock();
91+
const builder = model.api.builder;
92+
const vecId = builder.vec();
93+
builder.insVec(vecId, [
94+
[0, builder.const(1)],
95+
[1, vecId],
96+
]);
97+
builder.root(vecId);
98+
const patch1 = builder.flush();
99+
model.applyPatch(patch1);
100+
builder.insVec(vecId, [
101+
[1, vecId],
102+
]);
103+
const patch2 = builder.flush();
104+
model.applyPatch(patch2);
105+
expect(model.view()).toStrictEqual([1]);
106+
});
107+
});
108+
});
109+
110+
describe('val', () => {
111+
describe('new_val', () => {
112+
test('reference to parent object in same patch', () => {
113+
const model = Model.withLogicalClock();
114+
const builder = model.api.builder;
115+
const objId = builder.obj();
116+
const valId = builder.val();
117+
builder.insObj(objId, [['val', valId]]);
118+
builder.root(objId);
119+
const patch1 = builder.flush();
120+
model.applyPatch(patch1);
121+
console.log(model + '');
122+
console.log(model.view());
123+
expect(model.view()).toStrictEqual({val: undefined});
124+
});
125+
});
126+
127+
describe('ins_val', () => {
128+
test('reference to parent object in second patch', () => {
129+
const model = Model.withLogicalClock();
130+
const builder = model.api.builder;
131+
const objId = builder.obj();
132+
const conId = builder.const(3);
133+
const valId = builder.val(conId);
134+
builder.insObj(objId, [['val', valId]]);
135+
builder.root(objId);
136+
const patch1 = builder.flush();
137+
model.applyPatch(patch1);
138+
builder.setVal(valId, objId);
139+
const patch2 = builder.flush();
140+
model.applyPatch(patch2);
141+
expect(model.view()).toStrictEqual({val: 3});
142+
});
143+
});
144+
});
145+
});

0 commit comments

Comments
 (0)