test_markdown.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import pytest
  2. import reflex as rx
  3. from reflex.components.markdown import Markdown
  4. @pytest.mark.parametrize(
  5. "tag,expected",
  6. [
  7. ("h1", "Heading"),
  8. ("h2", "Heading"),
  9. ("h3", "Heading"),
  10. ("h4", "Heading"),
  11. ("h5", "Heading"),
  12. ("h6", "Heading"),
  13. ("p", "Text"),
  14. ("ul", "UnorderedList"),
  15. ("ol", "OrderedList"),
  16. ("li", "ListItem"),
  17. ("a", "Link"),
  18. ("code", "Code"),
  19. ],
  20. )
  21. def test_get_component(tag, expected):
  22. """Test getting a component from the component map.
  23. Args:
  24. tag: The tag to get.
  25. expected: The expected component.
  26. """
  27. md = Markdown.create("# Hello")
  28. assert tag in md.component_map # type: ignore
  29. assert md.get_component(tag).tag == expected # type: ignore
  30. def test_set_component_map():
  31. """Test setting the component map."""
  32. component_map = {
  33. "h1": lambda value: rx.box(
  34. rx.heading(value, as_="h1", size="2xl"), padding="1em"
  35. ),
  36. "p": lambda value: rx.box(rx.text(value), padding="1em"),
  37. }
  38. md = Markdown.create("# Hello", component_map=component_map)
  39. # Check that the new tags have been added.
  40. assert md.get_component("h1").tag == "Box" # type: ignore
  41. assert md.get_component("p").tag == "Box" # type: ignore
  42. # Make sure the old tags are still there.
  43. assert md.get_component("h2").tag == "Heading" # type: ignore
  44. def test_pass_custom_styles():
  45. """Test that passing custom styles works."""
  46. md = Markdown.create("# Hello", custom_styles={"h1": {"color": "red"}})
  47. comp = md.get_component("h1") # type: ignore
  48. assert comp.style == {"color": "red", "marginY": "0.5em"}