Skip to content

Commit 832208b

Browse files
committed
Add unit testing for TabbedContent adding before/after
1 parent e4b4aad commit 832208b

File tree

1 file changed

+135
-1
lines changed

1 file changed

+135
-1
lines changed

tests/test_tabbed_content.py

Lines changed: 135 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from textual.app import App, ComposeResult
44
from textual.reactive import var
5-
from textual.widgets import Label, TabbedContent, TabPane
5+
from textual.widgets import Label, Tab, TabbedContent, TabPane, Tabs
66

77

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

202202

203+
async def test_tabbed_content_add_before_id():
204+
class TabbedApp(App[None]):
205+
def compose(self) -> ComposeResult:
206+
with TabbedContent():
207+
yield TabPane("Test 1", id="initial-1")
208+
209+
async with TabbedApp().run_test() as pilot:
210+
tabbed_content = pilot.app.query_one(TabbedContent)
211+
assert tabbed_content.tab_count == 1
212+
assert tabbed_content.active == "initial-1"
213+
await tabbed_content.add_pane(TabPane("Added", id="new-1"), before="initial-1")
214+
await pilot.pause()
215+
assert tabbed_content.tab_count == 2
216+
assert tabbed_content.active == "initial-1"
217+
assert [tab.id for tab in tabbed_content.query(Tab).results(Tab)] == [
218+
"new-1",
219+
"initial-1",
220+
]
221+
222+
223+
async def test_tabbed_content_add_before_pane():
224+
class TabbedApp(App[None]):
225+
def compose(self) -> ComposeResult:
226+
with TabbedContent():
227+
yield TabPane("Test 1", id="initial-1")
228+
229+
async with TabbedApp().run_test() as pilot:
230+
tabbed_content = pilot.app.query_one(TabbedContent)
231+
assert tabbed_content.tab_count == 1
232+
assert tabbed_content.active == "initial-1"
233+
await tabbed_content.add_pane(
234+
TabPane("Added", id="new-1"),
235+
before=pilot.app.query_one("TabPane#initial-1", TabPane),
236+
)
237+
await pilot.pause()
238+
assert tabbed_content.tab_count == 2
239+
assert tabbed_content.active == "initial-1"
240+
assert [tab.id for tab in tabbed_content.query(Tab).results(Tab)] == [
241+
"new-1",
242+
"initial-1",
243+
]
244+
245+
246+
async def test_tabbed_content_add_before_badly():
247+
class TabbedApp(App[None]):
248+
def compose(self) -> ComposeResult:
249+
with TabbedContent():
250+
yield TabPane("Test 1", id="initial-1")
251+
252+
async with TabbedApp().run_test() as pilot:
253+
tabbed_content = pilot.app.query_one(TabbedContent)
254+
assert tabbed_content.tab_count == 1
255+
assert tabbed_content.active == "initial-1"
256+
with pytest.raises(Tabs.TabError):
257+
await tabbed_content.add_pane(
258+
TabPane("Added", id="new-1"), before="unknown-1"
259+
)
260+
261+
262+
async def test_tabbed_content_add_after():
263+
class TabbedApp(App[None]):
264+
def compose(self) -> ComposeResult:
265+
with TabbedContent():
266+
yield TabPane("Test 1", id="initial-1")
267+
268+
async with TabbedApp().run_test() as pilot:
269+
tabbed_content = pilot.app.query_one(TabbedContent)
270+
assert tabbed_content.tab_count == 1
271+
assert tabbed_content.active == "initial-1"
272+
await tabbed_content.add_pane(TabPane("Added", id="new-1"), after="initial-1")
273+
await pilot.pause()
274+
assert tabbed_content.tab_count == 2
275+
assert tabbed_content.active == "initial-1"
276+
assert [tab.id for tab in tabbed_content.query(Tab).results(Tab)] == [
277+
"initial-1",
278+
"new-1",
279+
]
280+
281+
282+
async def test_tabbed_content_add_after_pane():
283+
class TabbedApp(App[None]):
284+
def compose(self) -> ComposeResult:
285+
with TabbedContent():
286+
yield TabPane("Test 1", id="initial-1")
287+
288+
async with TabbedApp().run_test() as pilot:
289+
tabbed_content = pilot.app.query_one(TabbedContent)
290+
assert tabbed_content.tab_count == 1
291+
assert tabbed_content.active == "initial-1"
292+
await tabbed_content.add_pane(
293+
TabPane("Added", id="new-1"),
294+
after=pilot.app.query_one("TabPane#initial-1", TabPane),
295+
)
296+
await pilot.pause()
297+
assert tabbed_content.tab_count == 2
298+
assert tabbed_content.active == "initial-1"
299+
assert [tab.id for tab in tabbed_content.query(Tab).results(Tab)] == [
300+
"initial-1",
301+
"new-1",
302+
]
303+
304+
305+
async def test_tabbed_content_add_after_badly():
306+
class TabbedApp(App[None]):
307+
def compose(self) -> ComposeResult:
308+
with TabbedContent():
309+
yield TabPane("Test 1", id="initial-1")
310+
311+
async with TabbedApp().run_test() as pilot:
312+
tabbed_content = pilot.app.query_one(TabbedContent)
313+
assert tabbed_content.tab_count == 1
314+
assert tabbed_content.active == "initial-1"
315+
with pytest.raises(Tabs.TabError):
316+
await tabbed_content.add_pane(
317+
TabPane("Added", id="new-1"), after="unknown-1"
318+
)
319+
320+
321+
async def test_tabbed_content_add_before_and_after():
322+
class TabbedApp(App[None]):
323+
def compose(self) -> ComposeResult:
324+
with TabbedContent():
325+
yield TabPane("Test 1", id="initial-1")
326+
327+
async with TabbedApp().run_test() as pilot:
328+
tabbed_content = pilot.app.query_one(TabbedContent)
329+
assert tabbed_content.tab_count == 1
330+
assert tabbed_content.active == "initial-1"
331+
with pytest.raises(Tabs.TabError):
332+
await tabbed_content.add_pane(
333+
TabPane("Added", id="new-1"), before="initial-1", after="initial-1"
334+
)
335+
336+
203337
async def test_tabbed_content_removal():
204338
class TabbedApp(App[None]):
205339
cleared: var[int] = var(0)

0 commit comments

Comments
 (0)