| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 1 | import { html, fixture, expect, oneEvent, elementUpdated, fixtureCleanup } from "@open-wc/testing"; |
| 2 | import "./sketch-chat-input"; |
| 3 | import { SketchChatInput } from "./sketch-chat-input"; |
| 4 | |
| 5 | describe("SketchChatInput", () => { |
| 6 | afterEach(() => { |
| 7 | fixtureCleanup(); |
| 8 | }); |
| 9 | |
| 10 | it("initializes with empty content by default", async () => { |
| 11 | const el: SketchChatInput = await fixture(html` |
| 12 | <sketch-chat-input></sketch-chat-input> |
| 13 | `); |
| 14 | |
| 15 | expect(el.content).to.equal(""); |
| 16 | const textarea = el.shadowRoot!.querySelector("#chatInput") as HTMLTextAreaElement; |
| 17 | expect(textarea.value).to.equal(""); |
| 18 | }); |
| 19 | |
| 20 | it("initializes with provided content", async () => { |
| 21 | const testContent = "Hello, world!"; |
| 22 | const el: SketchChatInput = await fixture(html` |
| 23 | <sketch-chat-input .content=${testContent}></sketch-chat-input> |
| 24 | `); |
| 25 | |
| 26 | expect(el.content).to.equal(testContent); |
| 27 | const textarea = el.shadowRoot!.querySelector("#chatInput") as HTMLTextAreaElement; |
| 28 | expect(textarea.value).to.equal(testContent); |
| 29 | }); |
| 30 | |
| 31 | it("updates content when typing in the textarea", async () => { |
| 32 | const el: SketchChatInput = await fixture(html` |
| 33 | <sketch-chat-input></sketch-chat-input> |
| 34 | `); |
| 35 | |
| 36 | const textarea = el.shadowRoot!.querySelector("#chatInput") as HTMLTextAreaElement; |
| 37 | const newValue = "New message"; |
| 38 | |
| 39 | textarea.value = newValue; |
| 40 | textarea.dispatchEvent(new Event("input")); |
| 41 | |
| 42 | expect(el.content).to.equal(newValue); |
| 43 | }); |
| 44 | |
| 45 | it("sends message when clicking the send button", async () => { |
| 46 | const testContent = "Test message"; |
| 47 | const el: SketchChatInput = await fixture(html` |
| 48 | <sketch-chat-input .content=${testContent}></sketch-chat-input> |
| 49 | `); |
| 50 | |
| 51 | const button = el.shadowRoot!.querySelector("#sendChatButton") as HTMLButtonElement; |
| 52 | |
| 53 | // Setup listener for the send-chat event |
| 54 | setTimeout(() => button.click()); |
| 55 | const { detail } = await oneEvent(el, "send-chat"); |
| 56 | |
| 57 | expect(detail.message).to.equal(testContent); |
| 58 | expect(el.content).to.equal(""); |
| 59 | }); |
| 60 | |
| 61 | it("sends message when pressing Enter (without shift)", async () => { |
| 62 | const testContent = "Test message"; |
| 63 | const el: SketchChatInput = await fixture(html` |
| 64 | <sketch-chat-input .content=${testContent}></sketch-chat-input> |
| 65 | `); |
| 66 | |
| 67 | const textarea = el.shadowRoot!.querySelector("#chatInput") as HTMLTextAreaElement; |
| 68 | |
| 69 | // Setup listener for the send-chat event |
| 70 | setTimeout(() => { |
| 71 | const enterEvent = new KeyboardEvent("keydown", { |
| 72 | key: "Enter", |
| 73 | bubbles: true, |
| 74 | cancelable: true, |
| 75 | shiftKey: false |
| 76 | }); |
| 77 | textarea.dispatchEvent(enterEvent); |
| 78 | }); |
| 79 | |
| 80 | const { detail } = await oneEvent(el, "send-chat"); |
| 81 | |
| 82 | expect(detail.message).to.equal(testContent); |
| 83 | expect(el.content).to.equal(""); |
| 84 | }); |
| 85 | |
| 86 | it("does not send message when pressing Shift+Enter", async () => { |
| 87 | const testContent = "Test message"; |
| 88 | const el: SketchChatInput = await fixture(html` |
| 89 | <sketch-chat-input .content=${testContent}></sketch-chat-input> |
| 90 | `); |
| 91 | |
| 92 | const textarea = el.shadowRoot!.querySelector("#chatInput") as HTMLTextAreaElement; |
| 93 | |
| 94 | // Create a flag to track if the event was fired |
| 95 | let eventFired = false; |
| 96 | el.addEventListener("send-chat", () => { |
| 97 | eventFired = true; |
| 98 | }); |
| 99 | |
| 100 | // Dispatch the shift+enter keydown event |
| 101 | const shiftEnterEvent = new KeyboardEvent("keydown", { |
| 102 | key: "Enter", |
| 103 | bubbles: true, |
| 104 | cancelable: true, |
| 105 | shiftKey: true |
| 106 | }); |
| 107 | textarea.dispatchEvent(shiftEnterEvent); |
| 108 | |
| 109 | // Wait a short time to verify no event was fired |
| 110 | await new Promise(resolve => setTimeout(resolve, 10)); |
| 111 | |
| 112 | expect(eventFired).to.be.false; |
| 113 | expect(el.content).to.equal(testContent); |
| 114 | }); |
| 115 | |
| 116 | it("updates content when receiving update-content event", async () => { |
| 117 | const el: SketchChatInput = await fixture(html` |
| 118 | <sketch-chat-input></sketch-chat-input> |
| 119 | `); |
| 120 | |
| 121 | const newContent = "Updated content"; |
| 122 | |
| 123 | // Dispatch the update-content event |
| 124 | const updateEvent = new CustomEvent("update-content", { |
| 125 | detail: { content: newContent }, |
| 126 | bubbles: true |
| 127 | }); |
| 128 | el.dispatchEvent(updateEvent); |
| 129 | |
| 130 | // Wait for the component to update |
| 131 | await elementUpdated(el); |
| 132 | |
| 133 | expect(el.content).to.equal(newContent); |
| 134 | const textarea = el.shadowRoot!.querySelector("#chatInput") as HTMLTextAreaElement; |
| 135 | expect(textarea.value).to.equal(newContent); |
| 136 | }); |
| 137 | }); |