Skip to content

Commit

Permalink
remove activate() and deactivate()
Browse files Browse the repository at this point in the history
  • Loading branch information
falkoschindler committed Aug 2, 2024
1 parent ef58e84 commit 161c51f
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 58 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!')
63 changes: 7 additions & 56 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 @@ -31,7 +30,12 @@ def __init__(self, client: httpx.AsyncClient) -> None:
self.client: Optional[Client] = None
self.back_history: List[str] = []
self.forward_history: List[str] = []
ui.navigate = self.navigate = UserNavigate(self)
self.navigate = UserNavigate(self)

def __getattribute__(self, name: str) -> asyncio.Any:
if name != 'navigate': # NOTE: avoid infinite recursion
ui.navigate = self.navigate
return super().__getattribute__(name)

async def open(self, path: str, *, clear_forward_history: bool = True) -> None:
"""Open the given path."""
Expand All @@ -49,29 +53,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.
This can be used if you have multiple users and want to switch between them.
"""
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.
This can be used if you have multiple users and want to switch between them.
"""
assert self.client
self.client.__exit__()
self.current_user = None

@overload
async def should_see(self,
Expand Down Expand Up @@ -230,33 +211,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
1 change: 0 additions & 1 deletion tests/test_user_simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ def other():
await userB.open('/')
await userB.should_see('Main page')

userA.activate()
userA.find('go to other').click()
await userA.should_see('Other page')
await userB.should_see('Main page')
Expand Down

0 comments on commit 161c51f

Please sign in to comment.