|
| 1 | +import "@testing-library/jest-dom"; |
| 2 | +import { render, screen } from "@testing-library/react"; |
| 3 | +import userEvent from "@testing-library/user-event"; |
| 4 | +import type { Editor } from "@tiptap/react"; |
| 5 | +import { describe, expect, it, vi } from "vitest"; |
| 6 | +import { NotesToolbar, type NotesToolbarProps } from "./NotesToolbar"; |
| 7 | + |
| 8 | +vi.mock("@/components/ui/tooltip", () => ({ |
| 9 | + Tooltip: ({ children }: { children: React.ReactNode }) => children, |
| 10 | +})); |
| 11 | + |
| 12 | +vi.mock("@/contexts/I18nContext", () => ({ |
| 13 | + useScopedT: () => (key: string) => { |
| 14 | + const labels: Record<string, string> = { |
| 15 | + "tooltips.notesToolbar.play": "Play", |
| 16 | + "tooltips.notesToolbar.pause": "Pause", |
| 17 | + "tooltips.notesToolbar.speed": "Scroll speed", |
| 18 | + "tooltips.notesToolbar.decreaseSpeed": "Decrease scroll speed", |
| 19 | + "tooltips.notesToolbar.increaseSpeed": "Increase scroll speed", |
| 20 | + "tooltips.notesToolbar.fontSize": "Font size", |
| 21 | + "tooltips.notesToolbar.decreaseFontSize": "Decrease font size", |
| 22 | + "tooltips.notesToolbar.increaseFontSize": "Increase font size", |
| 23 | + "tooltips.notesToolbar.mirror": "Mirror", |
| 24 | + }; |
| 25 | + return labels[key] ?? key; |
| 26 | + }, |
| 27 | +})); |
| 28 | + |
| 29 | +function createEditor(): Editor { |
| 30 | + const chain: Record<string, ReturnType<typeof vi.fn>> = {}; |
| 31 | + for (const command of [ |
| 32 | + "focus", |
| 33 | + "toggleBold", |
| 34 | + "toggleItalic", |
| 35 | + "toggleStrike", |
| 36 | + "toggleBulletList", |
| 37 | + "toggleOrderedList", |
| 38 | + "toggleBlockquote", |
| 39 | + "toggleCodeBlock", |
| 40 | + ]) { |
| 41 | + chain[command] = vi.fn(() => chain); |
| 42 | + } |
| 43 | + chain.run = vi.fn(() => true); |
| 44 | + |
| 45 | + return { |
| 46 | + can: () => ({ chain: () => chain }), |
| 47 | + chain: () => chain, |
| 48 | + isActive: () => false, |
| 49 | + on: vi.fn(), |
| 50 | + off: vi.fn(), |
| 51 | + } as unknown as Editor; |
| 52 | +} |
| 53 | + |
| 54 | +function createProps(overrides: Partial<NotesToolbarProps> = {}): NotesToolbarProps { |
| 55 | + return { |
| 56 | + editor: createEditor(), |
| 57 | + isPlaying: false, |
| 58 | + speed: 40, |
| 59 | + fontSize: 16, |
| 60 | + mirrored: false, |
| 61 | + onTogglePlaying: vi.fn(), |
| 62 | + onDecreaseSpeed: vi.fn(), |
| 63 | + onIncreaseSpeed: vi.fn(), |
| 64 | + onDecreaseFontSize: vi.fn(), |
| 65 | + onIncreaseFontSize: vi.fn(), |
| 66 | + onToggleMirror: vi.fn(), |
| 67 | + ...overrides, |
| 68 | + }; |
| 69 | +} |
| 70 | + |
| 71 | +describe("NotesToolbar teleprompter controls", () => { |
| 72 | + it("exposes values and dispatches every manual control", async () => { |
| 73 | + const user = userEvent.setup(); |
| 74 | + const props = createProps(); |
| 75 | + render(<NotesToolbar {...props} />); |
| 76 | + |
| 77 | + expect(screen.getByRole("status", { name: "Scroll speed" })).toHaveTextContent("40 px/s"); |
| 78 | + expect(screen.getByRole("status", { name: "Font size" })).toHaveTextContent("16 px"); |
| 79 | + expect(screen.getByRole("button", { name: "Mirror" })).toHaveAttribute("aria-pressed", "false"); |
| 80 | + expect(screen.getByRole("button", { name: "Decrease scroll speed" })).not.toHaveAttribute( |
| 81 | + "aria-pressed", |
| 82 | + ); |
| 83 | + expect(screen.getByRole("button", { name: "Increase font size" })).not.toHaveAttribute( |
| 84 | + "aria-pressed", |
| 85 | + ); |
| 86 | + |
| 87 | + await user.click(screen.getByRole("button", { name: "Play" })); |
| 88 | + await user.click(screen.getByRole("button", { name: "Decrease scroll speed" })); |
| 89 | + await user.click(screen.getByRole("button", { name: "Increase scroll speed" })); |
| 90 | + await user.click(screen.getByRole("button", { name: "Decrease font size" })); |
| 91 | + await user.click(screen.getByRole("button", { name: "Increase font size" })); |
| 92 | + await user.click(screen.getByRole("button", { name: "Mirror" })); |
| 93 | + |
| 94 | + expect(props.onTogglePlaying).toHaveBeenCalledOnce(); |
| 95 | + expect(props.onDecreaseSpeed).toHaveBeenCalledOnce(); |
| 96 | + expect(props.onIncreaseSpeed).toHaveBeenCalledOnce(); |
| 97 | + expect(props.onDecreaseFontSize).toHaveBeenCalledOnce(); |
| 98 | + expect(props.onIncreaseFontSize).toHaveBeenCalledOnce(); |
| 99 | + expect(props.onToggleMirror).toHaveBeenCalledOnce(); |
| 100 | + }); |
| 101 | + |
| 102 | + it("disables controls at their bounds", () => { |
| 103 | + const { rerender } = render(<NotesToolbar {...createProps({ speed: 10, fontSize: 14 })} />); |
| 104 | + expect(screen.getByRole("button", { name: "Decrease scroll speed" })).toBeDisabled(); |
| 105 | + expect(screen.getByRole("button", { name: "Decrease font size" })).toBeDisabled(); |
| 106 | + |
| 107 | + rerender(<NotesToolbar {...createProps({ speed: 100, fontSize: 48 })} />); |
| 108 | + expect(screen.getByRole("button", { name: "Increase scroll speed" })).toBeDisabled(); |
| 109 | + expect(screen.getByRole("button", { name: "Increase font size" })).toBeDisabled(); |
| 110 | + }); |
| 111 | + |
| 112 | + it("keeps playback paused and disabled until the editor is ready", () => { |
| 113 | + render(<NotesToolbar {...createProps({ editor: null })} />); |
| 114 | + expect(screen.getByRole("button", { name: "Play" })).toBeDisabled(); |
| 115 | + expect(screen.queryByRole("button", { name: "Pause" })).not.toBeInTheDocument(); |
| 116 | + }); |
| 117 | +}); |
0 commit comments