|
| 1 | +import React from "react"; |
| 2 | +import "@testing-library/jest-dom"; |
| 3 | +import { screen } from "@testing-library/react"; |
| 4 | +import { render } from "@web/__tests__/__mocks__/mock.render"; |
| 5 | + |
| 6 | +// Mock the react-cmdk library |
| 7 | +jest.mock("react-cmdk", () => { |
| 8 | + const MockCommandPalette = ({ children, isOpen }: any) => ( |
| 9 | + <div |
| 10 | + className="command-palette" |
| 11 | + data-testid="command-palette" |
| 12 | + style={{ display: isOpen ? "block" : "none" }} |
| 13 | + > |
| 14 | + <div className="overflow-y-auto" data-testid="scrollable-content"> |
| 15 | + {children} |
| 16 | + </div> |
| 17 | + </div> |
| 18 | + ); |
| 19 | + |
| 20 | + MockCommandPalette.Page = ({ children }: any) => <div>{children}</div>; |
| 21 | + MockCommandPalette.List = ({ children }: any) => <div>{children}</div>; |
| 22 | + MockCommandPalette.ListItem = ({ children }: any) => <div>{children}</div>; |
| 23 | + MockCommandPalette.FreeSearchAction = () => <div>No results</div>; |
| 24 | + |
| 25 | + return { |
| 26 | + __esModule: true, |
| 27 | + default: MockCommandPalette, |
| 28 | + filterItems: jest.fn((items) => items), |
| 29 | + getItemIndex: jest.fn(() => 0), |
| 30 | + useHandleOpenCommandPalette: jest.fn(), |
| 31 | + }; |
| 32 | +}); |
| 33 | + |
| 34 | +// Mock the necessary modules |
| 35 | +jest.mock("@web/store/store.hooks", () => ({ |
| 36 | + useAppDispatch: () => jest.fn(), |
| 37 | + useAppSelector: (selector: any) => { |
| 38 | + if (selector.toString().includes("selectIsCmdPaletteOpen")) { |
| 39 | + return true; // Make the command palette open for testing |
| 40 | + } |
| 41 | + return false; |
| 42 | + }, |
| 43 | +})); |
| 44 | + |
| 45 | +const CmdPalette = require("./CmdPalette").default; |
| 46 | + |
| 47 | +const mockProps = { |
| 48 | + today: { |
| 49 | + format: jest.fn(() => "Monday, January 1"), |
| 50 | + } as any, |
| 51 | + isCurrentWeek: true, |
| 52 | + startOfView: new Date(), |
| 53 | + endOfView: new Date(), |
| 54 | + util: { |
| 55 | + goToToday: jest.fn(), |
| 56 | + } as any, |
| 57 | + scrollUtil: { |
| 58 | + scrollToNow: jest.fn(), |
| 59 | + } as any, |
| 60 | +}; |
| 61 | + |
| 62 | +describe("CmdPalette Scrollbar", () => { |
| 63 | + beforeEach(() => { |
| 64 | + jest.clearAllMocks(); |
| 65 | + }); |
| 66 | + |
| 67 | + it("renders command palette with scrollable content", () => { |
| 68 | + render(<CmdPalette {...mockProps} />); |
| 69 | + |
| 70 | + const commandPalette = screen.getByTestId("command-palette"); |
| 71 | + const scrollableContent = screen.getByTestId("scrollable-content"); |
| 72 | + |
| 73 | + expect(commandPalette).toBeInTheDocument(); |
| 74 | + expect(scrollableContent).toBeInTheDocument(); |
| 75 | + }); |
| 76 | + |
| 77 | + it("applies correct CSS classes for scrollbar styling", () => { |
| 78 | + render(<CmdPalette {...mockProps} />); |
| 79 | + |
| 80 | + const commandPalette = screen.getByTestId("command-palette"); |
| 81 | + const scrollableContent = screen.getByTestId("scrollable-content"); |
| 82 | + |
| 83 | + expect(commandPalette).toHaveClass("command-palette"); |
| 84 | + expect(scrollableContent).toHaveClass("overflow-y-auto"); |
| 85 | + }); |
| 86 | + |
| 87 | + it("scrollbar styling is applied through GlobalStyle", () => { |
| 88 | + render(<CmdPalette {...mockProps} />); |
| 89 | + |
| 90 | + // Check that the GlobalStyle component is rendered |
| 91 | + // The actual scrollbar styling is tested through CSS-in-JS |
| 92 | + const scrollableContent = screen.getByTestId("scrollable-content"); |
| 93 | + |
| 94 | + // Verify the element has the correct class that targets our CSS |
| 95 | + expect(scrollableContent).toHaveClass("overflow-y-auto"); |
| 96 | + |
| 97 | + // The scrollbar styling itself is handled by CSS and cannot be directly |
| 98 | + // tested in jsdom, but we can verify the structure is correct |
| 99 | + const commandPalette = scrollableContent.closest(".command-palette"); |
| 100 | + expect(commandPalette).toBeInTheDocument(); |
| 101 | + }); |
| 102 | + |
| 103 | + it("maintains accessibility for scrollable content", () => { |
| 104 | + render(<CmdPalette {...mockProps} />); |
| 105 | + |
| 106 | + const scrollableContent = screen.getByTestId("scrollable-content"); |
| 107 | + |
| 108 | + // Verify the scrollable content is accessible |
| 109 | + expect(scrollableContent).toBeInTheDocument(); |
| 110 | + expect(scrollableContent).toHaveClass("overflow-y-auto"); |
| 111 | + |
| 112 | + // The scrollbar should not interfere with keyboard navigation |
| 113 | + // This is ensured by using standard overflow CSS properties |
| 114 | + }); |
| 115 | + |
| 116 | + describe("CSS Scrollbar Properties", () => { |
| 117 | + it("uses theme colors for scrollbar styling", () => { |
| 118 | + render(<CmdPalette {...mockProps} />); |
| 119 | + |
| 120 | + // The GlobalStyle component should be present to apply scrollbar styles |
| 121 | + // We can't directly test CSS-in-JS styles in jsdom, but we verify |
| 122 | + // the component structure supports the styling |
| 123 | + const scrollableContent = screen.getByTestId("scrollable-content"); |
| 124 | + const commandPalette = scrollableContent.closest(".command-palette"); |
| 125 | + |
| 126 | + expect(commandPalette).toBeInTheDocument(); |
| 127 | + expect(scrollableContent).toHaveClass("overflow-y-auto"); |
| 128 | + }); |
| 129 | + |
| 130 | + it("provides proper scrollbar width and styling", () => { |
| 131 | + render(<CmdPalette {...mockProps} />); |
| 132 | + |
| 133 | + const scrollableContent = screen.getByTestId("scrollable-content"); |
| 134 | + |
| 135 | + // Verify the element structure that our CSS targets |
| 136 | + expect(scrollableContent).toHaveClass("overflow-y-auto"); |
| 137 | + |
| 138 | + // The CSS rules are: |
| 139 | + // - 8px width for webkit scrollbar |
| 140 | + // - Theme-based colors for thumb |
| 141 | + // - Hover states for better UX |
| 142 | + // These are applied through the GlobalStyle component |
| 143 | + }); |
| 144 | + }); |
| 145 | + |
| 146 | + describe("Cross-browser Compatibility", () => { |
| 147 | + it("supports both webkit and standard scrollbar properties", () => { |
| 148 | + render(<CmdPalette {...mockProps} />); |
| 149 | + |
| 150 | + const scrollableContent = screen.getByTestId("scrollable-content"); |
| 151 | + |
| 152 | + // Our CSS implementation supports: |
| 153 | + // - webkit browsers (Chrome, Safari, Edge) with ::-webkit-scrollbar |
| 154 | + // - Firefox with scrollbar-width and scrollbar-color |
| 155 | + expect(scrollableContent).toHaveClass("overflow-y-auto"); |
| 156 | + }); |
| 157 | + }); |
| 158 | +}); |
0 commit comments