|
| 1 | +from pathlib import Path, PurePosixPath |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from textual.app import App |
| 6 | +from textual.widgets import Input, Button |
| 7 | + |
| 8 | + |
| 9 | +# --- Layout related stuff --- |
| 10 | + |
1 | 11 | def test_grid_layout_basic(snap_compare): |
2 | 12 | assert snap_compare("docs/examples/guide/layout/grid_layout1.py") |
3 | 13 |
|
@@ -26,7 +36,73 @@ def test_dock_layout_sidebar(snap_compare): |
26 | 36 | assert snap_compare("docs/examples/guide/layout/dock_layout2_sidebar.py") |
27 | 37 |
|
28 | 38 |
|
| 39 | +# --- Widgets - rendering and basic interactions --- |
| 40 | +# Each widget should have a canonical example that is display in the docs. |
| 41 | +# When adding a new widget, ideally we should also create a snapshot test |
| 42 | +# from these examples which test rendering and simple interactions with it. |
| 43 | + |
29 | 44 | def test_checkboxes(snap_compare): |
30 | 45 | """Tests checkboxes but also acts a regression test for using |
31 | 46 | width: auto in a Horizontal layout context.""" |
32 | | - assert snap_compare("docs/examples/widgets/checkbox.py") |
| 47 | + press = [ |
| 48 | + "shift+tab", |
| 49 | + "enter", # toggle off |
| 50 | + "shift+tab", |
| 51 | + "wait:20", |
| 52 | + "enter", # toggle on |
| 53 | + "wait:20", |
| 54 | + ] |
| 55 | + assert snap_compare("docs/examples/widgets/checkbox.py", press=press) |
| 56 | + |
| 57 | + |
| 58 | +def test_input_and_focus(snap_compare): |
| 59 | + press = [ |
| 60 | + "tab", |
| 61 | + *"Darren", # Focus first input, write "Darren" |
| 62 | + "tab", |
| 63 | + *"Burns", # Tab focus to second input, write "Burns" |
| 64 | + ] |
| 65 | + assert snap_compare("docs/examples/widgets/input.py", press=press) |
| 66 | + |
| 67 | + # Assert that the state of the Input is what we'd expect |
| 68 | + app: App = snap_compare.app |
| 69 | + input: Input = app.query_one(Input) |
| 70 | + assert input.value == "Darren" |
| 71 | + assert input.cursor_position == 6 |
| 72 | + assert input.view_position == 0 |
| 73 | + |
| 74 | + |
| 75 | +def test_buttons_render(snap_compare): |
| 76 | + # Testing button rendering. We press tab to focus the first button too. |
| 77 | + assert snap_compare("docs/examples/widgets/button.py", press=["tab"]) |
| 78 | + |
| 79 | + app = snap_compare.app |
| 80 | + button: Button = app.query_one(Button) |
| 81 | + assert app.focused is button |
| 82 | + |
| 83 | + |
| 84 | +def test_datatable_render(snap_compare): |
| 85 | + press = ["tab", "down", "down", "right", "up", "left"] |
| 86 | + assert snap_compare("docs/examples/widgets/data_table.py", press=press) |
| 87 | + |
| 88 | + |
| 89 | +def test_footer_render(snap_compare): |
| 90 | + assert snap_compare("docs/examples/widgets/footer.py") |
| 91 | + |
| 92 | + |
| 93 | +def test_header_render(snap_compare): |
| 94 | + assert snap_compare("docs/examples/widgets/header.py") |
| 95 | + |
| 96 | + |
| 97 | +# --- CSS properties --- |
| 98 | +# We have a canonical example for each CSS property that is shown in their docs. |
| 99 | +# If any of these change, something has likely broken, so snapshot each of them. |
| 100 | + |
| 101 | +PATHS = [ |
| 102 | + str(PurePosixPath(path)) for path in Path("docs/examples/styles").iterdir() if path.suffix == ".py" |
| 103 | +] |
| 104 | + |
| 105 | + |
| 106 | +@pytest.mark.parametrize("path", PATHS) |
| 107 | +def test_css_property_snapshot(path, snap_compare): |
| 108 | + assert snap_compare(path) |
0 commit comments