-
-
Notifications
You must be signed in to change notification settings - Fork 615
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1514 from zauberzeug/clear-remove-delete
Fix and improve clear, remove and delete methods
- Loading branch information
Showing
10 changed files
with
211 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
from nicegui import binding, ui | ||
|
||
from .screen import Screen | ||
|
||
|
||
def test_remove_element_by_reference(screen: Screen): | ||
texts = {'a': 'Label A', 'b': 'Label B', 'c': 'Label C'} | ||
with ui.row() as row: | ||
ui.label().bind_text_from(texts, 'a') | ||
b = ui.label().bind_text_from(texts, 'b') | ||
ui.label().bind_text_from(texts, 'c') | ||
|
||
ui.button('Remove', on_click=lambda: row.remove(b)) | ||
|
||
screen.open('/') | ||
screen.click('Remove') | ||
screen.wait(0.5) | ||
screen.should_contain('Label A') | ||
screen.should_not_contain('Label B') | ||
screen.should_contain('Label C') | ||
assert b.is_deleted | ||
assert b.id not in row.client.elements | ||
assert len(row.default_slot.children) == 2 | ||
assert len(binding.active_links) == 2 | ||
|
||
|
||
def test_remove_element_by_index(screen: Screen): | ||
texts = {'a': 'Label A', 'b': 'Label B', 'c': 'Label C'} | ||
with ui.row() as row: | ||
ui.label().bind_text_from(texts, 'a') | ||
b = ui.label().bind_text_from(texts, 'b') | ||
ui.label().bind_text_from(texts, 'c') | ||
|
||
ui.button('Remove', on_click=lambda: row.remove(1)) | ||
|
||
screen.open('/') | ||
screen.click('Remove') | ||
screen.wait(0.5) | ||
screen.should_contain('Label A') | ||
screen.should_not_contain('Label B') | ||
screen.should_contain('Label C') | ||
assert b.is_deleted | ||
assert b.id not in row.client.elements | ||
assert len(row.default_slot.children) == 2 | ||
assert len(binding.active_links) == 2 | ||
|
||
|
||
def test_clear(screen: Screen): | ||
texts = {'a': 'Label A', 'b': 'Label B', 'c': 'Label C'} | ||
with ui.row() as row: | ||
a = ui.label().bind_text_from(texts, 'a') | ||
b = ui.label().bind_text_from(texts, 'b') | ||
c = ui.label().bind_text_from(texts, 'c') | ||
|
||
ui.button('Clear', on_click=row.clear) | ||
|
||
screen.open('/') | ||
screen.click('Clear') | ||
screen.wait(0.5) | ||
screen.should_not_contain('Label A') | ||
screen.should_not_contain('Label B') | ||
screen.should_not_contain('Label C') | ||
assert a.is_deleted | ||
assert b.is_deleted | ||
assert c.is_deleted | ||
assert b.id not in row.client.elements | ||
assert len(row.default_slot.children) == 0 | ||
assert len(binding.active_links) == 0 | ||
|
||
|
||
def test_remove_parent(screen: Screen): | ||
texts = {'a': 'Label A', 'b': 'Label B', 'c': 'Label C'} | ||
with ui.element() as container: | ||
with ui.row() as row: | ||
a = ui.label().bind_text_from(texts, 'a') | ||
b = ui.label().bind_text_from(texts, 'b') | ||
c = ui.label().bind_text_from(texts, 'c') | ||
|
||
ui.button('Remove parent', on_click=lambda: container.remove(row)) | ||
|
||
screen.open('/') | ||
screen.click('Remove parent') | ||
screen.wait(0.5) | ||
screen.should_not_contain('Label A') | ||
screen.should_not_contain('Label B') | ||
screen.should_not_contain('Label C') | ||
assert row.is_deleted | ||
assert a.is_deleted | ||
assert b.is_deleted | ||
assert c.is_deleted | ||
assert a.id not in container.client.elements | ||
assert b.id not in container.client.elements | ||
assert c.id not in container.client.elements | ||
assert len(container.default_slot.children) == 0 | ||
assert len(binding.active_links) == 0 | ||
|
||
|
||
def test_delete_element(screen: Screen): | ||
texts = {'a': 'Label A', 'b': 'Label B', 'c': 'Label C'} | ||
with ui.row() as row: | ||
ui.label().bind_text_from(texts, 'a') | ||
b = ui.label().bind_text_from(texts, 'b') | ||
ui.label().bind_text_from(texts, 'c') | ||
|
||
ui.button('Delete', on_click=b.delete) | ||
|
||
screen.open('/') | ||
screen.click('Delete') | ||
screen.wait(0.5) | ||
screen.should_contain('Label A') | ||
screen.should_not_contain('Label B') | ||
screen.should_contain('Label C') | ||
assert b.is_deleted | ||
assert b.id not in row.client.elements | ||
assert len(row.default_slot.children) == 2 | ||
assert len(binding.active_links) == 2 | ||
|
||
|
||
def test_on_delete(screen: Screen): | ||
deleted_labels = [] | ||
|
||
class CustomLabel(ui.label): | ||
|
||
def __init__(self, text: str) -> None: | ||
super().__init__(text) | ||
|
||
def _on_delete(self) -> None: | ||
deleted_labels.append(self.text) | ||
super()._on_delete() | ||
|
||
with ui.row() as row: | ||
CustomLabel('Label A') | ||
b = CustomLabel('Label B') | ||
CustomLabel('Label C') | ||
|
||
ui.button('Delete C', on_click=lambda: row.remove(2)) | ||
ui.button('Delete B', on_click=lambda: row.remove(b)) | ||
ui.button('Clear row', on_click=row.clear) | ||
|
||
screen.open('/') | ||
screen.click('Delete C') | ||
screen.click('Delete B') | ||
screen.click('Clear row') | ||
screen.wait(0.5) | ||
assert deleted_labels == ['Label C', 'Label B', 'Label A'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters