|
1 | 1 | # ruff: noqa: S701 |
| 2 | +import asyncio |
2 | 3 | from pathlib import Path |
3 | 4 |
|
4 | 5 | import pytest |
5 | 6 | from jinja2 import Environment as JinjaEnvironment |
6 | 7 | from jinja2 import FileSystemLoader as JinjaFileSystemLoader |
| 8 | +from requests import request |
7 | 9 | from starlette.applications import Starlette |
8 | 10 | from starlette.routing import Route |
9 | 11 | from starlette.templating import Jinja2Templates |
10 | 12 |
|
| 13 | +from reactpy import config as _config |
11 | 14 | from reactpy import html |
12 | 15 | from reactpy.executors.asgi.pyscript import ReactPyCsr |
13 | 16 | from reactpy.testing import BackendFixture, DisplayFixture |
14 | 17 |
|
| 18 | +REACTPY_TESTS_DEFAULT_TIMEOUT = _config.REACTPY_TESTS_DEFAULT_TIMEOUT |
| 19 | + |
15 | 20 |
|
16 | 21 | @pytest.fixture(scope="module") |
17 | 22 | async def display(browser): |
@@ -102,6 +107,66 @@ def test_bad_file_path(): |
102 | 107 | ReactPyCsr() |
103 | 108 |
|
104 | 109 |
|
| 110 | +async def test_customized_noscript_vdom(): |
| 111 | + app = ReactPyCsr( |
| 112 | + Path(__file__).parent / "pyscript_components" / "root.py", |
| 113 | + prepend_body=html.noscript( |
| 114 | + html.p({"id": "noscript-message"}, "Please enable JavaScript.") |
| 115 | + ), |
| 116 | + ) |
| 117 | + |
| 118 | + async with BackendFixture(app) as server: |
| 119 | + url = f"http://{server.host}:{server.port}" |
| 120 | + response = await asyncio.to_thread( |
| 121 | + request, "GET", url, timeout=REACTPY_TESTS_DEFAULT_TIMEOUT.current |
| 122 | + ) |
| 123 | + assert response.status_code == 200 |
| 124 | + assert ( |
| 125 | + '<noscript><p id="noscript-message">Please enable JavaScript.</p></noscript>' |
| 126 | + in response.text |
| 127 | + ) |
| 128 | + |
| 129 | + async with BackendFixture(app) as server: |
| 130 | + url = f"http://{server.host}:{server.port}" |
| 131 | + response = await asyncio.to_thread( |
| 132 | + request, "GET", url, timeout=REACTPY_TESTS_DEFAULT_TIMEOUT.current |
| 133 | + ) |
| 134 | + assert response.status_code == 200 |
| 135 | + assert ( |
| 136 | + '<noscript><p id="noscript-message">Please enable JavaScript.</p></noscript>' |
| 137 | + in response.text |
| 138 | + ) |
| 139 | + |
| 140 | + |
| 141 | +async def test_prepend_body_default_is_noscript(): |
| 142 | + app = ReactPyCsr(Path(__file__).parent / "pyscript_components" / "root.py") |
| 143 | + |
| 144 | + async with BackendFixture(app) as server: |
| 145 | + url = f"http://{server.host}:{server.port}" |
| 146 | + response = await asyncio.to_thread( |
| 147 | + request, "GET", url, timeout=REACTPY_TESTS_DEFAULT_TIMEOUT.current |
| 148 | + ) |
| 149 | + assert response.status_code == 200 |
| 150 | + assert ( |
| 151 | + "<noscript>Enable JavaScript to view this site.</noscript>" in response.text |
| 152 | + ) |
| 153 | + |
| 154 | + |
| 155 | +async def test_prepend_body_disabled(): |
| 156 | + app = ReactPyCsr( |
| 157 | + Path(__file__).parent / "pyscript_components" / "root.py", |
| 158 | + prepend_body=None, |
| 159 | + ) |
| 160 | + |
| 161 | + async with BackendFixture(app) as server: |
| 162 | + url = f"http://{server.host}:{server.port}" |
| 163 | + response = await asyncio.to_thread( |
| 164 | + request, "GET", url, timeout=REACTPY_TESTS_DEFAULT_TIMEOUT.current |
| 165 | + ) |
| 166 | + assert response.status_code == 200 |
| 167 | + assert "<noscript>" not in response.text |
| 168 | + |
| 169 | + |
105 | 170 | async def test_jinja_template_tag(jinja_display: DisplayFixture): |
106 | 171 | await jinja_display.goto("/") |
107 | 172 |
|
|
0 commit comments