test_debounce.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. """Test that DebounceInput collapses nested forms."""
  2. import pytest
  3. import reflex as rx
  4. from reflex.components.core.debounce import DEFAULT_DEBOUNCE_TIMEOUT
  5. from reflex.state import BaseState
  6. from reflex.vars.base import LiteralVar, Var
  7. def test_create_no_child():
  8. """DebounceInput raises RuntimeError if no child is provided."""
  9. with pytest.raises(RuntimeError):
  10. _ = rx.debounce_input()
  11. def test_create_no_child_recursive():
  12. """DebounceInput raises RuntimeError if no child is provided."""
  13. with pytest.raises(RuntimeError):
  14. _ = rx.debounce_input(rx.debounce_input(rx.debounce_input()))
  15. def test_create_many_child():
  16. """DebounceInput raises RuntimeError if more than 1 child is provided."""
  17. with pytest.raises(RuntimeError):
  18. _ = rx.debounce_input("foo", "bar")
  19. def test_create_no_on_change():
  20. """DebounceInput raises ValueError if child has no on_change handler."""
  21. with pytest.raises(ValueError):
  22. _ = rx.debounce_input(rx.input())
  23. class S(BaseState):
  24. """Example state for debounce tests."""
  25. value: str = ""
  26. @rx.event
  27. def on_change(self, v: str):
  28. """Dummy on_change handler.
  29. Args:
  30. v: The changed value.
  31. """
  32. pass
  33. def test_render_child_props():
  34. """DebounceInput should render props from child component."""
  35. tag = rx.debounce_input(
  36. rx.input(
  37. foo="bar",
  38. baz="quuc",
  39. value="real",
  40. on_change=S.on_change,
  41. )
  42. )._render()
  43. assert "css" in tag.props and isinstance(tag.props["css"], rx.vars.Var)
  44. for prop in ["foo", "bar", "baz", "quuc"]:
  45. assert prop in str(tag.props["css"])
  46. assert tag.props["value"].equals(LiteralVar.create("real"))
  47. assert len(tag.props["onChange"].events) == 1
  48. assert tag.props["onChange"].events[0].handler == S.on_change
  49. assert tag.contents == ""
  50. def test_render_with_class_name():
  51. tag = rx.debounce_input(
  52. rx.input(
  53. on_change=S.on_change,
  54. class_name="foo baz",
  55. )
  56. )._render()
  57. assert isinstance(tag.props["className"], rx.vars.Var)
  58. assert "foo baz" in str(tag.props["className"])
  59. def test_render_with_ref():
  60. tag = rx.debounce_input(
  61. rx.input(
  62. on_change=S.on_change,
  63. id="foo_bar",
  64. )
  65. )._render()
  66. assert isinstance(tag.props["inputRef"], rx.vars.Var)
  67. assert "foo_bar" in str(tag.props["inputRef"])
  68. def test_render_with_key():
  69. tag = rx.debounce_input(
  70. rx.input(
  71. on_change=S.on_change,
  72. key="foo_bar",
  73. )
  74. )._render()
  75. assert isinstance(tag.props["key"], rx.vars.Var)
  76. assert "foo_bar" in str(tag.props["key"])
  77. def test_render_with_special_props():
  78. special_prop = Var(_js_expr="{foo_bar}")
  79. tag = rx.debounce_input(
  80. rx.input(
  81. on_change=S.on_change,
  82. special_props=[special_prop],
  83. )
  84. )._render()
  85. assert len(tag.special_props) == 1
  86. assert next(iter(tag.special_props)).equals(special_prop)
  87. def test_event_triggers():
  88. debounced_input = rx.debounce_input(
  89. rx.input(
  90. on_change=S.on_change,
  91. )
  92. )
  93. assert tuple(debounced_input.get_event_triggers()) == (
  94. *rx.Component().get_event_triggers(), # default event triggers
  95. "on_change",
  96. )
  97. def test_render_child_props_recursive():
  98. """DebounceInput should render props from child component.
  99. If the child component is a DebounceInput, then props will be copied from it
  100. recursively.
  101. """
  102. tag = rx.debounce_input(
  103. rx.debounce_input(
  104. rx.debounce_input(
  105. rx.debounce_input(
  106. rx.input(
  107. foo="bar",
  108. baz="quuc",
  109. value="real",
  110. on_change=S.on_change,
  111. ),
  112. value="inner",
  113. debounce_timeout=666,
  114. force_notify_on_blur=False,
  115. ),
  116. debounce_timeout=42,
  117. ),
  118. value="outer",
  119. ),
  120. force_notify_by_enter=False,
  121. )._render()
  122. assert "css" in tag.props and isinstance(tag.props["css"], rx.vars.Var)
  123. for prop in ["foo", "bar", "baz", "quuc"]:
  124. assert prop in str(tag.props["css"])
  125. assert tag.props["value"].equals(LiteralVar.create("outer"))
  126. assert tag.props["forceNotifyOnBlur"]._js_expr == "false"
  127. assert tag.props["forceNotifyByEnter"]._js_expr == "false"
  128. assert tag.props["debounceTimeout"]._js_expr == "42"
  129. assert len(tag.props["onChange"].events) == 1
  130. assert tag.props["onChange"].events[0].handler == S.on_change
  131. assert tag.contents == ""
  132. def test_full_control_implicit_debounce():
  133. """DebounceInput is used when value and on_change are used together."""
  134. tag = rx.input(
  135. value=S.value,
  136. on_change=S.on_change,
  137. )._render()
  138. assert tag.props["debounceTimeout"]._js_expr == str(DEFAULT_DEBOUNCE_TIMEOUT)
  139. assert len(tag.props["onChange"].events) == 1
  140. assert tag.props["onChange"].events[0].handler == S.on_change
  141. assert tag.contents == ""
  142. def test_full_control_implicit_debounce_text_area():
  143. """DebounceInput is used when value and on_change are used together."""
  144. tag = rx.text_area(
  145. value=S.value,
  146. on_change=S.on_change,
  147. )._render()
  148. assert tag.props["debounceTimeout"]._js_expr == str(DEFAULT_DEBOUNCE_TIMEOUT)
  149. assert len(tag.props["onChange"].events) == 1
  150. assert tag.props["onChange"].events[0].handler == S.on_change
  151. assert tag.contents == ""