test_dark_mode.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. from nicegui import ui
  2. from nicegui.testing import Screen
  3. def test_dark_mode(screen: Screen):
  4. ui.label('Hello')
  5. dark = ui.dark_mode()
  6. ui.button('Dark', on_click=dark.enable)
  7. ui.button('Light', on_click=dark.disable)
  8. ui.button('Auto', on_click=dark.auto)
  9. ui.button('Toggle', on_click=dark.toggle)
  10. def assert_dark(value: bool) -> None:
  11. classes = (screen.find_by_tag('body').get_attribute('class') or '').split()
  12. assert ('dark' in classes) == value
  13. assert ('body--dark' in classes) == value
  14. assert ('body--light' in classes) != value
  15. screen.open('/')
  16. screen.should_contain('Hello')
  17. assert_dark(False)
  18. screen.click('Dark')
  19. screen.wait(0.5)
  20. assert_dark(True)
  21. screen.click('Auto')
  22. screen.wait(0.5)
  23. assert_dark(False)
  24. screen.click('Toggle')
  25. screen.wait(0.5)
  26. screen.assert_py_logger('ERROR', 'Cannot toggle dark mode when it is set to auto.')
  27. screen.click('Light')
  28. screen.wait(0.5)
  29. assert_dark(False)
  30. screen.click('Toggle')
  31. screen.wait(0.5)
  32. assert_dark(True)