33from pathlib import Path
44
55import pytest
6- from requests import request
76from jinja2 import Environment as JinjaEnvironment
87from jinja2 import FileSystemLoader as JinjaFileSystemLoader
8+ from requests import request
99from starlette .applications import Starlette
1010from starlette .routing import Route
1111from starlette .templating import Jinja2Templates
1212
13+ import reactpy
1314from reactpy import html
1415from reactpy .executors .asgi .pyscript import ReactPyCsr
1516from reactpy .testing import BackendFixture , DisplayFixture
@@ -110,7 +111,68 @@ async def test_customized_noscript(tmp_path: Path):
110111
111112 app = ReactPyCsr (
112113 Path (__file__ ).parent / "pyscript_components" / "root.py" ,
113- html_noscript_path = noscript_file ,
114+ html_noscript_str_or_path = noscript_file ,
115+ )
116+
117+ async with BackendFixture (app ) as server :
118+ url = f"http://{ server .host } :{ server .port } "
119+ response = await asyncio .to_thread (
120+ request , "GET" , url , timeout = REACTPY_TESTS_DEFAULT_TIMEOUT .current
121+ )
122+ assert response .status_code == 200
123+ assert (
124+ '<noscript><p id="noscript-message">Please enable JavaScript.</p></noscript>'
125+ in response .text
126+ )
127+
128+
129+
130+ async def test_customized_noscript_string ():
131+ app = ReactPyCsr (
132+ Path (__file__ ).parent / "pyscript_components" / "root.py" ,
133+ html_noscript_str_or_path = '<p id="noscript-message">Please enable JavaScript.</p>' ,
134+ )
135+
136+ async with BackendFixture (app ) as server :
137+ url = f"http://{ server .host } :{ server .port } "
138+ response = await asyncio .to_thread (
139+ request , "GET" , url , timeout = REACTPY_TESTS_DEFAULT_TIMEOUT .current
140+ )
141+ assert response .status_code == 200
142+ assert (
143+ '<noscript><p id="noscript-message">Please enable JavaScript.</p></noscript>'
144+ in response .text
145+ )
146+
147+
148+ async def test_customized_noscript_from_file (tmp_path : Path ):
149+ noscript_file = tmp_path / "noscript.html"
150+ noscript_file .write_text (
151+ '<p id="noscript-message">Please enable JavaScript.</p>' ,
152+ encoding = "utf-8" ,
153+ )
154+
155+ app = ReactPyCsr (
156+ Path (__file__ ).parent / "pyscript_components" / "root.py" ,
157+ html_noscript_str_or_path = noscript_file ,
158+ )
159+
160+ async with BackendFixture (app ) as server :
161+ url = f"http://{ server .host } :{ server .port } "
162+ response = await asyncio .to_thread (
163+ request , "GET" , url , timeout = REACTPY_TESTS_DEFAULT_TIMEOUT .current
164+ )
165+ assert response .status_code == 200
166+ assert (
167+ '<noscript><p id="noscript-message">Please enable JavaScript.</p></noscript>'
168+ in response .text
169+ )
170+
171+
172+ async def test_customized_noscript_from_string ():
173+ app = ReactPyCsr (
174+ Path (__file__ ).parent / "pyscript_components" / "root.py" ,
175+ html_noscript_str_or_path = '<p id="noscript-message">Please enable JavaScript.</p>' ,
114176 )
115177
116178 async with BackendFixture (app ) as server :
@@ -125,6 +187,46 @@ async def test_customized_noscript(tmp_path: Path):
125187 )
126188
127189
190+ async def test_default_noscript_rendered ():
191+ app = ReactPyCsr (Path (__file__ ).parent / "pyscript_components" / "root.py" )
192+
193+ async with BackendFixture (app ) as server :
194+ url = f"http://{ server .host } :{ server .port } "
195+ response = await asyncio .to_thread (
196+ request , "GET" , url , timeout = REACTPY_TESTS_DEFAULT_TIMEOUT .current
197+ )
198+ assert response .status_code == 200
199+ assert "<noscript>Enable JavaScript to view this site.</noscript>" in response .text
200+
201+
202+
203+ async def test_noscript_omitted ():
204+ app = ReactPyCsr (Path (__file__ ).parent / "pyscript_components" / "root.py" )
205+
206+ async with BackendFixture (app ) as server :
207+ url = f"http://{ server .host } :{ server .port } "
208+ response = await asyncio .to_thread (
209+ request , "GET" , url , timeout = REACTPY_TESTS_DEFAULT_TIMEOUT .current
210+ )
211+ assert response .status_code == 200
212+ assert (
213+ '<noscript><p id="noscript-message">Enable JavaScript to view this site.</p></noscript>'
214+ in response .text
215+ )
216+
217+
218+ async def test_noscript_disabled ():
219+ app = ReactPyCsr (Path (__file__ ).parent / "pyscript_components" / "root.py" , html_noscript_str_or_path = None )
220+
221+ async with BackendFixture (app ) as server :
222+ url = f"http://{ server .host } :{ server .port } "
223+ response = await asyncio .to_thread (
224+ request , "GET" , url , timeout = REACTPY_TESTS_DEFAULT_TIMEOUT .current
225+ )
226+ assert response .status_code == 200
227+ assert "<noscript>" not in response .text
228+
229+
128230async def test_jinja_template_tag (jinja_display : DisplayFixture ):
129231 await jinja_display .goto ("/" )
130232
0 commit comments