|
1 | 1 | import { mount } from "@vue/test-utils"; |
2 | | -import { describe, expect, it } from "vitest"; |
| 2 | +import { describe, expect, it, vi } from "vitest"; |
| 3 | +import { ref } from "vue"; |
| 4 | +import { DATA_KEY } from "./lib/provide-inject"; |
3 | 5 | import MenuOption from "./MenuOption.vue"; |
4 | 6 |
|
5 | 7 | describe("scrolling behavior when option is above viewport", () => { |
@@ -159,3 +161,66 @@ describe("keyboard event handling", () => { |
159 | 161 | expect(wrapper.emitted("select")).toHaveLength(1); |
160 | 162 | }); |
161 | 163 | }); |
| 164 | + |
| 165 | +describe("hover to focus behavior", () => { |
| 166 | + it("should set focused option when hovered", async () => { |
| 167 | + const mockSetFocusedOption = vi.fn(); |
| 168 | + const mockData = { |
| 169 | + setFocusedOption: mockSetFocusedOption, |
| 170 | + focusedOption: ref(-1), |
| 171 | + menuOpen: ref(true), |
| 172 | + }; |
| 173 | + |
| 174 | + const wrapper = mount(MenuOption, { |
| 175 | + props: { |
| 176 | + menu: null, |
| 177 | + index: 2, |
| 178 | + isFocused: false, |
| 179 | + isSelected: false, |
| 180 | + isDisabled: false, |
| 181 | + }, |
| 182 | + global: { |
| 183 | + provide: { |
| 184 | + [DATA_KEY as symbol]: mockData, |
| 185 | + }, |
| 186 | + }, |
| 187 | + }); |
| 188 | + |
| 189 | + // Trigger mouse enter |
| 190 | + await wrapper.trigger("mouseenter"); |
| 191 | + |
| 192 | + // Verify setFocusedOption was called with the correct index |
| 193 | + expect(mockSetFocusedOption).toHaveBeenCalledWith(2); |
| 194 | + expect(mockSetFocusedOption).toHaveBeenCalledTimes(1); |
| 195 | + }); |
| 196 | + |
| 197 | + it("should not set focused option when hovered on disabled option", async () => { |
| 198 | + const mockSetFocusedOption = vi.fn(); |
| 199 | + const mockData = { |
| 200 | + setFocusedOption: mockSetFocusedOption, |
| 201 | + focusedOption: ref(-1), |
| 202 | + menuOpen: ref(true), |
| 203 | + }; |
| 204 | + |
| 205 | + const wrapper = mount(MenuOption, { |
| 206 | + props: { |
| 207 | + menu: null, |
| 208 | + index: 2, |
| 209 | + isFocused: false, |
| 210 | + isSelected: false, |
| 211 | + isDisabled: true, // Option is disabled |
| 212 | + }, |
| 213 | + global: { |
| 214 | + provide: { |
| 215 | + [DATA_KEY as symbol]: mockData, |
| 216 | + }, |
| 217 | + }, |
| 218 | + }); |
| 219 | + |
| 220 | + // Trigger mouse enter |
| 221 | + await wrapper.trigger("mouseenter"); |
| 222 | + |
| 223 | + // Verify setFocusedOption was NOT called |
| 224 | + expect(mockSetFocusedOption).not.toHaveBeenCalled(); |
| 225 | + }); |
| 226 | +}); |
0 commit comments