| 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-view-mode-select"; |
| 10 | import type { SketchViewModeSelect } from "./sketch-view-mode-select"; |
| 11 | |
| 12 | describe("SketchViewModeSelect", () => { |
| 13 | afterEach(() => { |
| 14 | fixtureCleanup(); |
| 15 | }); |
| 16 | |
| 17 | it("initializes with 'chat' as the default mode", async () => { |
| 18 | const el: SketchViewModeSelect = await fixture(html` |
| 19 | <sketch-view-mode-select></sketch-view-mode-select> |
| 20 | `); |
| 21 | |
| 22 | expect(el.activeMode).to.equal("chat"); |
| 23 | const chatButton = el.shadowRoot!.querySelector("#showConversationButton"); |
| 24 | expect(chatButton!.classList.contains("active")).to.be.true; |
| 25 | }); |
| 26 | |
| 27 | it("displays all four view mode buttons", async () => { |
| 28 | const el: SketchViewModeSelect = await fixture(html` |
| 29 | <sketch-view-mode-select></sketch-view-mode-select> |
| 30 | `); |
| 31 | |
| 32 | const buttons = el.shadowRoot!.querySelectorAll(".emoji-button"); |
| 33 | expect(buttons.length).to.equal(4); |
| 34 | |
| 35 | const chatButton = el.shadowRoot!.querySelector("#showConversationButton"); |
| 36 | const diffButton = el.shadowRoot!.querySelector("#showDiffButton"); |
| 37 | const chartsButton = el.shadowRoot!.querySelector("#showChartsButton"); |
| 38 | const terminalButton = el.shadowRoot!.querySelector("#showTerminalButton"); |
| 39 | |
| 40 | expect(chatButton).to.exist; |
| 41 | expect(diffButton).to.exist; |
| 42 | expect(chartsButton).to.exist; |
| 43 | expect(terminalButton).to.exist; |
| 44 | |
| 45 | expect(chatButton!.getAttribute("title")).to.equal("Conversation View"); |
| 46 | expect(diffButton!.getAttribute("title")).to.equal("Diff View"); |
| 47 | expect(chartsButton!.getAttribute("title")).to.equal("Charts View"); |
| 48 | expect(terminalButton!.getAttribute("title")).to.equal("Terminal View"); |
| 49 | }); |
| 50 | |
| 51 | it("dispatches view-mode-select event when clicking a mode button", async () => { |
| 52 | const el: SketchViewModeSelect = await fixture(html` |
| 53 | <sketch-view-mode-select></sketch-view-mode-select> |
| 54 | `); |
| 55 | |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 56 | const diffButton = el.shadowRoot!.querySelector( |
| 57 | "#showDiffButton", |
| 58 | ) as HTMLButtonElement; |
| 59 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 60 | // Setup listener for the view-mode-select event |
| 61 | setTimeout(() => diffButton.click()); |
| 62 | const { detail } = await oneEvent(el, "view-mode-select"); |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 63 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 64 | expect(detail.mode).to.equal("diff"); |
| 65 | }); |
| 66 | |
| 67 | it("updates the active mode when receiving update-active-mode event", async () => { |
| 68 | const el: SketchViewModeSelect = await fixture(html` |
| 69 | <sketch-view-mode-select></sketch-view-mode-select> |
| 70 | `); |
| 71 | |
| 72 | // Initially should be in chat mode |
| 73 | expect(el.activeMode).to.equal("chat"); |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 74 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 75 | // Dispatch the update-active-mode event to change to diff mode |
| 76 | const updateEvent = new CustomEvent("update-active-mode", { |
| 77 | detail: { mode: "diff" }, |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 78 | bubbles: true, |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 79 | }); |
| 80 | el.dispatchEvent(updateEvent); |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 81 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 82 | // Wait for the component to update |
| 83 | await elementUpdated(el); |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 84 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 85 | expect(el.activeMode).to.equal("diff"); |
| 86 | const diffButton = el.shadowRoot!.querySelector("#showDiffButton"); |
| 87 | expect(diffButton!.classList.contains("active")).to.be.true; |
| 88 | }); |
| 89 | |
| 90 | it("correctly marks the active button based on mode", async () => { |
| 91 | const el: SketchViewModeSelect = await fixture(html` |
| 92 | <sketch-view-mode-select activeMode="terminal"></sketch-view-mode-select> |
| 93 | `); |
| 94 | |
| 95 | // Terminal button should be active |
| 96 | const terminalButton = el.shadowRoot!.querySelector("#showTerminalButton"); |
| 97 | const chatButton = el.shadowRoot!.querySelector("#showConversationButton"); |
| 98 | const diffButton = el.shadowRoot!.querySelector("#showDiffButton"); |
| 99 | const chartsButton = el.shadowRoot!.querySelector("#showChartsButton"); |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 100 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 101 | expect(terminalButton!.classList.contains("active")).to.be.true; |
| 102 | expect(chatButton!.classList.contains("active")).to.be.false; |
| 103 | expect(diffButton!.classList.contains("active")).to.be.false; |
| 104 | expect(chartsButton!.classList.contains("active")).to.be.false; |
| 105 | }); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 106 | }); |