1111import uvicorn
1212from asgiref import typing as asgi_types
1313
14- from reactpy .asgi .standalone import ReactPy
14+ from reactpy .asgi .standalone import ReactPy , ReactPyMiddleware
1515from reactpy .config import REACTPY_TESTS_DEFAULT_TIMEOUT
1616from reactpy .core .component import component
1717from reactpy .core .hooks import use_callback , use_effect , use_state
2121 list_logged_exceptions ,
2222)
2323from reactpy .testing .utils import find_available_port
24- from reactpy .types import ComponentConstructor
24+ from reactpy .types import ComponentConstructor , ReactPyConfig
2525from reactpy .utils import Ref
2626
2727
@@ -37,7 +37,7 @@ class BackendFixture:
3737 server.mount(MyComponent)
3838 """
3939
40- _records : list [logging .LogRecord ]
40+ log_records : list [logging .LogRecord ]
4141 _server_future : asyncio .Task [Any ]
4242 _exit_stack = AsyncExitStack ()
4343
@@ -47,25 +47,33 @@ def __init__(
4747 host : str = "127.0.0.1" ,
4848 port : int | None = None ,
4949 timeout : float | None = None ,
50+ reactpy_config : ReactPyConfig | None = None ,
5051 ) -> None :
5152 self .host = host
5253 self .port = port or find_available_port (host )
53- self .mount , self . _root_component = _hotswap ()
54+ self .mount = mount_to_hotswap
5455 self .timeout = (
5556 REACTPY_TESTS_DEFAULT_TIMEOUT .current if timeout is None else timeout
5657 )
57- self ._app = app or ReactPy (self ._root_component )
58+ if isinstance (app , (ReactPyMiddleware , ReactPy )):
59+ self ._app = app
60+ elif app :
61+ self ._app = ReactPyMiddleware (
62+ app ,
63+ root_components = ["reactpy.testing.backend.root_hotswap_component" ],
64+ ** (reactpy_config or {}),
65+ )
66+ else :
67+ self ._app = ReactPy (
68+ root_hotswap_component ,
69+ ** (reactpy_config or {}),
70+ )
5871 self .webserver = uvicorn .Server (
5972 uvicorn .Config (
6073 app = self ._app , host = self .host , port = self .port , loop = "asyncio"
6174 )
6275 )
6376
64- @property
65- def log_records (self ) -> list [logging .LogRecord ]:
66- """A list of captured log records"""
67- return self ._records
68-
6977 def url (self , path : str = "" , query : Any | None = None ) -> str :
7078 """Return a URL string pointing to the host and point of the server
7179
@@ -108,7 +116,7 @@ def list_logged_exceptions(
108116
109117 async def __aenter__ (self ) -> BackendFixture :
110118 self ._exit_stack = AsyncExitStack ()
111- self ._records = self ._exit_stack .enter_context (capture_reactpy_logs ())
119+ self .log_records = self ._exit_stack .enter_context (capture_reactpy_logs ())
112120
113121 # Wait for the server to start
114122 Thread (target = self .webserver .run , daemon = True ).start ()
@@ -215,3 +223,6 @@ def swap(constructor: Callable[[], Any] | None) -> None:
215223 constructor_ref .current = constructor or (lambda : None )
216224
217225 return swap , HotSwap
226+
227+
228+ mount_to_hotswap , root_hotswap_component = _hotswap ()
0 commit comments