Skip to content

Commit 88ce2e3

Browse files
tests(input_slider): add kitchensink tests for input_slider (#1691)
Co-authored-by: Barret Schloerke <barret@posit.co>
1 parent 54b0402 commit 88ce2e3

File tree

4 files changed

+116
-8
lines changed

4 files changed

+116
-8
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2525

2626
### Bug fixes
2727

28-
* A few fixes for `ui.Chat()`, including:
28+
* A few fixes for `ui.Chat()`, including:
2929
* Fixed a bug with `Chat()` sometimes silently dropping errors. (#1672)
3030
* Fixed a bug with `Chat()` sometimes not removing it's loading icon (on error or a `None` transform). (#1679)
3131
* `.messages(format="anthropic")` correctly removes non-user starting messages (once again). (#1685)
@@ -35,6 +35,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3535

3636
* `ui.Theme()` now works correctly on Windows when the theme requires Sass compilation. (thanks @yuuuxt, #1684)
3737

38+
* Fixed the `InputSlider` controller's `.expect_width()` to check the `width` property within the `style` attribute. (#1691)
39+
3840
## [1.1.0] - 2024-09-03
3941

4042
### New features

shiny/playwright/controller/_input_controls.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
UiWithContainerP,
2121
UiWithLabel,
2222
WidthContainerM,
23-
WidthLocM,
2423
all_missing,
2524
not_is_missing,
2625
)
@@ -30,7 +29,7 @@
3029
)
3130

3231

33-
class _InputSliderBase(WidthLocM, UiWithLabel):
32+
class _InputSliderBase(UiWithLabel):
3433

3534
loc_irs: Locator
3635
"""
@@ -203,6 +202,19 @@ def expect_max(self, value: AttrValue, *, timeout: Timeout = None) -> None:
203202
self.loc, "data-max", value=value, timeout=timeout
204203
)
205204

205+
def expect_width(self, value: str, *, timeout: Timeout = None) -> None:
206+
"""
207+
Expects the slider to have the specified width.
208+
209+
Parameters
210+
----------
211+
value
212+
The expected width.
213+
timeout
214+
The maximum time to wait for the width to be visible and interactable. Defaults to `None`.
215+
"""
216+
_expect_style_to_have_value(self.loc_container, "width", value, timeout=timeout)
217+
206218
def expect_step(self, value: AttrValue, *, timeout: Timeout = None) -> None:
207219
"""
208220
Expect the input element to have the expected `step` attribute value.

tests/playwright/shiny/inputs/input_slider/app.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import datetime
34
import typing
45

56
from shiny import App, Inputs, Outputs, Session, render, ui
@@ -50,6 +51,7 @@ def slider_row(
5051
value=0,
5152
step=2500,
5253
pre="$",
54+
post=".00",
5355
sep=",",
5456
animate=True,
5557
ticks=True,
@@ -70,7 +72,29 @@ def slider_row(
7072
step=1,
7173
animate=ui.AnimationOptions(interval=100, loop=False),
7274
),
73-
# TODO-future; Test dates
75+
slider_row(
76+
"Date format",
77+
min=(datetime.date(2024, 1, 1)),
78+
max=(datetime.date(2024, 1, 10)),
79+
value=(datetime.date(2024, 1, 5)),
80+
time_format="%m/%d/%y",
81+
timezone="0000",
82+
),
83+
slider_row(
84+
"Time format",
85+
min=(datetime.datetime(2024, 1, 1, 0, 0, tzinfo=datetime.timezone.utc)),
86+
max=(datetime.datetime(2024, 1, 10, 23, 59, tzinfo=datetime.timezone.utc)),
87+
value=(datetime.datetime(2024, 1, 5, 12, 00, tzinfo=datetime.timezone.utc)),
88+
width="600px",
89+
timezone="0000",
90+
),
91+
slider_row(
92+
"Drag Range (Disabled)",
93+
min=0,
94+
max=1000,
95+
value=(200, 500),
96+
drag_range=False,
97+
),
7498
)
7599

76100

tests/playwright/shiny/inputs/input_slider/test_input_slider_app.py

Lines changed: 74 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import datetime
12
import re
23
import time
34

@@ -8,6 +9,20 @@
89
from shiny.types import MISSING
910

1011

12+
def convert_to_utc_date(date_str: str) -> str:
13+
date_obj = datetime.datetime.strptime(date_str, "%m/%d/%y")
14+
epoch_time_milliseconds = date_obj.timestamp() * 1000
15+
return str(epoch_time_milliseconds)
16+
17+
18+
def convert_to_utc_date_time(date_time_str: str) -> str:
19+
date_time_obj = datetime.datetime.strptime(date_time_str, "%Y-%m-%d %H:%M:%S")
20+
utc_timestamp = (
21+
date_time_obj.replace(tzinfo=datetime.timezone.utc).timestamp() * 1000
22+
)
23+
return str(utc_timestamp)
24+
25+
1126
def test_slider_regular(page: Page, local_app: ShinyAppProc) -> None:
1227
page.goto(local_app.url)
1328

@@ -71,22 +86,22 @@ def test_slider_custom_format(page: Page, local_app: ShinyAppProc) -> None:
7186

7287
s2 = controller.InputSlider(page, "s2")
7388
s2.expect_label("Custom Format")
74-
s2.expect_value("$0")
89+
s2.expect_value("$0.00")
7590
s2.expect_min("0")
7691
s2.expect_max("10000")
7792
s2.expect_step("2500")
7893
s2.expect_ticks("true")
7994
s2.expect_sep(",")
8095
s2.expect_pre("$")
81-
s2.expect_post(None)
96+
s2.expect_post(".00")
8297
s2.expect_time_format(None)
8398
s2.expect_timezone(None)
8499
s2.expect_drag_range(None)
85100
s2.expect_animate_options(loop=True, interval=500)
86101
controller.OutputTextVerbatim(page, "txt2").expect_value("0")
87102

88-
s2.set("$7,500")
89-
s2.expect_value("$7,500")
103+
s2.set("$7,500.00")
104+
s2.expect_value("$7,500.00")
90105
controller.OutputTextVerbatim(page, "txt2").expect_value("7500")
91106

92107

@@ -147,3 +162,58 @@ def test_slider_play(page: Page, local_app: ShinyAppProc) -> None:
147162
s4.expect_value("5")
148163
s4.click_play() # can click again!
149164
s4.expect_value("5")
165+
166+
167+
def test_slider_date_format(page: Page, local_app: ShinyAppProc) -> None:
168+
page.goto(local_app.url)
169+
170+
s5 = controller.InputSlider(page, "s5")
171+
s5.expect_label("Date format")
172+
s5.expect_value("01/05/24")
173+
s5.expect_min(convert_to_utc_date("01/01/24"))
174+
s5.expect_max(convert_to_utc_date("01/10/24"))
175+
s5.expect_time_format("%m/%d/%y")
176+
s5.expect_timezone("0000")
177+
s5.expect_drag_range(None)
178+
controller.OutputTextVerbatim(page, "txt5").expect_value("2024-01-05")
179+
180+
new_val = "01/08/24"
181+
s5.set(new_val)
182+
s5.expect_value(new_val)
183+
controller.OutputTextVerbatim(page, "txt5").expect_value("2024-01-08")
184+
185+
186+
def test_slider_time_format(page: Page, local_app: ShinyAppProc) -> None:
187+
page.goto(local_app.url)
188+
189+
s6 = controller.InputSlider(page, "s6")
190+
s6.expect_label("Time format")
191+
s6.expect_timezone("0000")
192+
s6.expect_value("2024-01-05 12:00:00")
193+
s6.expect_min(convert_to_utc_date_time("2024-01-01 00:00:00"))
194+
s6.expect_max(convert_to_utc_date_time("2024-01-10 23:59:00"))
195+
s6.expect_time_format("%F %T")
196+
s6.expect_width("600px")
197+
s6.expect_drag_range(None)
198+
controller.OutputTextVerbatim(page, "txt6").expect_value("2024-01-05 12:00:00")
199+
200+
new_val = "2024-01-01 00:00:00"
201+
s6.set(new_val)
202+
s6.expect_value(new_val)
203+
controller.OutputTextVerbatim(page, "txt6").expect_value("2024-01-01 00:00:00")
204+
205+
206+
def test_slider_drag_range_disabled(page: Page, local_app: ShinyAppProc) -> None:
207+
page.goto(local_app.url)
208+
209+
s7 = controller.InputSliderRange(page, "s7")
210+
s7.expect_label("Drag Range (Disabled)")
211+
s7.expect_value(("200", "500"))
212+
s7.expect_min("0")
213+
s7.expect_max("1000")
214+
s7.expect_drag_range("false")
215+
new_val = ("25", "502")
216+
s7.set(new_val, max_err_values=1000)
217+
controller.OutputTextVerbatim(page, "txt7").expect_value(
218+
f"({new_val[0]}, {new_val[1]})"
219+
)

0 commit comments

Comments
 (0)