Skip to content

Commit

Permalink
Add unit testing for TabbedContent adding before/after
Browse files Browse the repository at this point in the history
  • Loading branch information
davep committed Jun 15, 2023
1 parent e4b4aad commit 832208b
Showing 1 changed file with 135 additions and 1 deletion.
136 changes: 135 additions & 1 deletion tests/test_tabbed_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from textual.app import App, ComposeResult
from textual.reactive import var
from textual.widgets import Label, TabbedContent, TabPane
from textual.widgets import Label, Tab, TabbedContent, TabPane, Tabs


async def test_tabbed_content_switch_via_ui():
Expand Down Expand Up @@ -200,6 +200,140 @@ def compose(self) -> ComposeResult:
assert tabbed_content.active == "initial-1"


async def test_tabbed_content_add_before_id():
class TabbedApp(App[None]):
def compose(self) -> ComposeResult:
with TabbedContent():
yield TabPane("Test 1", id="initial-1")

async with TabbedApp().run_test() as pilot:
tabbed_content = pilot.app.query_one(TabbedContent)
assert tabbed_content.tab_count == 1
assert tabbed_content.active == "initial-1"
await tabbed_content.add_pane(TabPane("Added", id="new-1"), before="initial-1")
await pilot.pause()
assert tabbed_content.tab_count == 2
assert tabbed_content.active == "initial-1"
assert [tab.id for tab in tabbed_content.query(Tab).results(Tab)] == [
"new-1",
"initial-1",
]


async def test_tabbed_content_add_before_pane():
class TabbedApp(App[None]):
def compose(self) -> ComposeResult:
with TabbedContent():
yield TabPane("Test 1", id="initial-1")

async with TabbedApp().run_test() as pilot:
tabbed_content = pilot.app.query_one(TabbedContent)
assert tabbed_content.tab_count == 1
assert tabbed_content.active == "initial-1"
await tabbed_content.add_pane(
TabPane("Added", id="new-1"),
before=pilot.app.query_one("TabPane#initial-1", TabPane),
)
await pilot.pause()
assert tabbed_content.tab_count == 2
assert tabbed_content.active == "initial-1"
assert [tab.id for tab in tabbed_content.query(Tab).results(Tab)] == [
"new-1",
"initial-1",
]


async def test_tabbed_content_add_before_badly():
class TabbedApp(App[None]):
def compose(self) -> ComposeResult:
with TabbedContent():
yield TabPane("Test 1", id="initial-1")

async with TabbedApp().run_test() as pilot:
tabbed_content = pilot.app.query_one(TabbedContent)
assert tabbed_content.tab_count == 1
assert tabbed_content.active == "initial-1"
with pytest.raises(Tabs.TabError):
await tabbed_content.add_pane(
TabPane("Added", id="new-1"), before="unknown-1"
)


async def test_tabbed_content_add_after():
class TabbedApp(App[None]):
def compose(self) -> ComposeResult:
with TabbedContent():
yield TabPane("Test 1", id="initial-1")

async with TabbedApp().run_test() as pilot:
tabbed_content = pilot.app.query_one(TabbedContent)
assert tabbed_content.tab_count == 1
assert tabbed_content.active == "initial-1"
await tabbed_content.add_pane(TabPane("Added", id="new-1"), after="initial-1")
await pilot.pause()
assert tabbed_content.tab_count == 2
assert tabbed_content.active == "initial-1"
assert [tab.id for tab in tabbed_content.query(Tab).results(Tab)] == [
"initial-1",
"new-1",
]


async def test_tabbed_content_add_after_pane():
class TabbedApp(App[None]):
def compose(self) -> ComposeResult:
with TabbedContent():
yield TabPane("Test 1", id="initial-1")

async with TabbedApp().run_test() as pilot:
tabbed_content = pilot.app.query_one(TabbedContent)
assert tabbed_content.tab_count == 1
assert tabbed_content.active == "initial-1"
await tabbed_content.add_pane(
TabPane("Added", id="new-1"),
after=pilot.app.query_one("TabPane#initial-1", TabPane),
)
await pilot.pause()
assert tabbed_content.tab_count == 2
assert tabbed_content.active == "initial-1"
assert [tab.id for tab in tabbed_content.query(Tab).results(Tab)] == [
"initial-1",
"new-1",
]


async def test_tabbed_content_add_after_badly():
class TabbedApp(App[None]):
def compose(self) -> ComposeResult:
with TabbedContent():
yield TabPane("Test 1", id="initial-1")

async with TabbedApp().run_test() as pilot:
tabbed_content = pilot.app.query_one(TabbedContent)
assert tabbed_content.tab_count == 1
assert tabbed_content.active == "initial-1"
with pytest.raises(Tabs.TabError):
await tabbed_content.add_pane(
TabPane("Added", id="new-1"), after="unknown-1"
)


async def test_tabbed_content_add_before_and_after():
class TabbedApp(App[None]):
def compose(self) -> ComposeResult:
with TabbedContent():
yield TabPane("Test 1", id="initial-1")

async with TabbedApp().run_test() as pilot:
tabbed_content = pilot.app.query_one(TabbedContent)
assert tabbed_content.tab_count == 1
assert tabbed_content.active == "initial-1"
with pytest.raises(Tabs.TabError):
await tabbed_content.add_pane(
TabPane("Added", id="new-1"), before="initial-1", after="initial-1"
)


async def test_tabbed_content_removal():
class TabbedApp(App[None]):
cleared: var[int] = var(0)
Expand Down

0 comments on commit 832208b

Please sign in to comment.