test_debounce.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 import BaseVar
  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.Var)
  43. for prop in ["foo", "bar", "baz", "quuc"]:
  44. assert prop in str(tag.props["css"])
  45. assert tag.props["value"].equals(
  46. BaseVar(
  47. _var_name="real", _var_type=str, _var_is_local=True, _var_is_string=False
  48. )
  49. )
  50. assert len(tag.props["onChange"].events) == 1
  51. assert tag.props["onChange"].events[0].handler == S.on_change
  52. assert tag.contents == ""
  53. def test_render_with_class_name():
  54. tag = rx.debounce_input(
  55. rx.input(
  56. on_change=S.on_change,
  57. class_name="foo baz",
  58. )
  59. )._render()
  60. assert isinstance(tag.props["className"], rx.Var)
  61. assert "foo baz" in str(tag.props["className"])
  62. def test_render_with_ref():
  63. tag = rx.debounce_input(
  64. rx.input(
  65. on_change=S.on_change,
  66. id="foo_bar",
  67. )
  68. )._render()
  69. assert isinstance(tag.props["inputRef"], rx.Var)
  70. assert "foo_bar" in str(tag.props["inputRef"])
  71. def test_event_triggers():
  72. debounced_input = rx.debounce_input(
  73. rx.input(
  74. on_change=S.on_change,
  75. )
  76. )
  77. assert tuple(debounced_input.get_event_triggers()) == (
  78. *rx.Component().get_event_triggers(), # default event triggers
  79. "on_change",
  80. )
  81. def test_render_child_props_recursive():
  82. """DebounceInput should render props from child component.
  83. If the child component is a DebounceInput, then props will be copied from it
  84. recursively.
  85. """
  86. tag = rx.debounce_input(
  87. rx.debounce_input(
  88. rx.debounce_input(
  89. rx.debounce_input(
  90. rx.input(
  91. foo="bar",
  92. baz="quuc",
  93. value="real",
  94. on_change=S.on_change,
  95. ),
  96. value="inner",
  97. debounce_timeout=666,
  98. force_notify_on_blur=False,
  99. ),
  100. debounce_timeout=42,
  101. ),
  102. value="outer",
  103. ),
  104. force_notify_by_enter=False,
  105. )._render()
  106. assert "css" in tag.props and isinstance(tag.props["css"], rx.Var)
  107. for prop in ["foo", "bar", "baz", "quuc"]:
  108. assert prop in str(tag.props["css"])
  109. assert tag.props["value"].equals(
  110. BaseVar(
  111. _var_name="outer", _var_type=str, _var_is_local=True, _var_is_string=False
  112. )
  113. )
  114. assert tag.props["forceNotifyOnBlur"]._var_name == "false"
  115. assert tag.props["forceNotifyByEnter"]._var_name == "false"
  116. assert tag.props["debounceTimeout"]._var_name == "42"
  117. assert len(tag.props["onChange"].events) == 1
  118. assert tag.props["onChange"].events[0].handler == S.on_change
  119. assert tag.contents == ""
  120. def test_full_control_implicit_debounce():
  121. """DebounceInput is used when value and on_change are used together."""
  122. tag = rx.input(
  123. value=S.value,
  124. on_change=S.on_change,
  125. )._render()
  126. assert tag.props["debounceTimeout"]._var_name == str(DEFAULT_DEBOUNCE_TIMEOUT)
  127. assert len(tag.props["onChange"].events) == 1
  128. assert tag.props["onChange"].events[0].handler == S.on_change
  129. assert tag.contents == ""
  130. def test_full_control_implicit_debounce_text_area():
  131. """DebounceInput is used when value and on_change are used together."""
  132. tag = rx.text_area(
  133. value=S.value,
  134. on_change=S.on_change,
  135. )._render()
  136. assert tag.props["debounceTimeout"]._var_name == str(DEFAULT_DEBOUNCE_TIMEOUT)
  137. assert len(tag.props["onChange"].events) == 1
  138. assert tag.props["onChange"].events[0].handler == S.on_change
  139. assert tag.contents == ""