Skip to content

Commit cf19684

Browse files
committed
tests: Reflect input value back in output text
1 parent 754495a commit cf19684

File tree

2 files changed

+54
-38
lines changed

2 files changed

+54
-38
lines changed

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

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,20 @@ def random_value(
6969
def text_input_ui(update_on: Literal["change", "blur"] = "change"):
7070
return ui.TagList(
7171
ui.h2(f'updateOn="{update_on}"'),
72-
ui.input_text("txt", "Text", value="Hello", update_on=update_on),
73-
ui.input_text_area("txtarea", "Text Area", update_on=update_on),
74-
ui.input_numeric("num", "Numeric", value=1, update_on=update_on),
75-
ui.input_password("pwd", "Password", update_on=update_on),
76-
ui.output_text_verbatim("value"),
72+
ui.layout_columns(
73+
ui.input_text("txt", "Text", value="Hello", update_on=update_on),
74+
ui.div("Text", ui.output_text_verbatim("value_txt")),
75+
76+
ui.input_text_area("txtarea", "Text Area", update_on=update_on),
77+
ui.div("Text Area", ui.output_text_verbatim("value_txtarea")),
78+
79+
ui.input_numeric("num", "Numeric", value=1, update_on=update_on),
80+
ui.div("Numeric", ui.output_text_verbatim("value_num")),
81+
82+
ui.input_password("pwd", "Password", update_on=update_on),
83+
ui.div("Password", ui.output_text_verbatim("value_pwd")),
84+
col_widths=6,
85+
),
7786
ui.input_action_button("update_text", "Update Text"),
7887
ui.input_action_button("update_text_area", "Update Text Area"),
7988
ui.input_action_button("update_number", "Update Number"),
@@ -84,19 +93,20 @@ def text_input_ui(update_on: Literal["change", "blur"] = "change"):
8493
@module.server
8594
def text_input_server(input: Inputs, output: Outputs, session: Session):
8695
@render.text
87-
def value() -> str:
88-
return "\n".join(
89-
[
90-
"---- Text ----",
91-
input.txt(),
92-
"---- Text Area ----",
93-
input.txtarea(),
94-
"---- Numeric ----",
95-
str(input.num()),
96-
"---- Password ----",
97-
input.pwd(),
98-
]
99-
)
96+
def value_txt() -> str:
97+
return input.txt()
98+
99+
@render.text
100+
def value_txtarea() -> str:
101+
return input.txtarea()
102+
103+
@render.text
104+
def value_num() -> str:
105+
return str(input.num())
106+
107+
@render.text
108+
def value_pwd() -> str:
109+
return input.pwd()
100110

101111
@reactive.effect
102112
@reactive.event(input.update_text)

tests/playwright/shiny/inputs/input_text_update_on/test_input_text_update_on.py

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,73 +8,79 @@
88
from shiny.run import ShinyAppProc
99

1010

11+
def click_action_button(page: Page, x: controller.InputActionButton):
12+
"""Click the button without moving focus (changes input, doesn't change value)"""
13+
page.evaluate(
14+
"([id]) => document.getElementById(id).click()", [x.id]
15+
)
16+
1117
def test_text_input_change(page: Page, local_app: ShinyAppProc):
1218
page.goto(local_app.url)
1319

1420
input = controller.InputText(page, "change-txt")
21+
output = controller.OutputTextVerbatim(page, "change-value_txt")
1522
update = controller.InputActionButton(page, "change-update_text")
1623

1724
# Test textInput() -- updateOn='change'
1825
the_value = "Hello"
1926
input.set(the_value)
20-
input.expect_value(the_value)
27+
output.expect_value(the_value)
2128

2229
input.loc.press("End")
2330
input.loc.type(", world")
2431

2532
time.sleep(0.5)
2633
the_value = "Hello, world"
27-
input.expect_value(the_value)
34+
output.expect_value(the_value)
2835
expect(input.loc).to_be_focused()
2936

30-
update.click()
37+
click_action_button(page, update)
3138
the_value = "serendipity ephemeral"
32-
input.expect_value(the_value)
39+
output.expect_value(the_value)
3340

3441

3542
def test_text_input_blur(page: Page, local_app: ShinyAppProc):
3643
page.goto(local_app.url)
3744

3845
# Test textInput() -- updateOn='blur'
3946
input = controller.InputText(page, "blur-txt")
47+
output = controller.OutputTextVerbatim(page, "blur-value_txt")
4048
update = controller.InputActionButton(page, "blur-update_text")
4149

4250
the_value = "Hello"
4351
input.set(the_value)
44-
input.expect_value(the_value)
52+
output.expect_value(the_value)
4553

4654
input.loc.focus()
4755
input.loc.press("End")
4856
input.loc.type(", world")
4957

50-
input.expect_value(the_value) # has not changed yet!
58+
output.expect_value(the_value) # has not changed yet!
5159

5260
the_value = "Hello, world"
5361
input.loc.blur()
54-
input.expect_value(the_value)
62+
output.expect_value(the_value) # changes on blur
5563

5664
input.loc.focus()
5765
input.loc.press("End")
5866
input.loc.type("!")
59-
input.expect_value(the_value) # still hasn't changed yet
60-
input.loc.press("Enter")
67+
output.expect_value(the_value) # still hasn't changed yet
6168

6269
the_value = "Hello, world!"
63-
input.expect_value(the_value)
70+
input.loc.press("Enter")
71+
output.expect_value(the_value) # changes after Enter
6472

65-
page.evaluate(
66-
"element => element.click()", update.loc
67-
) # hopefully doesn't move focus
68-
input.expect_value(the_value)
73+
click_action_button(page, update)
74+
input.expect_value("serendipity ephemeral")
75+
output.expect_value(the_value)
6976

70-
page.evaluate(
71-
"element => element.click()", update.loc
72-
) # hopefully doesn't move focus
73-
input.expect_value(the_value)
77+
click_action_button(page, update)
78+
input.expect_value("ephemeral mellifluous")
79+
output.expect_value(the_value)
7480

75-
input.loc.press("Enter") # now it changes
7681
the_value = "ephemeral mellifluous"
77-
input.expect_value(the_value)
82+
input.loc.press("Enter") # changes again after Enter
83+
output.expect_value(the_value)
7884

7985

8086
# Add similar tests for textAreaInput(), numericInput(), and passwordInput()

0 commit comments

Comments
 (0)