test_timer.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import asyncio
  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. def test_timer_with_update_after_await(screen: Screen):
  34. @ui.page('/')
  35. def page():
  36. async def update():
  37. ui.label('1')
  38. await asyncio.sleep(1.0)
  39. ui.label('2')
  40. ui.timer(1.0, update, once=True)
  41. screen.open('/')
  42. screen.should_not_contain('1')
  43. screen.wait_for('1')
  44. screen.should_not_contain('2')
  45. screen.wait_for('2')