|
| 1 | +import { fireEvent, render, screen } from "@testing-library/react"; |
| 2 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 3 | +import { SidebarProvider, useSidebar } from "./sidebar"; |
| 4 | + |
| 5 | +// jsdom implements no window.matchMedia; SidebarProvider pulls it in via useIsMobile. Stub it desktop-shaped |
| 6 | +// (matches:false), the standard shadcn use-mobile test setup. |
| 7 | +beforeEach(() => { |
| 8 | + vi.stubGlobal( |
| 9 | + "matchMedia", |
| 10 | + vi.fn(() => ({ |
| 11 | + matches: false, |
| 12 | + media: "", |
| 13 | + addEventListener: () => {}, |
| 14 | + removeEventListener: () => {}, |
| 15 | + })), |
| 16 | + ); |
| 17 | +}); |
| 18 | +afterEach(() => vi.unstubAllGlobals()); |
| 19 | + |
| 20 | +// #8305: SidebarProvider's global Cmd/Ctrl+B keydown handler used to toggle the sidebar (and preventDefault()) |
| 21 | +// no matter what had focus — hijacking the browser-native Bold shortcut inside text fields. These pin the |
| 22 | +// isTyping() guard: the shortcut still works from non-editable targets, and is inert inside a form field or |
| 23 | +// contenteditable element (target's own text-editing keeps the native behavior). |
| 24 | + |
| 25 | +function StateProbe() { |
| 26 | + const { open } = useSidebar(); |
| 27 | + return <span data-testid="sidebar-open">{open ? "open" : "closed"}</span>; |
| 28 | +} |
| 29 | + |
| 30 | +function setup() { |
| 31 | + const utils = render( |
| 32 | + <SidebarProvider> |
| 33 | + <StateProbe /> |
| 34 | + <input data-testid="field-input" /> |
| 35 | + <textarea data-testid="field-textarea" /> |
| 36 | + <div data-testid="field-editable" /> |
| 37 | + </SidebarProvider>, |
| 38 | + ); |
| 39 | + // jsdom does not derive `isContentEditable` from the contentEditable attribute, so define it explicitly to |
| 40 | + // represent a real contenteditable element the way a browser reports it to the guard. |
| 41 | + const editable = screen.getByTestId("field-editable"); |
| 42 | + Object.defineProperty(editable, "isContentEditable", { |
| 43 | + value: true, |
| 44 | + configurable: true, |
| 45 | + }); |
| 46 | + return utils; |
| 47 | +} |
| 48 | + |
| 49 | +const openState = () => screen.getByTestId("sidebar-open").textContent; |
| 50 | + |
| 51 | +describe("SidebarProvider Cmd/Ctrl+B guard (#8305)", () => { |
| 52 | + it("toggles the sidebar on Cmd/Ctrl+B from a non-editable target and prevents the default", () => { |
| 53 | + setup(); |
| 54 | + const before = openState(); |
| 55 | + // fireEvent returns false when the event's default was prevented. |
| 56 | + const notPrevented = fireEvent.keyDown(document.body, { |
| 57 | + key: "b", |
| 58 | + ctrlKey: true, |
| 59 | + }); |
| 60 | + expect(notPrevented).toBe(false); // preventDefault() ran |
| 61 | + expect(openState()).not.toBe(before); // state flipped |
| 62 | + }); |
| 63 | + |
| 64 | + it("does not toggle and does not preventDefault when the target is an input/textarea/contenteditable", () => { |
| 65 | + setup(); |
| 66 | + for (const id of ["field-input", "field-textarea", "field-editable"]) { |
| 67 | + const before = openState(); |
| 68 | + const notPrevented = fireEvent.keyDown(screen.getByTestId(id), { |
| 69 | + key: "b", |
| 70 | + metaKey: true, |
| 71 | + }); |
| 72 | + expect(notPrevented, id).toBe(true); // default NOT prevented — native Bold survives |
| 73 | + expect(openState(), id).toBe(before); // sidebar unchanged |
| 74 | + } |
| 75 | + }); |
| 76 | + |
| 77 | + it("leaves an unrelated Cmd/Ctrl chord (not 'b') alone", () => { |
| 78 | + setup(); |
| 79 | + const before = openState(); |
| 80 | + const notPrevented = fireEvent.keyDown(document.body, { |
| 81 | + key: "k", |
| 82 | + ctrlKey: true, |
| 83 | + }); |
| 84 | + expect(notPrevented).toBe(true); |
| 85 | + expect(openState()).toBe(before); |
| 86 | + }); |
| 87 | +}); |
0 commit comments