Sfoglia il codice sorgente

fix test for dependencies after startup

Falko Schindler 2 anni fa
parent
commit
f1004d6a87

+ 4 - 4
nicegui/elements/group.py

@@ -4,20 +4,20 @@ from typing import List
 
 import justpy as jp
 
+from .. import globals
 from ..binding import active_links, bindable_properties, bindings
-from ..globals import view_stack
 from .element import Element
 
 
 class Group(Element):
 
     def __enter__(self):
-        view_stack.append(self.view)
+        globals.view_stack.append(self.view)
         return self
 
     def __exit__(self, *_):
-        view_stack.pop()
-        if len(view_stack) <= 1:
+        globals.view_stack.pop()
+        if len(globals.view_stack) <= 1:
             self.update()  # NOTE: update when we are back on top of the stack (only the first page is in view stack)
 
     def tight(self) -> Group:

+ 2 - 2
nicegui/elements/link.py

@@ -2,7 +2,7 @@ from typing import Callable, Union
 
 import justpy as jp
 
-from ..globals import find_route
+from .. import globals
 from .group import Group
 
 
@@ -16,7 +16,7 @@ class Link(Group):
         :param text: display text
         :param target: page function or string that is a an absolute URL or relative path from base URL
         """
-        href = target if isinstance(target, str) else find_route(target)[1:]
+        href = target if isinstance(target, str) else globals.find_route(target)[1:]
         view = jp.A(text=text, href=href, classes='underline text-blue', temp=False)
 
         super().__init__(view)

+ 2 - 2
nicegui/elements/open.py

@@ -3,7 +3,7 @@ from typing import Callable, Optional, Union
 from justpy import WebPage
 from starlette.websockets import WebSocket
 
-from ..globals import find_route
+from .. import globals
 from ..task_logger import create_task
 
 
@@ -22,7 +22,7 @@ def open(self, target: Union[Callable, str], socket: Optional[WebSocket] = None)
 
 
 async def open_async(self, target: Union[Callable, str], socket: Optional[WebSocket] = None):
-    path = target if isinstance(target, str) else find_route(target)[1:]
+    path = target if isinstance(target, str) else globals.find_route(target)[1:]
     sockets = [socket] if socket else [s for socket_dict in WebPage.sockets.values() for s in socket_dict.values()]
     for socket in sockets:
         if not path:

+ 3 - 3
nicegui/elements/scene.py

@@ -5,8 +5,8 @@ from typing import Callable, Optional
 import websockets
 from justpy import WebPage
 
+from .. import globals
 from ..events import handle_event
-from ..globals import view_stack
 from ..page import Page
 from ..routes import add_dependencies
 from ..task_logger import create_task
@@ -107,14 +107,14 @@ class Scene(Element):
         super().__init__(SceneView(width=width, height=height, on_click=on_click))
 
     def __enter__(self):
-        view_stack.append(self.view)
+        globals.view_stack.append(self.view)
         scene = self.view.objects.get('scene', SceneObject(self.view, self.page))
         Object3D.stack.clear()
         Object3D.stack.append(scene)
         return self
 
     def __exit__(self, *_):
-        view_stack.pop()
+        globals.view_stack.pop()
 
     def move_camera(self,
                     x: Optional[float] = None,

+ 2 - 2
nicegui/timer.py

@@ -4,8 +4,8 @@ import traceback
 from collections import namedtuple
 from typing import Callable, List
 
+from . import globals
 from .binding import BindableProperty
-from .globals import tasks, view_stack
 from .helpers import is_coroutine
 from .task_logger import create_task
 
@@ -65,4 +65,4 @@ class Timer:
         if not event_loop.is_running():
             self.prepared_coroutines.append(NamedCoroutine(str(callback), coroutine))
         else:
-            tasks.append(create_task(coroutine, name=str(callback)))
+            globals.tasks.append(create_task(coroutine, name=str(callback)))

+ 8 - 7
tests/test_dependencies.py

@@ -9,10 +9,9 @@ def test_joystick_dependency(user: User):
         ui.joystick()
 
     user.open('/')
-    srcs = user.get_attributes('script', 'src')
-    assert any(s.endswith('joystick.js') for s in srcs)
-    assert any(s.endswith('nipplejs.min.js') for s in srcs)
-    user.sleep(2)  # NOTE we need to sleep here so the js timeout error is printed (start pytest with -s to see it)
+    sources = user.get_attributes('script', 'src')
+    assert any(s.endswith('joystick.js') for s in sources)
+    assert any(s.endswith('nipplejs.min.js') for s in sources)
 
 
 def test_keyboard_dependency_before_startup(user: User):
@@ -22,16 +21,18 @@ def test_keyboard_dependency_before_startup(user: User):
 
     user.open('/')
     assert any(s.endswith('keyboard.js') for s in user.get_attributes('script', 'src'))
-    user.sleep(2)  # NOTE we need to sleep here so the js timeout error is printed (start pytest with -s to see it)
 
 
 def test_keyboard_dependency_after_startup(user: User):
     @ui.page('/')
     def page():
-        ui.button('activate keyboard', on_click=lambda: ui.keyboard())
+        def add_keyboard():
+            with row:
+                ui.keyboard()
+        row = ui.row()
+        ui.button('activate keyboard', on_click=add_keyboard)
 
     user.open('/')
     assert not any(s.endswith('keyboard.js') for s in user.get_attributes('script', 'src'))
     user.click('activate keyboard')
     assert any(s.endswith('keyboard.js') for s in user.get_attributes('script', 'src'))
-    user.sleep(2)  # NOTE we need to sleep here so the js timeout error is printed (start pytest with -s to see it)