test_event.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. import json
  2. import pytest
  3. from reflex import event
  4. from reflex.event import Event, EventHandler, EventSpec, fix_events
  5. from reflex.utils import format
  6. from reflex.vars import Var
  7. def make_var(value) -> Var:
  8. """Make a variable.
  9. Args:
  10. value: The value of the var.
  11. Returns:
  12. The var.
  13. """
  14. var = Var.create(value)
  15. assert var is not None
  16. return var
  17. def test_create_event():
  18. """Test creating an event."""
  19. event = Event(token="token", name="state.do_thing", payload={"arg": "value"})
  20. assert event.token == "token"
  21. assert event.name == "state.do_thing"
  22. assert event.payload == {"arg": "value"}
  23. def test_call_event_handler():
  24. """Test that calling an event handler creates an event spec."""
  25. def test_fn():
  26. pass
  27. test_fn.__qualname__ = "test_fn"
  28. def test_fn_with_args(_, arg1, arg2):
  29. pass
  30. test_fn_with_args.__qualname__ = "test_fn_with_args"
  31. handler = EventHandler(fn=test_fn)
  32. event_spec = handler()
  33. assert event_spec.handler == handler
  34. assert event_spec.args == ()
  35. assert format.format_event(event_spec) == 'E("test_fn", {})'
  36. handler = EventHandler(fn=test_fn_with_args)
  37. event_spec = handler(make_var("first"), make_var("second"))
  38. # Test passing vars as args.
  39. assert event_spec.handler == handler
  40. assert event_spec.args == (("arg1", "first"), ("arg2", "second"))
  41. assert (
  42. format.format_event(event_spec)
  43. == 'E("test_fn_with_args", {arg1:first,arg2:second})'
  44. )
  45. # Passing args as strings should format differently.
  46. event_spec = handler("first", "second") # type: ignore
  47. assert (
  48. format.format_event(event_spec)
  49. == 'E("test_fn_with_args", {arg1:"first",arg2:"second"})'
  50. )
  51. first, second = 123, "456"
  52. handler = EventHandler(fn=test_fn_with_args)
  53. event_spec = handler(first, second) # type: ignore
  54. assert (
  55. format.format_event(event_spec)
  56. == 'E("test_fn_with_args", {arg1:123,arg2:"456"})'
  57. )
  58. assert event_spec.handler == handler
  59. assert event_spec.args == (
  60. ("arg1", format.json_dumps(first)),
  61. ("arg2", format.json_dumps(second)),
  62. )
  63. handler = EventHandler(fn=test_fn_with_args)
  64. with pytest.raises(TypeError):
  65. handler(test_fn) # type: ignore
  66. @pytest.mark.parametrize(
  67. ("arg1", "arg2"),
  68. (
  69. (1, 2),
  70. (1, "2"),
  71. ({"a": 1}, {"b": 2}),
  72. ),
  73. )
  74. def test_fix_events(arg1, arg2):
  75. """Test that chaining an event handler with args formats the payload correctly.
  76. Args:
  77. arg1: The first arg passed to the handler.
  78. arg2: The second arg passed to the handler.
  79. """
  80. def test_fn_with_args(_, arg1, arg2):
  81. pass
  82. test_fn_with_args.__qualname__ = "test_fn_with_args"
  83. handler = EventHandler(fn=test_fn_with_args)
  84. event_spec = handler(arg1, arg2)
  85. event = fix_events([event_spec], token="foo")[0]
  86. assert event.name == test_fn_with_args.__qualname__
  87. assert event.token == "foo"
  88. assert event.payload == {"arg1": arg1, "arg2": arg2}
  89. def test_event_redirect():
  90. """Test the event redirect function."""
  91. spec = event.redirect("/path")
  92. assert isinstance(spec, EventSpec)
  93. assert spec.handler.fn.__qualname__ == "_redirect"
  94. assert spec.args == (("path", "/path"),)
  95. assert format.format_event(spec) == 'E("_redirect", {path:"/path"})'
  96. spec = event.redirect(Var.create_safe("path"))
  97. assert format.format_event(spec) == 'E("_redirect", {path:path})'
  98. def test_event_console_log():
  99. """Test the event console log function."""
  100. spec = event.console_log("message")
  101. assert isinstance(spec, EventSpec)
  102. assert spec.handler.fn.__qualname__ == "_console"
  103. assert spec.args == (("message", "message"),)
  104. assert format.format_event(spec) == 'E("_console", {message:"message"})'
  105. spec = event.console_log(Var.create_safe("message"))
  106. assert format.format_event(spec) == 'E("_console", {message:message})'
  107. def test_event_window_alert():
  108. """Test the event window alert function."""
  109. spec = event.window_alert("message")
  110. assert isinstance(spec, EventSpec)
  111. assert spec.handler.fn.__qualname__ == "_alert"
  112. assert spec.args == (("message", "message"),)
  113. assert format.format_event(spec) == 'E("_alert", {message:"message"})'
  114. spec = event.window_alert(Var.create_safe("message"))
  115. assert format.format_event(spec) == 'E("_alert", {message:message})'
  116. def test_set_focus():
  117. """Test the event set focus function."""
  118. spec = event.set_focus("input1")
  119. assert isinstance(spec, EventSpec)
  120. assert spec.handler.fn.__qualname__ == "_set_focus"
  121. assert spec.args == (("ref", Var.create_safe("ref_input1")),)
  122. assert format.format_event(spec) == 'E("_set_focus", {ref:ref_input1})'
  123. spec = event.set_focus("input1")
  124. assert format.format_event(spec) == 'E("_set_focus", {ref:ref_input1})'
  125. def test_set_value():
  126. """Test the event window alert function."""
  127. spec = event.set_value("input1", "")
  128. assert isinstance(spec, EventSpec)
  129. assert spec.handler.fn.__qualname__ == "_set_value"
  130. assert spec.args == (
  131. ("ref", Var.create_safe("ref_input1")),
  132. ("value", ""),
  133. )
  134. assert format.format_event(spec) == 'E("_set_value", {ref:ref_input1,value:""})'
  135. spec = event.set_value("input1", Var.create_safe("message"))
  136. assert (
  137. format.format_event(spec) == 'E("_set_value", {ref:ref_input1,value:message})'
  138. )
  139. def test_set_cookie():
  140. """Test the event set_cookie."""
  141. spec = event.set_cookie("testkey", "testvalue")
  142. assert isinstance(spec, EventSpec)
  143. assert spec.handler.fn.__qualname__ == "_set_cookie"
  144. assert spec.args == (
  145. ("key", "testkey"),
  146. ("value", "testvalue"),
  147. )
  148. assert (
  149. format.format_event(spec)
  150. == 'E("_set_cookie", {key:"testkey",value:"testvalue"})'
  151. )
  152. def test_remove_cookie():
  153. """Test the event remove_cookie."""
  154. spec = event.remove_cookie("testkey")
  155. assert isinstance(spec, EventSpec)
  156. assert spec.handler.fn.__qualname__ == "_remove_cookie"
  157. assert spec.args == (("key", "testkey"), ("options", {}))
  158. assert (
  159. format.format_event(spec) == 'E("_remove_cookie", {key:"testkey",options:{}})'
  160. )
  161. def test_remove_cookie_with_options():
  162. """Test the event remove_cookie with options."""
  163. options = {
  164. "path": "/",
  165. "domain": "example.com",
  166. "secure": True,
  167. "sameSite": "strict",
  168. }
  169. spec = event.remove_cookie("testkey", options)
  170. assert isinstance(spec, EventSpec)
  171. assert spec.handler.fn.__qualname__ == "_remove_cookie"
  172. assert spec.args == (("key", "testkey"), ("options", options))
  173. assert (
  174. format.format_event(spec)
  175. == f'E("_remove_cookie", {{key:"testkey",options:{json.dumps(options)}}})'
  176. )
  177. def test_set_local_storage():
  178. """Test the event set_local_storage."""
  179. spec = event.set_local_storage("testkey", "testvalue")
  180. assert isinstance(spec, EventSpec)
  181. assert spec.handler.fn.__qualname__ == "_set_local_storage"
  182. assert spec.args == (
  183. ("key", "testkey"),
  184. ("value", "testvalue"),
  185. )
  186. assert (
  187. format.format_event(spec)
  188. == 'E("_set_local_storage", {key:"testkey",value:"testvalue"})'
  189. )
  190. def test_clear_local_storage():
  191. """Test the event clear_local_storage."""
  192. spec = event.clear_local_storage()
  193. assert isinstance(spec, EventSpec)
  194. assert spec.handler.fn.__qualname__ == "_clear_local_storage"
  195. assert not spec.args
  196. assert format.format_event(spec) == 'E("_clear_local_storage", {})'
  197. def test_remove_local_storage():
  198. """Test the event remove_local_storage."""
  199. spec = event.remove_local_storage("testkey")
  200. assert isinstance(spec, EventSpec)
  201. assert spec.handler.fn.__qualname__ == "_remove_local_storage"
  202. assert spec.args == (("key", "testkey"),)
  203. assert format.format_event(spec) == 'E("_remove_local_storage", {key:"testkey"})'