blob: 2c5dde331a4eb0cb3c8d8dadbc146da836e1b283 [file] [log] [blame]
Sean McCullough71941bd2025-04-18 13:31:48 -07001import {
2 html,
3 fixture,
4 expect,
5 oneEvent,
6 elementUpdated,
7 fixtureCleanup,
8} from "@open-wc/testing";
Sean McCullough86b56862025-04-18 13:04:03 -07009import "./sketch-chat-input";
10import { SketchChatInput } from "./sketch-chat-input";
11
12describe("SketchChatInput", () => {
13 afterEach(() => {
14 fixtureCleanup();
15 });
16
17 it("initializes with empty content by default", async () => {
18 const el: SketchChatInput = await fixture(html`
19 <sketch-chat-input></sketch-chat-input>
20 `);
21
22 expect(el.content).to.equal("");
Sean McCullough71941bd2025-04-18 13:31:48 -070023 const textarea = el.shadowRoot!.querySelector(
24 "#chatInput",
25 ) as HTMLTextAreaElement;
Sean McCullough86b56862025-04-18 13:04:03 -070026 expect(textarea.value).to.equal("");
27 });
28
29 it("initializes with provided content", async () => {
30 const testContent = "Hello, world!";
31 const el: SketchChatInput = await fixture(html`
32 <sketch-chat-input .content=${testContent}></sketch-chat-input>
33 `);
34
35 expect(el.content).to.equal(testContent);
Sean McCullough71941bd2025-04-18 13:31:48 -070036 const textarea = el.shadowRoot!.querySelector(
37 "#chatInput",
38 ) as HTMLTextAreaElement;
Sean McCullough86b56862025-04-18 13:04:03 -070039 expect(textarea.value).to.equal(testContent);
40 });
41
42 it("updates content when typing in the textarea", async () => {
43 const el: SketchChatInput = await fixture(html`
44 <sketch-chat-input></sketch-chat-input>
45 `);
46
Sean McCullough71941bd2025-04-18 13:31:48 -070047 const textarea = el.shadowRoot!.querySelector(
48 "#chatInput",
49 ) as HTMLTextAreaElement;
Sean McCullough86b56862025-04-18 13:04:03 -070050 const newValue = "New message";
Sean McCullough71941bd2025-04-18 13:31:48 -070051
Sean McCullough86b56862025-04-18 13:04:03 -070052 textarea.value = newValue;
53 textarea.dispatchEvent(new Event("input"));
Sean McCullough71941bd2025-04-18 13:31:48 -070054
Sean McCullough86b56862025-04-18 13:04:03 -070055 expect(el.content).to.equal(newValue);
56 });
57
58 it("sends message when clicking the send button", async () => {
59 const testContent = "Test message";
60 const el: SketchChatInput = await fixture(html`
61 <sketch-chat-input .content=${testContent}></sketch-chat-input>
62 `);
63
Sean McCullough71941bd2025-04-18 13:31:48 -070064 const button = el.shadowRoot!.querySelector(
65 "#sendChatButton",
66 ) as HTMLButtonElement;
67
Sean McCullough86b56862025-04-18 13:04:03 -070068 // Setup listener for the send-chat event
69 setTimeout(() => button.click());
70 const { detail } = await oneEvent(el, "send-chat");
Sean McCullough71941bd2025-04-18 13:31:48 -070071
Sean McCullough86b56862025-04-18 13:04:03 -070072 expect(detail.message).to.equal(testContent);
73 expect(el.content).to.equal("");
74 });
75
76 it("sends message when pressing Enter (without shift)", async () => {
77 const testContent = "Test message";
78 const el: SketchChatInput = await fixture(html`
79 <sketch-chat-input .content=${testContent}></sketch-chat-input>
80 `);
81
Sean McCullough71941bd2025-04-18 13:31:48 -070082 const textarea = el.shadowRoot!.querySelector(
83 "#chatInput",
84 ) as HTMLTextAreaElement;
85
Sean McCullough86b56862025-04-18 13:04:03 -070086 // Setup listener for the send-chat event
87 setTimeout(() => {
88 const enterEvent = new KeyboardEvent("keydown", {
89 key: "Enter",
90 bubbles: true,
91 cancelable: true,
Sean McCullough71941bd2025-04-18 13:31:48 -070092 shiftKey: false,
Sean McCullough86b56862025-04-18 13:04:03 -070093 });
94 textarea.dispatchEvent(enterEvent);
95 });
Sean McCullough71941bd2025-04-18 13:31:48 -070096
Sean McCullough86b56862025-04-18 13:04:03 -070097 const { detail } = await oneEvent(el, "send-chat");
Sean McCullough71941bd2025-04-18 13:31:48 -070098
Sean McCullough86b56862025-04-18 13:04:03 -070099 expect(detail.message).to.equal(testContent);
100 expect(el.content).to.equal("");
101 });
102
103 it("does not send message when pressing Shift+Enter", async () => {
104 const testContent = "Test message";
105 const el: SketchChatInput = await fixture(html`
106 <sketch-chat-input .content=${testContent}></sketch-chat-input>
107 `);
108
Sean McCullough71941bd2025-04-18 13:31:48 -0700109 const textarea = el.shadowRoot!.querySelector(
110 "#chatInput",
111 ) as HTMLTextAreaElement;
112
Sean McCullough86b56862025-04-18 13:04:03 -0700113 // Create a flag to track if the event was fired
114 let eventFired = false;
115 el.addEventListener("send-chat", () => {
116 eventFired = true;
117 });
Sean McCullough71941bd2025-04-18 13:31:48 -0700118
Sean McCullough86b56862025-04-18 13:04:03 -0700119 // Dispatch the shift+enter keydown event
120 const shiftEnterEvent = new KeyboardEvent("keydown", {
121 key: "Enter",
122 bubbles: true,
123 cancelable: true,
Sean McCullough71941bd2025-04-18 13:31:48 -0700124 shiftKey: true,
Sean McCullough86b56862025-04-18 13:04:03 -0700125 });
126 textarea.dispatchEvent(shiftEnterEvent);
Sean McCullough71941bd2025-04-18 13:31:48 -0700127
Sean McCullough86b56862025-04-18 13:04:03 -0700128 // Wait a short time to verify no event was fired
Sean McCullough71941bd2025-04-18 13:31:48 -0700129 await new Promise((resolve) => setTimeout(resolve, 10));
130
Sean McCullough86b56862025-04-18 13:04:03 -0700131 expect(eventFired).to.be.false;
132 expect(el.content).to.equal(testContent);
133 });
134
135 it("updates content when receiving update-content event", async () => {
136 const el: SketchChatInput = await fixture(html`
137 <sketch-chat-input></sketch-chat-input>
138 `);
139
140 const newContent = "Updated content";
Sean McCullough71941bd2025-04-18 13:31:48 -0700141
Sean McCullough86b56862025-04-18 13:04:03 -0700142 // Dispatch the update-content event
143 const updateEvent = new CustomEvent("update-content", {
144 detail: { content: newContent },
Sean McCullough71941bd2025-04-18 13:31:48 -0700145 bubbles: true,
Sean McCullough86b56862025-04-18 13:04:03 -0700146 });
147 el.dispatchEvent(updateEvent);
Sean McCullough71941bd2025-04-18 13:31:48 -0700148
Sean McCullough86b56862025-04-18 13:04:03 -0700149 // Wait for the component to update
150 await elementUpdated(el);
Sean McCullough71941bd2025-04-18 13:31:48 -0700151
Sean McCullough86b56862025-04-18 13:04:03 -0700152 expect(el.content).to.equal(newContent);
Sean McCullough71941bd2025-04-18 13:31:48 -0700153 const textarea = el.shadowRoot!.querySelector(
154 "#chatInput",
155 ) as HTMLTextAreaElement;
Sean McCullough86b56862025-04-18 13:04:03 -0700156 expect(textarea.value).to.equal(newContent);
157 });
158});