| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 1 | import { test, expect } from "@sand4rt/experimental-ct-web"; |
| 2 | import { SketchViewModeSelect } from "./sketch-view-mode-select"; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 3 | |
| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 4 | test("initializes with 'chat' as the default mode", async ({ mount }) => { |
| 5 | const component = await mount(SketchViewModeSelect, {}); |
| 6 | |
| 7 | // Check the activeMode property |
| 8 | const activeMode = await component.evaluate( |
| 9 | (el: SketchViewModeSelect) => el.activeMode, |
| 10 | ); |
| 11 | expect(activeMode).toBe("chat"); |
| 12 | |
| 13 | // Check that the chat button has the active class |
| 14 | await expect( |
| 15 | component.locator("#showConversationButton.active"), |
| 16 | ).toBeVisible(); |
| 17 | }); |
| 18 | |
| 19 | test("displays all four view mode buttons", async ({ mount }) => { |
| 20 | const component = await mount(SketchViewModeSelect, {}); |
| 21 | |
| 22 | // Count the number of buttons |
| 23 | const buttonCount = await component.locator(".emoji-button").count(); |
| 24 | expect(buttonCount).toBe(4); |
| 25 | |
| 26 | // Check that each button exists |
| 27 | await expect(component.locator("#showConversationButton")).toBeVisible(); |
| 28 | await expect(component.locator("#showDiffButton")).toBeVisible(); |
| 29 | await expect(component.locator("#showChartsButton")).toBeVisible(); |
| 30 | await expect(component.locator("#showTerminalButton")).toBeVisible(); |
| 31 | |
| 32 | // Check the title attributes |
| 33 | expect( |
| 34 | await component.locator("#showConversationButton").getAttribute("title"), |
| 35 | ).toBe("Conversation View"); |
| 36 | expect(await component.locator("#showDiffButton").getAttribute("title")).toBe( |
| 37 | "Diff View", |
| 38 | ); |
| 39 | expect( |
| 40 | await component.locator("#showChartsButton").getAttribute("title"), |
| 41 | ).toBe("Charts View"); |
| 42 | expect( |
| 43 | await component.locator("#showTerminalButton").getAttribute("title"), |
| 44 | ).toBe("Terminal View"); |
| 45 | }); |
| 46 | |
| 47 | test("dispatches view-mode-select event when clicking a mode button", async ({ |
| 48 | mount, |
| 49 | }) => { |
| 50 | const component = await mount(SketchViewModeSelect, {}); |
| 51 | |
| 52 | // Set up promise to wait for the event |
| 53 | const eventPromise = component.evaluate((el) => { |
| 54 | return new Promise((resolve) => { |
| 55 | el.addEventListener( |
| 56 | "view-mode-select", |
| 57 | (event) => { |
| 58 | resolve((event as CustomEvent).detail); |
| 59 | }, |
| 60 | { once: true }, |
| 61 | ); |
| 62 | }); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 63 | }); |
| 64 | |
| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 65 | // Click the diff button |
| 66 | await component.locator("#showDiffButton").click(); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 67 | |
| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 68 | // Wait for the event and check its details |
| 69 | const detail: any = await eventPromise; |
| 70 | expect(detail.mode).toBe("diff"); |
| 71 | }); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 72 | |
| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 73 | test("updates the active mode when receiving update-active-mode event", async ({ |
| 74 | mount, |
| 75 | }) => { |
| 76 | const component = await mount(SketchViewModeSelect, {}); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 77 | |
| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 78 | // Initially should be in chat mode |
| 79 | let activeMode = await component.evaluate( |
| 80 | (el: SketchViewModeSelect) => el.activeMode, |
| 81 | ); |
| 82 | expect(activeMode).toBe("chat"); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 83 | |
| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 84 | // Dispatch the update-active-mode event |
| 85 | await component.evaluate((el) => { |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 86 | const updateEvent = new CustomEvent("update-active-mode", { |
| 87 | detail: { mode: "diff" }, |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 88 | bubbles: true, |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 89 | }); |
| 90 | el.dispatchEvent(updateEvent); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 91 | }); |
| 92 | |
| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 93 | // Check that the mode was updated |
| 94 | activeMode = await component.evaluate( |
| 95 | (el: SketchViewModeSelect) => el.activeMode, |
| 96 | ); |
| 97 | expect(activeMode).toBe("diff"); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 98 | |
| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 99 | // Check that the diff button is now active |
| 100 | await expect(component.locator("#showDiffButton.active")).toBeVisible(); |
| 101 | }); |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 102 | |
| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 103 | test("correctly marks the active button based on mode", async ({ mount }) => { |
| 104 | const component = await mount(SketchViewModeSelect, { |
| 105 | props: { |
| 106 | activeMode: "terminal", |
| 107 | }, |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 108 | }); |
| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 109 | |
| 110 | // Terminal button should be active |
| 111 | await expect(component.locator("#showTerminalButton.active")).toBeVisible(); |
| 112 | |
| 113 | // Other buttons should not be active |
| 114 | await expect( |
| 115 | component.locator("#showConversationButton.active"), |
| 116 | ).not.toBeVisible(); |
| 117 | await expect(component.locator("#showDiffButton.active")).not.toBeVisible(); |
| 118 | await expect(component.locator("#showChartsButton.active")).not.toBeVisible(); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 119 | }); |