// The small parts of the document: plain values in Yjs and back, and the // order of entries two people placed at once. import { describe, expect, it } from 'vitest'; import * as Y from 'yjs'; import { fromY, inOrder, toY } from '../src/index.js'; /** A value as the document holds it, inside a document (a Yjs type outside one cannot be read). */ function held(value: Parameters[0]): unknown { const doc = new Y.Doc(); doc.getMap('root').set('value', toY(value)); return doc.getMap('root').get('value'); } describe('plain values in the document', () => { it('leaves out a field that is undefined', () => { const map = held({ a: 1, b: undefined, c: null }) as Y.Map; expect([...map.keys()].sort()).toEqual(['a', 'c']); expect(fromY(map)).toStrictEqual({ a: 1, c: null }); }); it('keeps lists, nested objects, bytes and the values of JSON', () => { const value = { rows: [{ name: 'x', ttl: 3600 }, { name: 'y', ttl: 0 }], on: true, off: false, text: 'รค', none: null, bytes: new Uint8Array([1, 2]) }; const map = held(value) as Y.Map; expect(map.get('rows')).toBeInstanceOf(Y.Array); expect((map.get('rows') as Y.Array).get(0)).toBeInstanceOf(Y.Map); expect(fromY(map)).toStrictEqual(value); }); }); describe('the order of entries', () => { it('orders by place, and two entries given one place by their keys', () => { const doc = new Y.Doc(); const map = doc.getMap>('entries'); for (const [key, order] of [['c', 1], ['b', 0], ['a', 1], ['d', -1]] as const) { const entry = new Y.Map(); entry.set('order', order); map.set(key, entry); } expect(inOrder(map).map(([key]) => key)).toEqual(['d', 'b', 'a', 'c']); }); });