Skip to content

Commit 011c3ca

Browse files
committed
test(json-crdt): 💍 add tests for new eventing methods
1 parent 9897f40 commit 011c3ca

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

src/json-crdt/model/api/__tests__/ModelApi.events.spec.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,3 +196,42 @@ describe('DOM Level 2 events, .et.addEventListener()', () => {
196196
expect(set!.has(ModelChangeType.RESET)).toBe(true);
197197
});
198198
});
199+
200+
describe('fanout', () => {
201+
describe('changes', () => {
202+
test('emits events on document change', async () => {
203+
const doc = Model.withLogicalClock();
204+
const api = doc.api;
205+
let cnt = 0;
206+
api.root({a: {}});
207+
expect(cnt).toBe(0);
208+
api.changes.listen(() => {
209+
cnt++;
210+
});
211+
api.obj([]).set({gg: true});
212+
await Promise.resolve();
213+
expect(cnt).toBe(1);
214+
api.obj(['a']).set({1: 1, 2: 2});
215+
await Promise.resolve();
216+
expect(cnt).toBe(2);
217+
});
218+
219+
test('can have multiple subscribers', async () => {
220+
const doc = Model.withLogicalClock();
221+
const api = doc.api;
222+
let cnt = 0;
223+
api.root({a: {}});
224+
expect(cnt).toBe(0);
225+
api.changes.listen(() => {
226+
cnt++;
227+
});
228+
api.changes.listen(() => {
229+
cnt++;
230+
});
231+
expect(cnt).toBe(0);
232+
api.obj([]).set({gg: true});
233+
await Promise.resolve();
234+
expect(cnt).toBe(2);
235+
});
236+
});
237+
});

src/json-crdt/model/api/__tests__/StringApi.spec.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,55 @@ describe('events', () => {
6363
await Promise.resolve();
6464
expect(cnt).toEqual(1);
6565
});
66+
67+
describe('.changes', () => {
68+
test('can listen to events', async () => {
69+
const doc = Model.withLogicalClock();
70+
const api = doc.api;
71+
api.root('');
72+
const str = api.str([]);
73+
let cnt = 0;
74+
const onView = () => cnt++;
75+
str.ins(0, 'aaa');
76+
expect(cnt).toEqual(0);
77+
const unsubscribe = str.events.changes.listen(onView);
78+
str.ins(0, 'bbb');
79+
await Promise.resolve();
80+
expect(cnt).toEqual(1);
81+
str.ins(0, 'ccc');
82+
await Promise.resolve();
83+
expect(cnt).toEqual(2);
84+
unsubscribe();
85+
str.del(1, 7);
86+
expect(cnt).toEqual(2);
87+
});
88+
});
89+
90+
describe('SyncStore', () => {
91+
test('can listen to events', async () => {
92+
const doc = Model.withLogicalClock();
93+
const api = doc.api;
94+
api.root('');
95+
const str = api.str([]);
96+
let cnt = 0;
97+
const onView = () => cnt++;
98+
str.ins(0, 'aaa');
99+
expect(cnt).toEqual(0);
100+
expect(str.events.getSnapshot()).toEqual('aaa');
101+
const unsubscribe = str.events.subscribe(onView);
102+
str.ins(0, 'bbb');
103+
await Promise.resolve();
104+
expect(str.events.getSnapshot()).toEqual('bbbaaa');
105+
expect(cnt).toEqual(1);
106+
str.ins(0, 'ccc');
107+
await Promise.resolve();
108+
expect(str.events.getSnapshot()).toEqual('cccbbbaaa');
109+
expect(cnt).toEqual(2);
110+
unsubscribe();
111+
str.del(1, 7);
112+
expect(cnt).toEqual(2);
113+
});
114+
});
66115
});
116+
117+

0 commit comments

Comments
 (0)