Skip to content

Commit

Permalink
remove current_user and activate()/deactivate()
Browse files Browse the repository at this point in the history
  • Loading branch information
falkoschindler committed Aug 2, 2024
1 parent b54adf7 commit 989dfa1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 52 deletions.
1 change: 0 additions & 1 deletion examples/chat_app/test_chat_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,5 @@ async def test_sending_messages(create_user: Callable[[], User]) -> None:
userB.find(ui.input).type('Hello from screen B!').trigger('keydown.enter')
await userB.should_see('message')

userA.activate()
await userA.should_see('Hello from screen A!')
await userA.should_see('Hello from screen B!')
60 changes: 9 additions & 51 deletions nicegui/testing/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@

import httpx
import socketio
from typing_extensions import Self

from nicegui import Client, ElementFilter, ui
from nicegui.element import Element
from nicegui.nicegui import Slot, _on_handshake
from nicegui.nicegui import _on_handshake

from .user_interaction import UserInteraction
from .user_navigate import UserNavigate
Expand All @@ -23,7 +22,6 @@


class User:
current_user: Optional[User] = None

def __init__(self, client: httpx.AsyncClient) -> None:
self.http_client = client
Expand All @@ -49,23 +47,6 @@ async def open(self, path: str, *, clear_forward_history: bool = True) -> None:
self.back_history.append(path)
if clear_forward_history:
self.forward_history.clear()
self.activate()

def activate(self) -> Self:
"""Activate the user for interaction."""
if self.current_user:
self.current_user.deactivate()
self.current_user = self
assert self.client
ui.navigate = self.navigate
self.client.__enter__() # pylint: disable=unnecessary-dunder-call
return self

def deactivate(self, *_) -> None:
"""Deactivate the user."""
assert self.client
self.client.__exit__()
self.current_user = None

@overload
async def should_see(self,
Expand Down Expand Up @@ -93,7 +74,14 @@ async def should_see(self,
content: Union[str, list[str], None] = None,
retries: int = 3,
) -> None:
"""Assert that the page contains an input with the given value."""
"""Assert that the page contains an element fulfilling certain filter rules.
Note that there is no scrolling in the user simulation -- the entire page is always *visible*.
Due to asynchronous execution, sometimes the expected elements only appear after a short delay.
By default `should_see` makes three attempts to find the element before failing.
This can be adjusted with the `retries` parameter.
"""
assert self.client
for _ in range(retries):
with self.client:
Expand Down Expand Up @@ -217,33 +205,3 @@ def _build_error_message(self,
return f'element of type {kind.__name__} with {marker=} and {content=} on the page:\n{self.current_layout}'
else:
return f'element with {marker=} and {content=} on the page:\n{self.current_layout}'


original_get_slot_stack = Slot.get_stack
original_prune_slot_stack = Slot.prune_stack


def get_stack(_=None) -> List[Slot]:
"""Return the slot stack of the current client."""
if User.current_user is None:
return original_get_slot_stack()
cls = Slot
client_id = id(User.current_user)
if client_id not in cls.stacks:
cls.stacks[client_id] = []
return cls.stacks[client_id]


def prune_stack(cls) -> None:
"""Remove the current slot stack if it is empty."""
if User.current_user is None:
original_prune_slot_stack()
return
cls = Slot
client_id = id(User.current_user)
if not cls.stacks[client_id]:
del cls.stacks[client_id]


Slot.get_stack = get_stack # type: ignore
Slot.prune_stack = prune_stack # type: ignore

0 comments on commit 989dfa1

Please sign in to comment.