Repository: Textualize/textual
File: tests/snapshot_tests/test_snapshots.py (Python)

Code before:
        BINDINGS = [("z", "toggle_console", "Console")]
        CSS = """
        RichLog { border-top: dashed blue; height: 6; }
        .hidden { display: none; }
        """

        def compose(self):
            yield VerticalScroll()
            yield ProgressBar(
                classes="hidden"
            )  # removing or displaying this widget prevents the bug
            yield Footer()  # clicking "Console" in the footer prevents the bug
            yield RichLog(classes="hidden")

        def on_ready(self) -> None:
            self.query_one(RichLog).write("\n".join(f"line #{i}" for i in range(5)))

        def action_toggle_console(self) -> None:
            self.query_one(RichLog).toggle_class("hidden")

    app = MRE()
    assert snap_compare(app, press=["space", "space", "z"])


def test_updates_with_auto_refresh(snap_compare):
    """Regression test for https://github.com/Textualize/textual/issues/5056"""

    class MRE(App):
        BINDINGS = [
            ("z", "toggle_widget('RichLog')", "Console"),
            ("x", "toggle_widget('ProgressBar')", "Progress bar"),
        ]
        CSS = """
        Placeholder { height: 15; }
        RichLog { border-top: dashed blue; height: 6; }
        .hidden { display: none; }
        """

        def compose(self):
            with VerticalScroll():
                for i in range(10):
                    yield Placeholder()
            yield ProgressBar(classes="hidden")
            yield RichLog(classes="hidden")
            yield Footer()

        def on_ready(self) -> None:
            self.query_one(RichLog).write("\n".join(f"line #{i}" for i in range(5)))

        def action_toggle_widget(self, widget_type: str) -> None:
            self.query_one(widget_type).toggle_class("hidden")

Review comment (bug):
This is hard to follow because it contains no context unless I click through to GitHub, and also contains some redundancy that might make sense in the MRE, but not in the test. If it fails, we should be able to validate the failure without following a paper-trail.

I think it's better to refine MREs a bit when translating them into tests.

```suggestion
    """Regression test for https://github.com/Textualize/textual/issues/5056
    
    After hiding and unhiding the RichLog, you should be able to see 1.5 fully rendered placeholder widgets.
    Prior to this fix, the bottom portion of the screen did not 
    refresh after the RichLog was hidden/unhidden while in the presence of the auto-refreshing ProgressBar widget.
    """
```

Code after:

    Footer should show bindings `k` to Increment, and `down` to Decrement.

    Key panel should show bindings `k, plus` to increment,
    and `down, minus, j` to decrement.

    """

    class Counter(App[None]):
        BINDINGS = [
            Binding(
                key="i,up",
                action="increment",
                description="Increment",
                id="app.increment",
            ),
            Binding(
                key="d,down",
                action="decrement",
                description="Decrement",
                id="app.decrement",
            ),
        ]

        def compose(self) -> ComposeResult:
            yield Label("Counter")
            yield Footer()

        def on_mount(self) -> None:
            self.action_show_help_panel()
            self.set_keymap(
                {
                    "app.increment": "k,plus",
                    "app.decrement": "down,minus,j",
                }
            )

    assert snap_compare(Counter())


def test_keymap_bindings_key_display(snap_compare):
    """If a default binding in `BINDINGS` has a key_display, it should be reset
    when that binding is overridden by a Keymap.

    The key_display should be taken from `App.get_key_display`, so in this case
    it should be "THIS IS CORRECT" in the Footer and help panel, not "INCORRECT".
    """

    class MyApp(App[None]):
        BINDINGS = [
            Binding(