test_html.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import pytest
  2. from reflex.components.core.html import Html
  3. from reflex.state import State
  4. def test_html_no_children():
  5. with pytest.raises(ValueError):
  6. _ = Html.create()
  7. def test_html_many_children():
  8. with pytest.raises(ValueError):
  9. _ = Html.create("foo", "bar")
  10. def test_html_create():
  11. html = Html.create("<p>Hello !</p>")
  12. assert str(html.dangerouslySetInnerHTML) == '({ ["__html"] : "<p>Hello !</p>" })' # pyright: ignore [reportAttributeAccessIssue]
  13. assert (
  14. str(html)
  15. == '<div className={"rx-Html"} dangerouslySetInnerHTML={({ ["__html"] : "<p>Hello !</p>" })}/>'
  16. )
  17. def test_html_fstring_create():
  18. class TestState(State):
  19. """The app state."""
  20. myvar: str = "Blue"
  21. html = Html.create(f"<p>Hello {TestState.myvar}!</p>")
  22. assert (
  23. str(html.dangerouslySetInnerHTML) # pyright: ignore [reportAttributeAccessIssue]
  24. == f'({{ ["__html"] : ("<p>Hello "+{TestState.myvar!s}+"!</p>") }})'
  25. )
  26. assert (
  27. str(html)
  28. == f'<div className={{"rx-Html"}} dangerouslySetInnerHTML={{{html.dangerouslySetInnerHTML!s}}}/>' # pyright: ignore [reportAttributeAccessIssue]
  29. )