test_element_delete.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import pytest
  2. from nicegui import ui
  3. from .screen import Screen
  4. def test_remove_element_by_reference(screen: Screen):
  5. with ui.row() as row:
  6. ui.label('Label A')
  7. b = ui.label('Label B')
  8. ui.label('Label C')
  9. ui.button('Remove', on_click=lambda: row.remove(b))
  10. screen.open('/')
  11. screen.click('Remove')
  12. screen.wait(0.5)
  13. screen.should_contain('Label A')
  14. screen.should_not_contain('Label B')
  15. screen.should_contain('Label C')
  16. assert b.is_deleted
  17. def test_remove_element_by_index(screen: Screen):
  18. with ui.row() as row:
  19. ui.label('Label A')
  20. b = ui.label('Label B')
  21. ui.label('Label C')
  22. ui.button('Remove', on_click=lambda: row.remove(1))
  23. screen.open('/')
  24. screen.click('Remove')
  25. screen.wait(0.5)
  26. screen.should_contain('Label A')
  27. screen.should_not_contain('Label B')
  28. screen.should_contain('Label C')
  29. assert b.is_deleted
  30. def test_clear(screen: Screen):
  31. with ui.row() as row:
  32. a = ui.label('Label A')
  33. b = ui.label('Label B')
  34. c = ui.label('Label C')
  35. ui.button('Clear', on_click=row.clear)
  36. screen.open('/')
  37. screen.click('Clear')
  38. screen.wait(0.5)
  39. screen.should_not_contain('Label A')
  40. screen.should_not_contain('Label B')
  41. screen.should_not_contain('Label C')
  42. assert a.is_deleted
  43. assert b.is_deleted
  44. assert c.is_deleted
  45. @pytest.mark.skip(reason='needs fix in element.py') # TODO
  46. def test_remove_parent(screen: Screen):
  47. with ui.element() as container:
  48. with ui.row() as row:
  49. a = ui.label('Label A')
  50. b = ui.label('Label B')
  51. c = ui.label('Label C')
  52. ui.button('Remove parent', on_click=lambda: container.remove(row))
  53. screen.open('/')
  54. screen.click('Remove parent')
  55. screen.wait(0.5)
  56. screen.should_not_contain('Label A')
  57. screen.should_not_contain('Label B')
  58. screen.should_not_contain('Label C')
  59. assert row.is_deleted
  60. assert a.is_deleted
  61. assert b.is_deleted
  62. assert c.is_deleted