test_timer.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. from nicegui import ui
  2. from .screen import Screen
  3. class Counter:
  4. value = 0
  5. def increment(self):
  6. self.value += 1
  7. def test_timer(screen: Screen):
  8. counter = Counter()
  9. ui.timer(0.1, counter.increment)
  10. assert counter.value == 0, 'count is initially zero'
  11. screen.wait(0.5)
  12. assert counter.value == 0, 'timer is not running'
  13. screen.start_server()
  14. screen.wait(0.5)
  15. assert counter.value > 0, 'timer is running after starting the server'
  16. def test_timer_on_private_page(screen: Screen):
  17. counter = Counter()
  18. @ui.page('/')
  19. def page():
  20. ui.timer(0.1, counter.increment)
  21. assert counter.value == 0, 'count is initially zero'
  22. screen.start_server()
  23. screen.wait(0.5)
  24. assert counter.value == 0, 'timer is not running even after starting the server'
  25. screen.open('/')
  26. screen.wait(0.5)
  27. assert counter.value > 0, 'timer is running after opening the page'
  28. screen.close()
  29. count = counter.value
  30. screen.wait(0.5)
  31. assert counter.value == count, 'timer is not running anymore after closing the page'