Просмотр исходного кода

[Tests] add unit test for event (#614)

Xiaojing Chen 2 лет назад
Родитель
Сommit
b4c3cb5eb3
1 измененных файлов с 19 добавлено и 0 удалено
  1. 19 0
      tests/test_event.py

+ 19 - 0
tests/test_event.py

@@ -1,3 +1,7 @@
+import json
+
+import pytest
+
 from pynecone import event
 from pynecone.event import Event, EventHandler, EventSpec
 from pynecone.var import Var
@@ -48,6 +52,21 @@ def test_call_event_handler():
     assert event_spec.local_args == ()
     assert event_spec.args == (("arg1", "first"), ("arg2", "second"))
 
+    first, second = 123, "456"
+    handler = EventHandler(fn=test_fn_with_args)
+    event_spec = handler(first, second)  # type: ignore
+
+    assert event_spec.handler == handler
+    assert event_spec.local_args == ()
+    assert event_spec.args == (
+        ("arg1", json.dumps(first, ensure_ascii=False)),
+        ("arg2", json.dumps(second, ensure_ascii=False)),
+    )
+
+    handler = EventHandler(fn=test_fn_with_args)
+    with pytest.raises(TypeError):
+        handler(test_fn)  # type: ignore
+
 
 def test_event_redirect():
     """Test the event redirect function."""