test_element_delete.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import pytest
  2. from nicegui import binding, ui
  3. from .screen import Screen
  4. def test_remove_element_by_reference(screen: Screen):
  5. texts = {'a': 'Label A', 'b': 'Label B', 'c': 'Label C'}
  6. with ui.row() as row:
  7. ui.label().bind_text_from(texts, 'a')
  8. b = ui.label().bind_text_from(texts, 'b')
  9. ui.label().bind_text_from(texts, 'c')
  10. ui.button('Remove', on_click=lambda: row.remove(b))
  11. screen.open('/')
  12. screen.click('Remove')
  13. screen.wait(0.5)
  14. screen.should_contain('Label A')
  15. screen.should_not_contain('Label B')
  16. screen.should_contain('Label C')
  17. assert b.is_deleted
  18. assert b.id not in row.client.elements
  19. assert len(row.default_slot.children) == 2
  20. assert len(binding.active_links) == 2
  21. def test_remove_element_by_index(screen: Screen):
  22. texts = {'a': 'Label A', 'b': 'Label B', 'c': 'Label C'}
  23. with ui.row() as row:
  24. ui.label().bind_text_from(texts, 'a')
  25. b = ui.label().bind_text_from(texts, 'b')
  26. ui.label().bind_text_from(texts, 'c')
  27. ui.button('Remove', on_click=lambda: row.remove(1))
  28. screen.open('/')
  29. screen.click('Remove')
  30. screen.wait(0.5)
  31. screen.should_contain('Label A')
  32. screen.should_not_contain('Label B')
  33. screen.should_contain('Label C')
  34. assert b.is_deleted
  35. assert b.id not in row.client.elements
  36. assert len(row.default_slot.children) == 2
  37. assert len(binding.active_links) == 2
  38. def test_clear(screen: Screen):
  39. texts = {'a': 'Label A', 'b': 'Label B', 'c': 'Label C'}
  40. with ui.row() as row:
  41. a = ui.label().bind_text_from(texts, 'a')
  42. b = ui.label().bind_text_from(texts, 'b')
  43. c = ui.label().bind_text_from(texts, 'c')
  44. ui.button('Clear', on_click=row.clear)
  45. screen.open('/')
  46. screen.click('Clear')
  47. screen.wait(0.5)
  48. screen.should_not_contain('Label A')
  49. screen.should_not_contain('Label B')
  50. screen.should_not_contain('Label C')
  51. assert a.is_deleted
  52. assert b.is_deleted
  53. assert c.is_deleted
  54. assert b.id not in row.client.elements
  55. assert len(row.default_slot.children) == 0
  56. assert len(binding.active_links) == 0
  57. @pytest.mark.skip(reason='needs fix in element.py') # TODO
  58. def test_remove_parent(screen: Screen):
  59. texts = {'a': 'Label A', 'b': 'Label B', 'c': 'Label C'}
  60. with ui.element() as container:
  61. with ui.row() as row:
  62. a = ui.label().bind_text_from(texts, 'a')
  63. b = ui.label().bind_text_from(texts, 'b')
  64. c = ui.label().bind_text_from(texts, 'c')
  65. ui.button('Remove parent', on_click=lambda: container.remove(row))
  66. screen.open('/')
  67. screen.click('Remove parent')
  68. screen.wait(0.5)
  69. screen.should_not_contain('Label A')
  70. screen.should_not_contain('Label B')
  71. screen.should_not_contain('Label C')
  72. assert row.is_deleted
  73. assert a.is_deleted
  74. assert b.is_deleted
  75. assert c.is_deleted
  76. assert a.id not in container.client.elements
  77. assert b.id not in container.client.elements
  78. assert c.id not in container.client.elements
  79. assert len(container.default_slot.children) == 0
  80. assert len(row.default_slot.children) == 0
  81. assert len(binding.active_links) == 0
  82. @pytest.mark.skip(reason='needs fix in element.py') # TODO
  83. def test_delete_element(screen: Screen):
  84. texts = {'a': 'Label A', 'b': 'Label B', 'c': 'Label C'}
  85. with ui.row() as row:
  86. ui.label().bind_text_from(texts, 'a')
  87. b = ui.label().bind_text_from(texts, 'b')
  88. ui.label().bind_text_from(texts, 'c')
  89. ui.button('Delete', on_click=b.delete)
  90. screen.open('/')
  91. screen.click('Delete')
  92. screen.wait(0.5)
  93. screen.should_contain('Label A')
  94. screen.should_not_contain('Label B')
  95. screen.should_contain('Label C')
  96. assert b.is_deleted
  97. assert b.id not in row.client.elements
  98. assert len(row.default_slot.children) == 2
  99. assert len(binding.active_links) == 2