test_element_delete.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. from nicegui import binding, ui
  2. from .screen import Screen
  3. def test_remove_element_by_reference(screen: Screen):
  4. texts = {'a': 'Label A', 'b': 'Label B', 'c': 'Label C'}
  5. with ui.row() as row:
  6. ui.label().bind_text_from(texts, 'a')
  7. b = ui.label().bind_text_from(texts, 'b')
  8. ui.label().bind_text_from(texts, '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 b.id not in row.client.elements
  18. assert len(row.default_slot.children) == 2
  19. assert len(binding.active_links) == 2
  20. def test_remove_element_by_index(screen: Screen):
  21. texts = {'a': 'Label A', 'b': 'Label B', 'c': 'Label C'}
  22. with ui.row() as row:
  23. ui.label().bind_text_from(texts, 'a')
  24. b = ui.label().bind_text_from(texts, 'b')
  25. ui.label().bind_text_from(texts, 'c')
  26. ui.button('Remove', on_click=lambda: row.remove(1))
  27. screen.open('/')
  28. screen.click('Remove')
  29. screen.wait(0.5)
  30. screen.should_contain('Label A')
  31. screen.should_not_contain('Label B')
  32. screen.should_contain('Label C')
  33. assert b.is_deleted
  34. assert b.id not in row.client.elements
  35. assert len(row.default_slot.children) == 2
  36. assert len(binding.active_links) == 2
  37. def test_clear(screen: Screen):
  38. texts = {'a': 'Label A', 'b': 'Label B', 'c': 'Label C'}
  39. with ui.row() as row:
  40. a = ui.label().bind_text_from(texts, 'a')
  41. b = ui.label().bind_text_from(texts, 'b')
  42. c = ui.label().bind_text_from(texts, 'c')
  43. ui.button('Clear', on_click=row.clear)
  44. screen.open('/')
  45. screen.click('Clear')
  46. screen.wait(0.5)
  47. screen.should_not_contain('Label A')
  48. screen.should_not_contain('Label B')
  49. screen.should_not_contain('Label C')
  50. assert a.is_deleted
  51. assert b.is_deleted
  52. assert c.is_deleted
  53. assert b.id not in row.client.elements
  54. assert len(row.default_slot.children) == 0
  55. assert len(binding.active_links) == 0
  56. def test_remove_parent(screen: Screen):
  57. texts = {'a': 'Label A', 'b': 'Label B', 'c': 'Label C'}
  58. with ui.element() as container:
  59. with ui.row() as row:
  60. a = ui.label().bind_text_from(texts, 'a')
  61. b = ui.label().bind_text_from(texts, 'b')
  62. c = ui.label().bind_text_from(texts, 'c')
  63. ui.button('Remove parent', on_click=lambda: container.remove(row))
  64. screen.open('/')
  65. screen.click('Remove parent')
  66. screen.wait(0.5)
  67. screen.should_not_contain('Label A')
  68. screen.should_not_contain('Label B')
  69. screen.should_not_contain('Label C')
  70. assert row.is_deleted
  71. assert a.is_deleted
  72. assert b.is_deleted
  73. assert c.is_deleted
  74. assert a.id not in container.client.elements
  75. assert b.id not in container.client.elements
  76. assert c.id not in container.client.elements
  77. assert len(container.default_slot.children) == 0
  78. assert len(binding.active_links) == 0
  79. def test_delete_element(screen: Screen):
  80. texts = {'a': 'Label A', 'b': 'Label B', 'c': 'Label C'}
  81. with ui.row() as row:
  82. ui.label().bind_text_from(texts, 'a')
  83. b = ui.label().bind_text_from(texts, 'b')
  84. ui.label().bind_text_from(texts, 'c')
  85. ui.button('Delete', on_click=b.delete)
  86. screen.open('/')
  87. screen.click('Delete')
  88. screen.wait(0.5)
  89. screen.should_contain('Label A')
  90. screen.should_not_contain('Label B')
  91. screen.should_contain('Label C')
  92. assert b.is_deleted
  93. assert b.id not in row.client.elements
  94. assert len(row.default_slot.children) == 2
  95. assert len(binding.active_links) == 2
  96. def test_on_delete(screen: Screen):
  97. deleted_labels = []
  98. class CustomLabel(ui.label):
  99. def __init__(self, text: str) -> None:
  100. super().__init__(text)
  101. def _handle_delete(self) -> None:
  102. deleted_labels.append(self.text)
  103. super()._handle_delete()
  104. with ui.row() as row:
  105. CustomLabel('Label A')
  106. b = CustomLabel('Label B')
  107. CustomLabel('Label C')
  108. ui.button('Delete C', on_click=lambda: row.remove(2))
  109. ui.button('Delete B', on_click=lambda: row.remove(b))
  110. ui.button('Clear row', on_click=row.clear)
  111. screen.open('/')
  112. screen.click('Delete C')
  113. screen.click('Delete B')
  114. screen.click('Clear row')
  115. screen.wait(0.5)
  116. assert deleted_labels == ['Label C', 'Label B', 'Label A']