Example #1
0
def test_class_name_swapped():
    """Test that class_name is swapped properly."""
    val = render(_.div(class_name="myclass"))
    assert val == '<div class="myclass"></div>'

    val = render(_.p("Some Text", class_name="myclass"))
    assert val == '<p class="myclass">Some Text</p>'
Example #2
0
def test_render_boolean():
    """Test the rendering of boolean attributes."""
    val = render(_.input(type="checkbox", checked=True))
    assert val == '<input type="checkbox" checked>'

    val = render(_.option("California", selected=True))
    assert val == "<option selected>California</option>"
Example #3
0
def test_render_data():
    """Test the rendering of data attributes."""
    val = render(_.div(data_text="Some text!"))
    assert val == '<div data-text="Some text!"></div>'

    val = render(_.div(data_lots_of_dashes="value"))
    assert val == '<div data-lots-of-dashes="value"></div>'
Example #4
0
def test_render_numbers():
    val = render(_.p(8))
    assert val == "<p>8</p>"

    val = render(_.p(8.8))
    assert val == "<p>8.8</p>"

    val = render(_.div(data_number=8))
    assert val == '<div data-number="8"></div>'
Example #5
0
def test_render_aria():
    """Test the rendering of aria attributes."""

    val = render(_.button("Close", aria_label="Close"))
    assert val == '<button aria-label="Close">Close</button>'

    val = render(
        _.button("Collapse", aria_expanded="false", aria_controls="collapsible-0")
    )
    assert (
        val
        == '<button aria-expanded="false" aria-controls="collapsible-0">Collapse</button>'
    )
Example #6
0
def test_orm_style_functions():
    """An ORM-like example of an element function."""

    from typing import NamedTuple

    class Article(NamedTuple):
        """Basically a fake database model."""

        title: str
        content: str
        author: str

        def html(self):
            header = _.header(_.h1(self.title, class_name="article__header"))
            content = _.section(_.p(self.content, class_name="article__content"))
            byline = _.section(
                _.p(_.i(f"By {self.author}"), class_name="article__byline")
            )

            return _.article(header, content, byline,)

    article = Article(
        title="Hello from DashML!",
        content="Demo of ORM-style usage of DashML",
        author="Madelyn Eriksen",
    )

    assert render(article.html())
    assert (
        article.html().xpath("//p[@class='article__content']")[0].text
        == article.content
    )
    assert (
        article.html().xpath("//h1[@class='article__header']")[0].text == article.title
    )
    assert (
        article.html().xpath("//p[@class='article__byline']/i")[0].text
        == "By Madelyn Eriksen"
    )
Example #7
0
def test_html_for_swapped():
    """Test that html_for is swapped."""
    val = render(_.label(html_for="my-input"))
    assert val == '<label for="my-input"></label>'
Example #8
0
def test_render_script_fails():
    """Gratuitous test of the classic alert."""
    value = render(_.p("<script>alert('Hello, world!')</script>"))
    assert value == (
        "<p>&lt;script&gt;alert('Hello, world!')&lt;/script&gt;</p>")