test_element_delete.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. assert len(row.default_slot.children) == 2
  18. def test_remove_element_by_index(screen: Screen):
  19. with ui.row() as row:
  20. ui.label('Label A')
  21. b = ui.label('Label B')
  22. ui.label('Label C')
  23. ui.button('Remove', on_click=lambda: row.remove(1))
  24. screen.open('/')
  25. screen.click('Remove')
  26. screen.wait(0.5)
  27. screen.should_contain('Label A')
  28. screen.should_not_contain('Label B')
  29. screen.should_contain('Label C')
  30. assert b.is_deleted
  31. assert len(row.default_slot.children) == 2
  32. def test_clear(screen: Screen):
  33. with ui.row() as row:
  34. a = ui.label('Label A')
  35. b = ui.label('Label B')
  36. c = ui.label('Label C')
  37. ui.button('Clear', on_click=row.clear)
  38. screen.open('/')
  39. screen.click('Clear')
  40. screen.wait(0.5)
  41. screen.should_not_contain('Label A')
  42. screen.should_not_contain('Label B')
  43. screen.should_not_contain('Label C')
  44. assert a.is_deleted
  45. assert b.is_deleted
  46. assert c.is_deleted
  47. assert len(row.default_slot.children) == 0
  48. @pytest.mark.skip(reason='needs fix in element.py') # TODO
  49. def test_remove_parent(screen: Screen):
  50. with ui.element() as container:
  51. with ui.row() as row:
  52. a = ui.label('Label A')
  53. b = ui.label('Label B')
  54. c = ui.label('Label C')
  55. ui.button('Remove parent', on_click=lambda: container.remove(row))
  56. screen.open('/')
  57. screen.click('Remove parent')
  58. screen.wait(0.5)
  59. screen.should_not_contain('Label A')
  60. screen.should_not_contain('Label B')
  61. screen.should_not_contain('Label C')
  62. assert row.is_deleted
  63. assert a.is_deleted
  64. assert b.is_deleted
  65. assert c.is_deleted
  66. @pytest.mark.skip(reason='needs fix in element.py') # TODO
  67. def test_delete_element(screen: Screen):
  68. with ui.row() as row:
  69. ui.label('Label A')
  70. b = ui.label('Label B')
  71. ui.label('Label C')
  72. ui.button('Delete', on_click=b.delete)
  73. screen.open('/')
  74. screen.click('Delete')
  75. screen.wait(0.5)
  76. screen.should_contain('Label A')
  77. screen.should_not_contain('Label B')
  78. screen.should_contain('Label C')
  79. assert b.is_deleted
  80. assert len(row.default_slot.children) == 2