test_timer.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import pytest
  2. from nicegui import ui
  3. from .screen import Screen
  4. class Counter:
  5. value = 0
  6. def increment(self):
  7. self.value += 1
  8. def test_timer(screen: Screen):
  9. counter = Counter()
  10. ui.timer(0.1, counter.increment)
  11. assert counter.value == 0, 'count is initially zero'
  12. screen.wait(0.5)
  13. assert counter.value == 0, 'timer is not running'
  14. screen.start_server()
  15. screen.wait(0.5)
  16. assert counter.value > 0, 'timer is running after starting the server'
  17. def test_timer_on_private_page(screen: Screen):
  18. counter = Counter()
  19. @ui.page('/')
  20. def page():
  21. ui.timer(0.1, counter.increment)
  22. assert counter.value == 0, 'count is initially zero'
  23. screen.start_server()
  24. screen.wait(0.5)
  25. assert counter.value == 0, 'timer is not running even after starting the server'
  26. screen.open('/')
  27. screen.wait(0.5)
  28. assert counter.value > 0, 'timer is running after opening the page'
  29. screen.close()
  30. count = counter.value
  31. screen.wait(0.5)
  32. assert counter.value == count, 'timer is not running anymore after closing the page'
  33. @pytest.mark.parametrize('once', [True, False])
  34. def test_setting_visibility(screen: Screen, once: bool):
  35. '''reproduction of https://github.com/zauberzeug/nicegui/issues/206'''
  36. @ui.page('/')
  37. def page():
  38. label = ui.label('Some Label')
  39. ui.timer(0.1, lambda: label.set_visibility(False), once=once)
  40. screen.open('/')
  41. screen.wait(0.5)
  42. screen.should_not_contain('Some Label')