test_event.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. import json
  2. import pytest
  3. from reflex import event
  4. from reflex.event import Event, EventHandler, EventSpec
  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. def test_event_redirect():
  67. """Test the event redirect function."""
  68. spec = event.redirect("/path")
  69. assert isinstance(spec, EventSpec)
  70. assert spec.handler.fn.__qualname__ == "_redirect"
  71. assert spec.args == (("path", "/path"),)
  72. assert format.format_event(spec) == 'E("_redirect", {path:"/path"})'
  73. spec = event.redirect(Var.create_safe("path"))
  74. assert format.format_event(spec) == 'E("_redirect", {path:path})'
  75. def test_event_console_log():
  76. """Test the event console log function."""
  77. spec = event.console_log("message")
  78. assert isinstance(spec, EventSpec)
  79. assert spec.handler.fn.__qualname__ == "_console"
  80. assert spec.args == (("message", "message"),)
  81. assert format.format_event(spec) == 'E("_console", {message:"message"})'
  82. spec = event.console_log(Var.create_safe("message"))
  83. assert format.format_event(spec) == 'E("_console", {message:message})'
  84. def test_event_window_alert():
  85. """Test the event window alert function."""
  86. spec = event.window_alert("message")
  87. assert isinstance(spec, EventSpec)
  88. assert spec.handler.fn.__qualname__ == "_alert"
  89. assert spec.args == (("message", "message"),)
  90. assert format.format_event(spec) == 'E("_alert", {message:"message"})'
  91. spec = event.window_alert(Var.create_safe("message"))
  92. assert format.format_event(spec) == 'E("_alert", {message:message})'
  93. def test_set_focus():
  94. """Test the event set focus function."""
  95. spec = event.set_focus("input1")
  96. assert isinstance(spec, EventSpec)
  97. assert spec.handler.fn.__qualname__ == "_set_focus"
  98. assert spec.args == (("ref", Var.create_safe("ref_input1")),)
  99. assert format.format_event(spec) == 'E("_set_focus", {ref:ref_input1})'
  100. spec = event.set_focus("input1")
  101. assert format.format_event(spec) == 'E("_set_focus", {ref:ref_input1})'
  102. def test_set_value():
  103. """Test the event window alert function."""
  104. spec = event.set_value("input1", "")
  105. assert isinstance(spec, EventSpec)
  106. assert spec.handler.fn.__qualname__ == "_set_value"
  107. assert spec.args == (
  108. ("ref", Var.create_safe("ref_input1")),
  109. ("value", ""),
  110. )
  111. assert format.format_event(spec) == 'E("_set_value", {ref:ref_input1,value:""})'
  112. spec = event.set_value("input1", Var.create_safe("message"))
  113. assert (
  114. format.format_event(spec) == 'E("_set_value", {ref:ref_input1,value:message})'
  115. )
  116. def test_set_cookie():
  117. """Test the event set_cookie."""
  118. spec = event.set_cookie("testkey", "testvalue")
  119. assert isinstance(spec, EventSpec)
  120. assert spec.handler.fn.__qualname__ == "_set_cookie"
  121. assert spec.args == (
  122. ("key", "testkey"),
  123. ("value", "testvalue"),
  124. )
  125. assert (
  126. format.format_event(spec)
  127. == 'E("_set_cookie", {key:"testkey",value:"testvalue"})'
  128. )
  129. def test_remove_cookie():
  130. """Test the event remove_cookie."""
  131. spec = event.remove_cookie("testkey")
  132. assert isinstance(spec, EventSpec)
  133. assert spec.handler.fn.__qualname__ == "_remove_cookie"
  134. assert spec.args == (("key", "testkey"), ("options", {}))
  135. assert (
  136. format.format_event(spec) == 'E("_remove_cookie", {key:"testkey",options:{}})'
  137. )
  138. def test_remove_cookie_with_options():
  139. """Test the event remove_cookie with options."""
  140. options = {
  141. "path": "/",
  142. "domain": "example.com",
  143. "secure": True,
  144. "sameSite": "strict",
  145. }
  146. spec = event.remove_cookie("testkey", options)
  147. assert isinstance(spec, EventSpec)
  148. assert spec.handler.fn.__qualname__ == "_remove_cookie"
  149. assert spec.args == (("key", "testkey"), ("options", options))
  150. assert (
  151. format.format_event(spec)
  152. == f'E("_remove_cookie", {{key:"testkey",options:{json.dumps(options)}}})'
  153. )
  154. def test_set_local_storage():
  155. """Test the event set_local_storage."""
  156. spec = event.set_local_storage("testkey", "testvalue")
  157. assert isinstance(spec, EventSpec)
  158. assert spec.handler.fn.__qualname__ == "_set_local_storage"
  159. assert spec.args == (
  160. ("key", "testkey"),
  161. ("value", "testvalue"),
  162. )
  163. assert (
  164. format.format_event(spec)
  165. == 'E("_set_local_storage", {key:"testkey",value:"testvalue"})'
  166. )
  167. def test_clear_local_storage():
  168. """Test the event clear_local_storage."""
  169. spec = event.clear_local_storage()
  170. assert isinstance(spec, EventSpec)
  171. assert spec.handler.fn.__qualname__ == "_clear_local_storage"
  172. assert not spec.args
  173. assert format.format_event(spec) == 'E("_clear_local_storage", {})'
  174. def test_remove_local_storage():
  175. """Test the event remove_local_storage."""
  176. spec = event.remove_local_storage("testkey")
  177. assert isinstance(spec, EventSpec)
  178. assert spec.handler.fn.__qualname__ == "_remove_local_storage"
  179. assert spec.args == (("key", "testkey"),)
  180. assert format.format_event(spec) == 'E("_remove_local_storage", {key:"testkey"})'