|
1 | 1 | import React, { ReactNode } from "react" |
2 | | -import { render, screen } from "@testing-library/react" |
3 | | -import { SelectDropdown } from "../select-dropdown" |
| 2 | +import { render, screen, fireEvent } from "@testing-library/react" |
| 3 | +import { SelectDropdown, DropdownOptionType } from "../select-dropdown" |
| 4 | + |
| 5 | +// Mock window.postMessage |
| 6 | +const postMessageMock = jest.fn() |
| 7 | +Object.defineProperty(window, "postMessage", { |
| 8 | + writable: true, |
| 9 | + value: postMessageMock, |
| 10 | +}) |
4 | 11 |
|
5 | 12 | // Mock the Radix UI DropdownMenu component and its children |
6 | 13 | jest.mock("../dropdown-menu", () => { |
@@ -107,4 +114,133 @@ describe("SelectDropdown", () => { |
107 | 114 | const trigger = screen.getByTestId("dropdown-trigger") |
108 | 115 | expect(trigger.classList.toString()).toContain("custom-trigger-class") |
109 | 116 | }) |
| 117 | + |
| 118 | + // Tests for the new functionality |
| 119 | + describe("Option types", () => { |
| 120 | + it("renders separator options correctly", () => { |
| 121 | + const optionsWithTypedSeparator = [ |
| 122 | + { value: "option1", label: "Option 1" }, |
| 123 | + { value: "sep-1", label: "Separator", type: DropdownOptionType.SEPARATOR }, |
| 124 | + { value: "option2", label: "Option 2" }, |
| 125 | + ] |
| 126 | + |
| 127 | + render(<SelectDropdown value="option1" options={optionsWithTypedSeparator} onChange={onChangeMock} />) |
| 128 | + |
| 129 | + // Check for separator |
| 130 | + const separators = screen.getAllByTestId("dropdown-separator") |
| 131 | + expect(separators.length).toBe(1) |
| 132 | + }) |
| 133 | + |
| 134 | + it("renders string separator (backward compatibility) correctly", () => { |
| 135 | + const optionsWithStringSeparator = [ |
| 136 | + { value: "option1", label: "Option 1" }, |
| 137 | + { value: "sep-1", label: "────", disabled: true }, |
| 138 | + { value: "option2", label: "Option 2" }, |
| 139 | + ] |
| 140 | + |
| 141 | + render(<SelectDropdown value="option1" options={optionsWithStringSeparator} onChange={onChangeMock} />) |
| 142 | + |
| 143 | + // Check for separator |
| 144 | + const separators = screen.getAllByTestId("dropdown-separator") |
| 145 | + expect(separators.length).toBe(1) |
| 146 | + }) |
| 147 | + |
| 148 | + it("renders shortcut options correctly", () => { |
| 149 | + const shortcutText = "Ctrl+K" |
| 150 | + const optionsWithShortcut = [ |
| 151 | + { value: "shortcut", label: shortcutText, type: DropdownOptionType.SHORTCUT }, |
| 152 | + { value: "option1", label: "Option 1" }, |
| 153 | + ] |
| 154 | + |
| 155 | + render( |
| 156 | + <SelectDropdown |
| 157 | + value="option1" |
| 158 | + options={optionsWithShortcut} |
| 159 | + onChange={onChangeMock} |
| 160 | + shortcutText={shortcutText} |
| 161 | + />, |
| 162 | + ) |
| 163 | + |
| 164 | + // The shortcut text should be rendered as a div, not a dropdown item |
| 165 | + expect(screen.queryByText(shortcutText)).toBeInTheDocument() |
| 166 | + const dropdownItems = screen.getAllByTestId("dropdown-item") |
| 167 | + expect(dropdownItems.length).toBe(1) // Only one regular option |
| 168 | + }) |
| 169 | + |
| 170 | + it("handles action options correctly", () => { |
| 171 | + const optionsWithAction = [ |
| 172 | + { value: "option1", label: "Option 1" }, |
| 173 | + { value: "settingsButtonClicked", label: "Settings", type: DropdownOptionType.ACTION }, |
| 174 | + ] |
| 175 | + |
| 176 | + render(<SelectDropdown value="option1" options={optionsWithAction} onChange={onChangeMock} />) |
| 177 | + |
| 178 | + // Get all dropdown items |
| 179 | + const dropdownItems = screen.getAllByTestId("dropdown-item") |
| 180 | + |
| 181 | + // Click the action item |
| 182 | + fireEvent.click(dropdownItems[1]) |
| 183 | + |
| 184 | + // Check that postMessage was called with the correct action |
| 185 | + expect(postMessageMock).toHaveBeenCalledWith({ |
| 186 | + type: "action", |
| 187 | + action: "settingsButtonClicked", |
| 188 | + }) |
| 189 | + |
| 190 | + // The onChange callback should not be called for action items |
| 191 | + expect(onChangeMock).not.toHaveBeenCalled() |
| 192 | + }) |
| 193 | + |
| 194 | + it("only treats options with explicit ACTION type as actions", () => { |
| 195 | + const optionsForTest = [ |
| 196 | + { value: "option1", label: "Option 1" }, |
| 197 | + // This should be treated as a regular option despite the -action suffix |
| 198 | + { value: "settings-action", label: "Regular option with action suffix" }, |
| 199 | + // This should be treated as an action |
| 200 | + { value: "settingsButtonClicked", label: "Settings", type: DropdownOptionType.ACTION }, |
| 201 | + ] |
| 202 | + |
| 203 | + render(<SelectDropdown value="option1" options={optionsForTest} onChange={onChangeMock} />) |
| 204 | + |
| 205 | + // Get all dropdown items |
| 206 | + const dropdownItems = screen.getAllByTestId("dropdown-item") |
| 207 | + |
| 208 | + // Click the second option (with action suffix but no ACTION type) |
| 209 | + fireEvent.click(dropdownItems[1]) |
| 210 | + |
| 211 | + // Should trigger onChange, not postMessage |
| 212 | + expect(onChangeMock).toHaveBeenCalledWith("settings-action") |
| 213 | + expect(postMessageMock).not.toHaveBeenCalled() |
| 214 | + |
| 215 | + // Reset mocks |
| 216 | + onChangeMock.mockReset() |
| 217 | + postMessageMock.mockReset() |
| 218 | + |
| 219 | + // Click the third option (ACTION type) |
| 220 | + fireEvent.click(dropdownItems[2]) |
| 221 | + |
| 222 | + // Should trigger postMessage with "settingsButtonClicked", not onChange |
| 223 | + expect(postMessageMock).toHaveBeenCalledWith({ |
| 224 | + type: "action", |
| 225 | + action: "settingsButtonClicked", |
| 226 | + }) |
| 227 | + expect(onChangeMock).not.toHaveBeenCalled() |
| 228 | + }) |
| 229 | + |
| 230 | + it("calls onChange for regular menu items", () => { |
| 231 | + render(<SelectDropdown value="option1" options={options} onChange={onChangeMock} />) |
| 232 | + |
| 233 | + // Get all dropdown items |
| 234 | + const dropdownItems = screen.getAllByTestId("dropdown-item") |
| 235 | + |
| 236 | + // Click the second option (index 1) |
| 237 | + fireEvent.click(dropdownItems[1]) |
| 238 | + |
| 239 | + // Check that onChange was called with the correct value |
| 240 | + expect(onChangeMock).toHaveBeenCalledWith("option2") |
| 241 | + |
| 242 | + // postMessage should not be called for regular items |
| 243 | + expect(postMessageMock).not.toHaveBeenCalled() |
| 244 | + }) |
| 245 | + }) |
110 | 246 | }) |
0 commit comments