Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add undo manager test #48

Merged
merged 1 commit into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
})
})
Loading