test_markdown.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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", "Flex"),
  15. ("ol", "Flex"),
  16. ("li", "li"),
  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.chakra.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 == {
  49. "color": "red",
  50. "marginBottom": "0.5em",
  51. "marginTop": "0.5em",
  52. }