How to write basic tests?

Hi,
I am trying to write some basic tests where I want to test my sync code. My sync logic subscribes to observeDeep to listen to the events. But my tests do not work, so I made the following sample:

test('should remove property from object', () => {
    const doc1 = new Y.Doc();
    const doc2 = new Y.Doc();

    const root1 = doc1.getMap();
    const root2 = doc2.getMap();

    doc1.transact(() => {
        root1.set('Key', '1');
    });

    doc2.transact(() => {
        root2.set('Key', '1');
    });

    doc1.on('update', (update: Uint8Array) => {
        console.log('Update');

        Y.logUpdate(update);
        Y.applyUpdate(doc2, update);
    });

    root2.observeDeep((events: any) => {
        console.log('EVENTS');
        console.log(JSON.stringify(events, undefined, 2));
    });


    doc1.transact(() => {
        root1.set('Key', '42');
    });
});

If I comment the initial two set calls out, I get an event, otherwise I do not receive an event. So I am confused what I am actually doing wrong.

But I receive updates:

I’m not totally clear what you’re asking. If you write a failing test with assertions, that would be easier to give feedback on.

Or, clearly describe the actual behavior and expected behavior.

The sync was the problem, not the tests. But it turned out that I need a two way sync:

    const doc1 = new Y.Doc();
    const doc2 = new Y.Doc();

    doc1.on('update', (update: Uint8Array) => {
        Y.applyUpdate(doc2, update); 
    });

    doc2.on('update', (update: Uint8Array) => {
        Y.applyUpdate(doc1, update); 
    });

Then it works fine.Problem is solved.

1 Like