Skip to content

Commit b2d5d43

Browse files
committed
tests: Add tests for dynamic chat icon
1 parent 12d6de3 commit b2d5d43

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

tests/playwright/shiny/components/chat/icon/app.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import faicons
44

5-
from shiny.express import ui
5+
from shiny.express import input, ui
66

77
ui.page_opts(title="Chat Icons")
88

@@ -27,25 +27,30 @@ async def handle_user_input_default(user_input: str):
2727
await asyncio.sleep(1)
2828
await chat_default.append_message(f"You said: {user_input}")
2929

30-
# Otter Bot -----------------------------------------------------------------------
31-
chat_otter = ui.Chat(
32-
id="chat_otter",
30+
# Animal Bot ----------------------------------------------------------------------
31+
chat_animal = ui.Chat(
32+
id="chat_animal",
3333
messages=[
3434
{
35-
"content": "Hello! I'm Otter Bot. How can I help you today?",
35+
"content": "Hello! I'm Animal Bot. How can I help you today?",
3636
"role": "assistant",
3737
},
3838
],
3939
)
4040

4141
with ui.div():
42-
ui.h2("Otter Bot")
43-
chat_otter.ui(icon_assistant=faicons.icon_svg("otter").add_class("icon-otter"))
42+
ui.h2("Animal Bot")
43+
chat_animal.ui(icon_assistant=faicons.icon_svg("otter").add_class("icon-otter"))
44+
ui.input_select("animal", "Animal", choices=["Otter", "Hippo", "Frog", "Dove"])
4445

45-
@chat_otter.on_user_submit
46+
@chat_animal.on_user_submit
4647
async def handle_user_input_otter(user_input: str):
4748
await asyncio.sleep(1)
48-
await chat_otter.append_message(f"You said: {user_input}")
49+
icon_name = input.animal().lower()
50+
await chat_animal.append_message(
51+
f"{input.animal()} said: {user_input}",
52+
icon = faicons.icon_svg(icon_name).add_class(f"icon-{icon_name}")
53+
)
4954

5055
# SVG Bot -------------------------------------------------------------------------
5156
bs_icon_info_circle_fill = """

tests/playwright/shiny/components/chat/icon/test_chat_icon.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def __init__(self, page: Page, id: str, classes: str):
1111
self.chat = controller.Chat(page, f"chat_{id}")
1212
self.classes = classes
1313

14-
def expect_last_message_icon_to_have_classes(self):
14+
def expect_last_message_icon_to_have_classes(self, classes: str | None = None):
1515
# TODO: Fix this test to find icon in shadow root
1616
if self.id == "default":
1717
last_msg_icon = self.chat.loc_latest_message.locator(
@@ -22,7 +22,7 @@ def expect_last_message_icon_to_have_classes(self):
2222
'[slot="icon"]'
2323
)
2424

25-
expect(last_msg_icon).to_have_class(self.classes)
25+
expect(last_msg_icon).to_have_class(classes or self.classes)
2626

2727

2828
@skip_on_webkit
@@ -31,7 +31,7 @@ def test_validate_chat_basic(page: Page, local_app: ShinyAppProc) -> None:
3131

3232
chats: list[ChatModule] = [
3333
ChatModule(page, "default", "bi bi-robot"),
34-
ChatModule(page, "otter", "fa icon-otter"),
34+
ChatModule(page, "animal", "fa icon-otter"),
3535
ChatModule(page, "svg", "bi bi-info-circle-fill icon-svg"),
3636
ChatModule(page, "shiny", "icon-shiny"),
3737
]
@@ -44,3 +44,16 @@ def test_validate_chat_basic(page: Page, local_app: ShinyAppProc) -> None:
4444
mod.chat.send_user_input()
4545

4646
mod.expect_last_message_icon_to_have_classes()
47+
48+
animal = controller.InputSelect(page, "animal")
49+
chat_animal = chats[1]
50+
51+
animal.set("Hippo")
52+
chat_animal.chat.set_user_input("hello")
53+
chat_animal.chat.send_user_input()
54+
chat_animal.expect_last_message_icon_to_have_classes("fa icon-hippo")
55+
56+
animal.set("Frog")
57+
chat_animal.chat.set_user_input("hello")
58+
chat_animal.chat.send_user_input()
59+
chat_animal.expect_last_message_icon_to_have_classes("fa icon-frog")

0 commit comments

Comments
 (0)