File: test_events.py

package info (click to toggle)
textual 2.1.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 55,084 kB
  • sloc: python: 85,423; lisp: 1,669; makefile: 101
file content (75 lines) | stat: -rw-r--r-- 2,153 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from typing import Union
from unittest import mock

from textual import on
from textual.app import App
from textual.command import CommandPalette, Hit, Hits, Provider

CommandPaletteEvent = Union[
    CommandPalette.Opened, CommandPalette.Closed, CommandPalette.OptionHighlighted
]


class SimpleSource(Provider):
    async def search(self, query: str) -> Hits:
        def goes_nowhere_does_nothing() -> None:
            pass

        yield Hit(1, query, goes_nowhere_does_nothing, query)


class AppWithActiveCommandPalette(App[None]):
    COMMANDS = {SimpleSource}

    def __init__(self) -> None:
        super().__init__()
        self.events: list[CommandPaletteEvent] = []

    def on_mount(self) -> None:
        self.action_command_palette()

    @on(CommandPalette.Opened)
    @on(CommandPalette.Closed)
    @on(CommandPalette.OptionHighlighted)
    def record_event(
        self,
        event: CommandPaletteEvent,
    ) -> None:
        self.events.append(event)


async def test_command_palette_opened_event():
    app = AppWithActiveCommandPalette()
    async with app.run_test():
        assert app.events == [CommandPalette.Opened()]


async def test_command_palette_closed_event():
    app = AppWithActiveCommandPalette()
    async with app.run_test() as pilot:
        await pilot.press("escape")
        assert app.events == [CommandPalette.Opened(), CommandPalette.Closed(False)]


async def test_command_palette_closed_event_value():
    app = AppWithActiveCommandPalette()
    async with app.run_test() as pilot:
        await pilot.press("a")
        await pilot.press("down")
        await pilot.press("enter")
        assert app.events == [
            CommandPalette.Opened(),
            CommandPalette.OptionHighlighted(mock.ANY),
            CommandPalette.Closed(True),
        ]


async def test_command_palette_option_highlighted_event():
    app = AppWithActiveCommandPalette()
    async with app.run_test() as pilot:
        await pilot.press("a")
        await pilot.press("down")
        assert app.events == [
            CommandPalette.Opened(),
            CommandPalette.OptionHighlighted(mock.ANY),
        ]