Testing to ensure the hover coordinate is updated

This commit is contained in:
Darren Burns
2023-02-07 11:23:02 +00:00
parent 0afcc8cd47
commit 3cf010ebe7
2 changed files with 45 additions and 14 deletions

View File

@@ -1128,9 +1128,6 @@ class DataTable(ScrollView, Generic[CellType], can_focus=True):
ordered_row.append(cell) ordered_row.append(cell)
empty = Text() empty = Text()
if ordered_row is None:
return [empty for _ in self.columns]
else:
return [ return [
Text() if datum is None else default_cell_formatter(datum) or empty Text() if datum is None else default_cell_formatter(datum) or empty
for datum, _ in zip_longest(ordered_row, range(len(self.columns))) for datum, _ in zip_longest(ordered_row, range(len(self.columns)))

View File

@@ -1,11 +1,12 @@
from __future__ import annotations from __future__ import annotations
import pytest import pytest
from rich.style import Style
from textual._wait import wait_for_idle from textual._wait import wait_for_idle
from textual.app import App from textual.app import App
from textual.coordinate import Coordinate from textual.coordinate import Coordinate
from textual.events import Click from textual.events import Click, MouseMove
from textual.message import Message from textual.message import Message
from textual.message_pump import MessagePump from textual.message_pump import MessagePump
from textual.widgets import DataTable from textual.widgets import DataTable
@@ -421,7 +422,7 @@ async def test_coordinate_to_cell_key_invalid_coordinate():
table.coordinate_to_cell_key(Coordinate(9999, 9999)) table.coordinate_to_cell_key(Coordinate(9999, 9999))
def click_in_app(sender: MessagePump): def make_click_event(sender: MessagePump):
return Click( return Click(
sender=sender, sender=sender,
x=1, x=1,
@@ -436,11 +437,13 @@ def click_in_app(sender: MessagePump):
async def test_datatable_on_click_cell_cursor(): async def test_datatable_on_click_cell_cursor():
# Regression test for https://github.com/Textualize/textual/issues/1723 """When the cell cursor is used, and we click, we emit a CellHighlighted
*and* a CellSelected message for the cell that was clicked.
Regression test for https://github.com/Textualize/textual/issues/1723"""
app = DataTableApp() app = DataTableApp()
async with app.run_test(): async with app.run_test():
table = app.query_one(DataTable) table = app.query_one(DataTable)
click = click_in_app(app) click = make_click_event(app)
column_key = table.add_column("ABC") column_key = table.add_column("ABC")
table.add_row("123") table.add_row("123")
row_key = table.add_row("456") row_key = table.add_row("456")
@@ -467,11 +470,13 @@ async def test_datatable_on_click_cell_cursor():
async def test_datatable_on_click_row_cursor(): async def test_datatable_on_click_row_cursor():
"""When the row cursor is used, and we click, we emit a RowHighlighted
*and* a RowSelected message for the row that was clicked."""
app = DataTableApp() app = DataTableApp()
async with app.run_test(): async with app.run_test():
table = app.query_one(DataTable) table = app.query_one(DataTable)
table.cursor_type = "row" table.cursor_type = "row"
click = click_in_app(app) click = make_click_event(app)
table.add_column("ABC") table.add_column("ABC")
table.add_row("123") table.add_row("123")
row_key = table.add_row("456") row_key = table.add_row("456")
@@ -491,14 +496,16 @@ async def test_datatable_on_click_row_cursor():
async def test_datatable_on_click_column_cursor(): async def test_datatable_on_click_column_cursor():
"""When the column cursor is used, and we click, we emit a ColumnHighlighted
*and* a ColumnSelected message for the column that was clicked."""
app = DataTableApp() app = DataTableApp()
async with app.run_test(): async with app.run_test():
table = app.query_one(DataTable) table = app.query_one(DataTable)
table.cursor_type = "column" table.cursor_type = "column"
click = click_in_app(app)
column_key = table.add_column("ABC") column_key = table.add_column("ABC")
table.add_row("123") table.add_row("123")
table.add_row("123") table.add_row("456")
click = make_click_event(app)
table.on_click(event=click) table.on_click(event=click)
await wait_for_idle(0) await wait_for_idle(0)
assert app.message_names == [ assert app.message_names == [
@@ -517,6 +524,33 @@ async def test_datatable_on_click_column_cursor():
assert column_highlighted.cursor_column == 0 assert column_highlighted.cursor_column == 0
async def test_datatable_hover_coordinate():
"""Ensure that the hover_coordinate reactive is updated as expected."""
app = DataTableApp()
async with app.run_test():
table = app.query_one(DataTable)
table.add_column("ABC")
table.add_row("123")
table.add_row("456")
assert table.hover_coordinate == Coordinate(0, 0)
mouse_move = MouseMove(
sender=app,
x=1,
y=2,
delta_x=0,
delta_y=0,
button=0,
shift=False,
meta=False,
ctrl=False,
style=Style(meta={"row": 1, "column": 2}),
)
table.on_mouse_move(mouse_move)
await wait_for_idle(0)
assert table.hover_coordinate == Coordinate(1, 2)
def test_key_equals_equivalent_string(): def test_key_equals_equivalent_string():
text = "Hello" text = "Hello"
key = RowKey(text) key = RowKey(text)