Repository: pyscript/pyscript
File: pyscript.core/tests/integration/test_02_display.py (Python)

Code before:
            <script type="py">
                from pyscript import display
                display('hello world', target="#mydiv")
            </script>
            <div id="mydiv"></div>
            """
        )
        mydiv = self.page.locator("#mydiv")
        assert mydiv.inner_text() == "hello world"

    def test_non_existing_id_target_raises_value_error(self):
        self.pyscript_run(
            """
            <script type="py">
                from pyscript import display
                display('hello world', target="non-existing")
            </script>
            """
        )
        error_msg = (
            f"Invalid selector with id=non-existing. Cannot be found in the page."
        )
        self.check_py_errors(f"ValueError: {error_msg}")
        # Check Console error
        tb_lines = self.console.error.lines[-1].splitlines()
        assert tb_lines[0] == "PythonError: Traceback (most recent call last):"

    def test_empty_string_target_raises_value_error(self):
        self.pyscript_run(
            """
            <script type="py">
                from pyscript import display
                display('hello world', target="")
            </script>
            """
        )
        self.check_py_errors(f"ValueError: Cannot have an empty target")
        # Check Console error
        tb_lines = self.console.error.lines[-1].splitlines()
        assert tb_lines[0] == "PythonError: Traceback (most recent call last):"

    def test_non_string_target_values_raise_typerror(self):
        self.pyscript_run(
            """
            <script type="py">
                from pyscript import display
                display("hello False", target=False)
            </script>
            """
        )
        error_msg = f"target must be str or None, not bool"

Review comment (suggestion):
I think this is not strictly necessary: if we decide that Python exceptions should go *both* to the console and to the DOM (and I think they should), then this check should go inside `check_py_errors`, probably.

Code after:
            <script type="py">
                from pyscript import display
                display('hello world', target="#mydiv")
            </script>
            <div id="mydiv"></div>
            """
        )
        mydiv = self.page.locator("#mydiv")
        assert mydiv.inner_text() == "hello world"

    def test_non_existing_id_target_raises_value_error(self):
        self.pyscript_run(
            """
            <script type="py">
                from pyscript import display
                display('hello world', target="non-existing")
            </script>
            """
        )
        error_msg = (
            f"Invalid selector with id=non-existing. Cannot be found in the page."
        )
        self.check_py_errors(f"ValueError: {error_msg}")

    def test_empty_string_target_raises_value_error(self):
        self.pyscript_run(
            """
            <script type="py">
                from pyscript import display
                display('hello world', target="")
            </script>
            """
        )
        self.check_py_errors(f"ValueError: Cannot have an empty target")

    def test_non_string_target_values_raise_typerror(self):
        self.pyscript_run(
            """
            <script type="py">
                from pyscript import display
                display("hello False", target=False)
            </script>
            """
        )
        error_msg = f"target must be str or None, not bool"
        self.check_py_errors(f"TypeError: {error_msg}")

        self.pyscript_run(
            """
            <script type="py">
                from pyscript import display