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. def on_change(self, v: str):
  27. """Dummy on_change handler.
  28. Args:
  29. v: The changed value.
  30. """
  31. pass
  32. def test_render_child_props():
  33. """DebounceInput should render props from child component."""
  34. tag = rx.debounce_input(
  35. rx.input(
  36. foo="bar",
  37. baz="quuc",
  38. value="real",
  39. on_change=S.on_change,
  40. )
  41. )._render()
  42. assert "css" in tag.props and isinstance(tag.props["css"], rx.vars.Var)
  43. for prop in ["foo", "bar", "baz", "quuc"]:
  44. assert prop in str(tag.props["css"])
  45. assert tag.props["value"].equals(LiteralVar.create("real"))
  46. assert len(tag.props["onChange"].events) == 1
  47. assert tag.props["onChange"].events[0].handler == S.on_change
  48. assert tag.contents == ""
  49. def test_render_with_class_name():
  50. tag = rx.debounce_input(
  51. rx.input(
  52. on_change=S.on_change,
  53. class_name="foo baz",
  54. )
  55. )._render()
  56. assert isinstance(tag.props["className"], rx.vars.Var)
  57. assert "foo baz" in str(tag.props["className"])
  58. def test_render_with_ref():
  59. tag = rx.debounce_input(
  60. rx.input(
  61. on_change=S.on_change,
  62. id="foo_bar",
  63. )
  64. )._render()
  65. assert isinstance(tag.props["inputRef"], rx.vars.Var)
  66. assert "foo_bar" in str(tag.props["inputRef"])
  67. def test_render_with_key():
  68. tag = rx.debounce_input(
  69. rx.input(
  70. on_change=S.on_change,
  71. key="foo_bar",
  72. )
  73. )._render()
  74. assert isinstance(tag.props["key"], rx.vars.Var)
  75. assert "foo_bar" in str(tag.props["key"])
  76. def test_render_with_special_props():
  77. special_prop = Var(_js_expr="{foo_bar}")
  78. tag = rx.debounce_input(
  79. rx.input(
  80. on_change=S.on_change,
  81. special_props=[special_prop],
  82. )
  83. )._render()
  84. assert len(tag.special_props) == 1
  85. assert list(tag.special_props)[0].equals(special_prop)
  86. def test_event_triggers():
  87. debounced_input = rx.debounce_input(
  88. rx.input(
  89. on_change=S.on_change,
  90. )
  91. )
  92. assert tuple(debounced_input.get_event_triggers()) == (
  93. *rx.Component().get_event_triggers(), # default event triggers
  94. "on_change",
  95. )
  96. def test_render_child_props_recursive():
  97. """DebounceInput should render props from child component.
  98. If the child component is a DebounceInput, then props will be copied from it
  99. recursively.
  100. """
  101. tag = rx.debounce_input(
  102. rx.debounce_input(
  103. rx.debounce_input(
  104. rx.debounce_input(
  105. rx.input(
  106. foo="bar",
  107. baz="quuc",
  108. value="real",
  109. on_change=S.on_change,
  110. ),
  111. value="inner",
  112. debounce_timeout=666,
  113. force_notify_on_blur=False,
  114. ),
  115. debounce_timeout=42,
  116. ),
  117. value="outer",
  118. ),
  119. force_notify_by_enter=False,
  120. )._render()
  121. assert "css" in tag.props and isinstance(tag.props["css"], rx.vars.Var)
  122. for prop in ["foo", "bar", "baz", "quuc"]:
  123. assert prop in str(tag.props["css"])
  124. assert tag.props["value"].equals(LiteralVar.create("outer"))
  125. assert tag.props["forceNotifyOnBlur"]._js_expr == "false"
  126. assert tag.props["forceNotifyByEnter"]._js_expr == "false"
  127. assert tag.props["debounceTimeout"]._js_expr == "42"
  128. assert len(tag.props["onChange"].events) == 1
  129. assert tag.props["onChange"].events[0].handler == S.on_change
  130. assert tag.contents == ""
  131. def test_full_control_implicit_debounce():
  132. """DebounceInput is used when value and on_change are used together."""
  133. tag = rx.input(
  134. value=S.value,
  135. on_change=S.on_change,
  136. )._render()
  137. assert tag.props["debounceTimeout"]._js_expr == str(DEFAULT_DEBOUNCE_TIMEOUT)
  138. assert len(tag.props["onChange"].events) == 1
  139. assert tag.props["onChange"].events[0].handler == S.on_change
  140. assert tag.contents == ""
  141. def test_full_control_implicit_debounce_text_area():
  142. """DebounceInput is used when value and on_change are used together."""
  143. tag = rx.text_area(
  144. value=S.value,
  145. on_change=S.on_change,
  146. )._render()
  147. assert tag.props["debounceTimeout"]._js_expr == str(DEFAULT_DEBOUNCE_TIMEOUT)
  148. assert len(tag.props["onChange"].events) == 1
  149. assert tag.props["onChange"].events[0].handler == S.on_change
  150. assert tag.contents == ""