Skip to content

Commit fa73d6f

Browse files
committed
tests: chat.update_user_input() with submit and focus
1 parent 1440d4d commit fa73d6f

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from shiny import reactive
2+
from shiny.express import input, ui
3+
4+
ui.page_opts(fillable=True)
5+
6+
with ui.layout_columns(fill=False, fillable=True):
7+
ui.input_action_button("set_input", "Just set input")
8+
ui.input_action_button("set_and_focus", "Set and focus input")
9+
ui.input_action_button("submit", "Submit to chat")
10+
ui.input_action_button("submit_and_focus", "Submit and focus chat")
11+
12+
chat = ui.Chat("chat")
13+
14+
15+
chat.ui()
16+
17+
18+
@reactive.effect
19+
@reactive.event(input.set_input)
20+
def do_set_input():
21+
chat.update_user_input(value="Input was set, but neither focused nor submitted.")
22+
23+
24+
@reactive.effect
25+
@reactive.event(input.set_and_focus)
26+
def do_set_and_focus():
27+
chat.update_user_input(
28+
value="Input was set and focused, but not submitted.",
29+
focus=True,
30+
)
31+
32+
33+
@reactive.effect
34+
@reactive.event(input.submit)
35+
def do_submit():
36+
chat.update_user_input(
37+
value="This chat was sent on behalf of the user.",
38+
submit=True,
39+
)
40+
41+
42+
@reactive.effect
43+
@reactive.event(input.submit_and_focus)
44+
def do_submit_and_focus():
45+
chat.update_user_input(
46+
value="This chat was sent on behalf of the user. Input will still be focused.",
47+
submit=True,
48+
focus=True,
49+
)
50+
51+
52+
@chat.on_user_submit
53+
async def on_user_submit(message: str):
54+
await chat.append_message(f"You said: {message}")
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from playwright.sync_api import Page, expect
2+
from utils.deploy_utils import skip_on_webkit
3+
4+
from shiny.playwright import controller
5+
from shiny.run import ShinyAppProc
6+
7+
8+
@skip_on_webkit
9+
def test_validate_chat_update_user_input(page: Page, local_app: ShinyAppProc) -> None:
10+
page.goto(local_app.url)
11+
12+
chat = controller.Chat(page, "chat")
13+
14+
# Starting state sanity check
15+
expect(chat.loc).to_be_visible(timeout=30 * 1000)
16+
chat.expect_user_input("")
17+
expect(chat.loc_input_button).to_be_disabled()
18+
19+
# Locate action buttons
20+
set_input = controller.InputActionButton(page, "set_input")
21+
set_and_focus = controller.InputActionButton(page, "set_and_focus")
22+
submit = controller.InputActionButton(page, "submit")
23+
submit_and_focus = controller.InputActionButton(page, "submit_and_focus")
24+
25+
# Just set the input ----
26+
set_input.loc.focus()
27+
set_input.click()
28+
chat.expect_user_input("Input was set, but neither focused nor submitted.")
29+
expect(chat.loc_input_button).to_be_enabled()
30+
expect(chat.loc_input).not_to_be_focused()
31+
32+
# Set the input and move focus ----
33+
set_and_focus.click()
34+
chat.expect_user_input("Input was set and focused, but not submitted.")
35+
expect(chat.loc_input_button).to_be_enabled()
36+
expect(chat.loc_input).to_be_focused()
37+
38+
# Auto submit ----
39+
submit.loc.focus()
40+
submit.click()
41+
chat.expect_user_input("")
42+
chat.expect_latest_message("You said: This chat was sent on behalf of the user.")
43+
expect(chat.loc_input_button).to_be_disabled()
44+
expect(chat.loc_input).not_to_be_focused()
45+
46+
# Auto submit and move focus ----
47+
submit_and_focus.loc.focus()
48+
submit_and_focus.click()
49+
chat.expect_user_input("")
50+
chat.expect_latest_message(
51+
"You said: This chat was sent on behalf of the user. Input will still be focused."
52+
)
53+
expect(chat.loc_input_button).to_be_disabled()
54+
expect(chat.loc_input).to_be_focused()

0 commit comments

Comments
 (0)