| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 1 | import { |
| 2 | html, |
| 3 | fixture, |
| 4 | expect, |
| 5 | oneEvent, |
| 6 | elementUpdated, |
| 7 | fixtureCleanup, |
| 8 | } from "@open-wc/testing"; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 9 | import "./sketch-chat-input"; |
| 10 | import { SketchChatInput } from "./sketch-chat-input"; |
| 11 | |
| 12 | describe("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 McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 23 | const textarea = el.shadowRoot!.querySelector( |
| 24 | "#chatInput", |
| 25 | ) as HTMLTextAreaElement; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 26 | 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 McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 36 | const textarea = el.shadowRoot!.querySelector( |
| 37 | "#chatInput", |
| 38 | ) as HTMLTextAreaElement; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 39 | 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 McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 47 | const textarea = el.shadowRoot!.querySelector( |
| 48 | "#chatInput", |
| 49 | ) as HTMLTextAreaElement; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 50 | const newValue = "New message"; |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 51 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 52 | textarea.value = newValue; |
| 53 | textarea.dispatchEvent(new Event("input")); |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 54 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 55 | 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 McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 64 | const button = el.shadowRoot!.querySelector( |
| 65 | "#sendChatButton", |
| 66 | ) as HTMLButtonElement; |
| 67 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 68 | // Setup listener for the send-chat event |
| 69 | setTimeout(() => button.click()); |
| 70 | const { detail } = await oneEvent(el, "send-chat"); |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 71 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 72 | 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 McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 82 | const textarea = el.shadowRoot!.querySelector( |
| 83 | "#chatInput", |
| 84 | ) as HTMLTextAreaElement; |
| 85 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 86 | // 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 McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 92 | shiftKey: false, |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 93 | }); |
| 94 | textarea.dispatchEvent(enterEvent); |
| 95 | }); |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 96 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 97 | const { detail } = await oneEvent(el, "send-chat"); |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 98 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 99 | 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 McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 109 | const textarea = el.shadowRoot!.querySelector( |
| 110 | "#chatInput", |
| 111 | ) as HTMLTextAreaElement; |
| 112 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 113 | // Create a flag to track if the event was fired |
| 114 | let eventFired = false; |
| 115 | el.addEventListener("send-chat", () => { |
| 116 | eventFired = true; |
| 117 | }); |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 118 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 119 | // Dispatch the shift+enter keydown event |
| 120 | const shiftEnterEvent = new KeyboardEvent("keydown", { |
| 121 | key: "Enter", |
| 122 | bubbles: true, |
| 123 | cancelable: true, |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 124 | shiftKey: true, |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 125 | }); |
| 126 | textarea.dispatchEvent(shiftEnterEvent); |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 127 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 128 | // Wait a short time to verify no event was fired |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 129 | await new Promise((resolve) => setTimeout(resolve, 10)); |
| 130 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 131 | 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 McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 141 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 142 | // Dispatch the update-content event |
| 143 | const updateEvent = new CustomEvent("update-content", { |
| 144 | detail: { content: newContent }, |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 145 | bubbles: true, |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 146 | }); |
| 147 | el.dispatchEvent(updateEvent); |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 148 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 149 | // Wait for the component to update |
| 150 | await elementUpdated(el); |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 151 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 152 | expect(el.content).to.equal(newContent); |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 153 | const textarea = el.shadowRoot!.querySelector( |
| 154 | "#chatInput", |
| 155 | ) as HTMLTextAreaElement; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 156 | expect(textarea.value).to.equal(newContent); |
| 157 | }); |
| 158 | }); |