Skip to content

Commit

Permalink
Add undo manager test
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbrochart committed May 27, 2024
1 parent e6e7b58 commit 46c94cf
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
15 changes: 15 additions & 0 deletions tests/test_pycrdt_yjs.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,18 @@ async def test_pycrdt_yjs_1(yws_server, yws_provider, yjs_client):
await ystate_change.wait()
assert ycells.to_py() == [{"metadata": {"foo": "bar"}, "source": "1 + 2"}]
assert ystate.to_py() == {"state": {"dirty": False}}


@pytest.mark.parametrize("yjs_client", [2], indirect=True)
async def test_pycrdt_yjs_2(yws_server, yws_provider, yjs_client):
ydoc, _ = yws_provider
ydoc["map0"] = map0 = Map()
map0_change = watch(map0)
await map0_change.wait()
assert map0.get("key0") == "val0"
# client is synced, let's make a change
map0["key1"] = "val1"
# wait for client to undo the change
map0_change = watch(map0)
await map0_change.wait()
assert "key1" not in map0
29 changes: 29 additions & 0 deletions tests/yjs_client_2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const Y = require('yjs')
const WebsocketProvider = require('y-websocket').WebsocketProvider
const ws = require('ws')

const port = process.argv[2]
const ydoc = new Y.Doc()

const wsProvider = new WebsocketProvider(
`ws://127.0.0.1:${port}`, 'my-roomname',
ydoc,
{ WebSocketPolyfill: ws }
)

wsProvider.on('sync', () => {
const map0 = ydoc.getMap('map0')
const undoManager = new Y.UndoManager(map0, {
trackedOrigins: new Set([wsProvider]),
})
map0.set('key0', 'val0')
undoManager.clear()

function undo() {
undoManager.undo()
}

map0.observe((event, transaction) => {
setTimeout(undo, 100)
})
})

0 comments on commit 46c94cf

Please sign in to comment.