| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 1 | import { test, expect } from "@sand4rt/experimental-ct-web"; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 2 | import { SketchChatInput } from "./sketch-chat-input"; |
| 3 | |
| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 4 | test("initializes with empty content by default", async ({ mount }) => { |
| 5 | const component = await mount(SketchChatInput, {}); |
| 6 | |
| 7 | // Check public property via component's evaluate method |
| 8 | const content = await component.evaluate((el: SketchChatInput) => el.content); |
| 9 | expect(content).toBe(""); |
| 10 | |
| 11 | // Check textarea value |
| 12 | await expect(component.locator("#chatInput")).toHaveValue(""); |
| 13 | }); |
| 14 | |
| 15 | test("initializes with provided content", async ({ mount }) => { |
| 16 | const testContent = "Hello, world!"; |
| 17 | const component = await mount(SketchChatInput, { |
| 18 | props: { |
| 19 | content: testContent, |
| 20 | }, |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 21 | }); |
| 22 | |
| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 23 | // Check public property via component's evaluate method |
| 24 | const content = await component.evaluate((el: SketchChatInput) => el.content); |
| 25 | expect(content).toBe(testContent); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 26 | |
| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 27 | // Check textarea value |
| 28 | await expect(component.locator("#chatInput")).toHaveValue(testContent); |
| 29 | }); |
| 30 | |
| 31 | test("updates content when typing in the textarea", async ({ mount }) => { |
| 32 | const component = await mount(SketchChatInput, {}); |
| 33 | const newValue = "New message"; |
| 34 | |
| 35 | // Fill the textarea with new content |
| 36 | await component.locator("#chatInput").fill(newValue); |
| 37 | |
| 38 | // Check that the content property was updated |
| 39 | const content = await component.evaluate((el: SketchChatInput) => el.content); |
| 40 | expect(content).toBe(newValue); |
| 41 | }); |
| 42 | |
| 43 | test("sends message when clicking the send button", async ({ mount }) => { |
| 44 | const testContent = "Test message"; |
| 45 | const component = await mount(SketchChatInput, { |
| 46 | props: { |
| 47 | content: testContent, |
| 48 | }, |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 49 | }); |
| 50 | |
| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 51 | // Set up promise to wait for the event |
| 52 | const eventPromise = component.evaluate((el) => { |
| 53 | return new Promise((resolve) => { |
| 54 | el.addEventListener( |
| 55 | "send-chat", |
| 56 | (event) => { |
| 57 | resolve((event as CustomEvent).detail); |
| 58 | }, |
| 59 | { once: true }, |
| 60 | ); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 61 | }); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 62 | }); |
| 63 | |
| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 64 | // Click the send button |
| 65 | await component.locator("#sendChatButton").click(); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 66 | |
| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 67 | // Wait for the event and check its details |
| 68 | const detail: any = await eventPromise; |
| 69 | expect(detail.message).toBe(testContent); |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 70 | |
| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 71 | // Check that content was cleared |
| 72 | const content = await component.evaluate((el: SketchChatInput) => el.content); |
| 73 | expect(content).toBe(""); |
| 74 | }); |
| 75 | |
| 76 | test.skip("sends message when pressing Enter (without shift)", async ({ |
| 77 | mount, |
| 78 | }) => { |
| 79 | const testContent = "Test message"; |
| 80 | const component = await mount(SketchChatInput, { |
| 81 | props: { |
| 82 | content: testContent, |
| 83 | }, |
| 84 | }); |
| 85 | |
| 86 | // Set up promise to wait for the event |
| 87 | const eventPromise = component.evaluate((el) => { |
| 88 | return new Promise((resolve) => { |
| 89 | el.addEventListener( |
| 90 | "send-chat", |
| 91 | (event) => { |
| 92 | resolve((event as CustomEvent).detail); |
| 93 | }, |
| 94 | { once: true }, |
| 95 | ); |
| 96 | }); |
| 97 | }); |
| 98 | |
| 99 | // Press Enter in the textarea |
| 100 | await component.locator("#chatInput").press("Enter"); |
| 101 | |
| 102 | // Wait for the event and check its details |
| 103 | const detail: any = await eventPromise; |
| 104 | expect(detail.message).toBe(testContent); |
| 105 | |
| 106 | // Check that content was cleared |
| 107 | const content = await component.evaluate((el: SketchChatInput) => el.content); |
| 108 | expect(content).toBe(""); |
| 109 | }); |
| 110 | |
| 111 | test.skip("does not send message when pressing Shift+Enter", async ({ |
| 112 | mount, |
| 113 | }) => { |
| 114 | const testContent = "Test message"; |
| 115 | const component = await mount(SketchChatInput, { |
| 116 | props: { |
| 117 | content: testContent, |
| 118 | }, |
| 119 | }); |
| 120 | |
| 121 | // Set up to track if event fires |
| 122 | let eventFired = false; |
| 123 | await component.evaluate((el) => { |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 124 | el.addEventListener("send-chat", () => { |
| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 125 | (window as any).__eventFired = true; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 126 | }); |
| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 127 | (window as any).__eventFired = false; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 128 | }); |
| 129 | |
| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 130 | // Press Shift+Enter in the textarea |
| 131 | await component.locator("#chatInput").press("Shift+Enter"); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 132 | |
| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 133 | // Wait a short time and check if event fired |
| 134 | await new Promise((resolve) => setTimeout(resolve, 50)); |
| 135 | eventFired = await component.evaluate(() => (window as any).__eventFired); |
| 136 | expect(eventFired).toBe(false); |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 137 | |
| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 138 | // Check that content was not cleared |
| 139 | const content = await component.evaluate((el: SketchChatInput) => el.content); |
| 140 | expect(content).toBe(testContent); |
| 141 | }); |
| 142 | |
| Sean McCullough | 5164eee | 2025-04-21 18:20:23 -0700 | [diff] [blame^] | 143 | test("resizes when user enters more text than will fit", async ({ mount }) => { |
| Sean McCullough | 07b3e39 | 2025-04-21 22:51:14 +0000 | [diff] [blame] | 144 | const testContent = "Test message\n\n\n\n\n\n\n\n\n\n\n\n\nends here."; |
| 145 | const component = await mount(SketchChatInput, { |
| 146 | props: { |
| Sean McCullough | 5164eee | 2025-04-21 18:20:23 -0700 | [diff] [blame^] | 147 | content: "", |
| Sean McCullough | 07b3e39 | 2025-04-21 22:51:14 +0000 | [diff] [blame] | 148 | }, |
| 149 | }); |
| Sean McCullough | 5164eee | 2025-04-21 18:20:23 -0700 | [diff] [blame^] | 150 | const origHeight = await component.evaluate( |
| 151 | (el: SketchChatInput) => el.chatInput.style.height, |
| 152 | ); |
| Sean McCullough | 07b3e39 | 2025-04-21 22:51:14 +0000 | [diff] [blame] | 153 | |
| 154 | // Enter very tall text in the textarea |
| 155 | await component.locator("#chatInput").fill(testContent); |
| 156 | |
| 157 | // Check that textarea resized |
| Sean McCullough | 5164eee | 2025-04-21 18:20:23 -0700 | [diff] [blame^] | 158 | const newHeight = await component.evaluate( |
| 159 | (el: SketchChatInput) => el.chatInput.style.height, |
| 160 | ); |
| 161 | expect(Number.parseInt(newHeight)).toBeGreaterThan( |
| 162 | Number.parseInt(origHeight), |
| 163 | ); |
| Sean McCullough | 07b3e39 | 2025-04-21 22:51:14 +0000 | [diff] [blame] | 164 | }); |
| 165 | |
| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 166 | test("updates content when receiving update-content event", async ({ |
| 167 | mount, |
| 168 | }) => { |
| 169 | const component = await mount(SketchChatInput, {}); |
| 170 | const newContent = "Updated content"; |
| 171 | |
| 172 | // Dispatch the update-content event |
| 173 | await component.evaluate((el, newContent) => { |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 174 | const updateEvent = new CustomEvent("update-content", { |
| 175 | detail: { content: newContent }, |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 176 | bubbles: true, |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 177 | }); |
| 178 | el.dispatchEvent(updateEvent); |
| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 179 | }, newContent); |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 180 | |
| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 181 | // Wait for the component to update and check values |
| 182 | await expect(component.locator("#chatInput")).toHaveValue(newContent); |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 183 | |
| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 184 | // Check the content property |
| 185 | const content = await component.evaluate((el: SketchChatInput) => el.content); |
| 186 | expect(content).toBe(newContent); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 187 | }); |