-
-
Notifications
You must be signed in to change notification settings - Fork 3
Add remote controls input modal #130
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3d649c8
Add remote controls input modal
wizzomafizzo 5e2f4ef
Merge branch 'main' into feature/remote-controls-inputs
wizzomafizzo 2ae27c2
Address remote controls review feedback
wizzomafizzo 1ccf6be
Merge remote-tracking branch 'origin/feature/remote-controls-inputs' …
wizzomafizzo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
334 changes: 334 additions & 0 deletions
334
src/__tests__/unit/components/RemoteKeyboardModal.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,334 @@ | ||
| import { describe, it, expect, vi, beforeEach } from "vitest"; | ||
| import { render, screen, waitFor } from "@/test-utils"; | ||
| import userEvent from "@testing-library/user-event"; | ||
| import { useStatusStore } from "@/lib/store"; | ||
| import { RemoteKeyboardModal } from "@/components/RemoteKeyboardModal"; | ||
| import { CoreAPI } from "@/lib/coreApi"; | ||
| import toast from "react-hot-toast"; | ||
|
|
||
| interface KeyboardMockProps { | ||
| layout: Record<string, string[]>; | ||
| layoutName: string; | ||
| display: Record<string, string>; | ||
| onKeyPress: (button: string) => void; | ||
| } | ||
|
|
||
| vi.mock("react-simple-keyboard", () => ({ | ||
| default: ({ layout, layoutName, display, onKeyPress }: KeyboardMockProps) => ( | ||
| <div> | ||
| {layout[layoutName]?.flatMap((row) => | ||
| row.split(" ").map((button) => ( | ||
| <button | ||
| key={`${layoutName}-${button}`} | ||
| type="button" | ||
| onClick={() => onKeyPress(button)} | ||
| > | ||
| {display[button] ?? button} | ||
| </button> | ||
| )), | ||
| )} | ||
| </div> | ||
| ), | ||
| })); | ||
|
|
||
| vi.mock("react-hot-toast", () => ({ | ||
| default: { | ||
| error: vi.fn(), | ||
| }, | ||
| })); | ||
|
|
||
| vi.mock("@/lib/coreApi", () => ({ | ||
| CoreAPI: { | ||
| reset: vi.fn(), | ||
| inputKeyboard: vi.fn().mockResolvedValue(undefined), | ||
| screenshot: vi.fn().mockResolvedValue({ | ||
| path: "/media/fat/screenshots/MiSTer.png", | ||
| data: "iVBORw0KGgo=", | ||
| size: 12, | ||
| }), | ||
| }, | ||
| })); | ||
|
|
||
| describe("RemoteKeyboardModal", () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| CoreAPI.reset(); | ||
| useStatusStore.setState({ connected: true, corePlatform: null }); | ||
| }); | ||
|
|
||
| it("should not render redundant description text", () => { | ||
| render(<RemoteKeyboardModal isOpen close={vi.fn()} />); | ||
|
|
||
| expect( | ||
| screen.queryByText("remoteKeyboard.description"), | ||
| ).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("should size modal to content instead of fixed height", () => { | ||
| render(<RemoteKeyboardModal isOpen close={vi.fn()} />); | ||
|
|
||
| expect(screen.getByRole("dialog").style.height).toBe(""); | ||
| }); | ||
|
|
||
| it("should default to remote mode", () => { | ||
| render(<RemoteKeyboardModal isOpen close={vi.fn()} />); | ||
|
|
||
| expect( | ||
| screen.getByRole("button", { name: "remoteKeyboard.ok" }), | ||
| ).toBeInTheDocument(); | ||
| expect(screen.queryByRole("button", { name: "q" })).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("should send remote directional actions", async () => { | ||
| const user = userEvent.setup(); | ||
|
|
||
| render(<RemoteKeyboardModal isOpen close={vi.fn()} />); | ||
|
|
||
| await user.click(screen.getByRole("button", { name: "remoteKeyboard.up" })); | ||
|
|
||
| await waitFor(() => { | ||
| expect(CoreAPI.inputKeyboard).toHaveBeenCalledWith({ keys: "{up}" }); | ||
| }); | ||
| }); | ||
|
|
||
| it("should send remote OK action", async () => { | ||
| const user = userEvent.setup(); | ||
|
|
||
| render(<RemoteKeyboardModal isOpen close={vi.fn()} />); | ||
|
|
||
| await user.click(screen.getByRole("button", { name: "remoteKeyboard.ok" })); | ||
|
|
||
| await waitFor(() => { | ||
| expect(CoreAPI.inputKeyboard).toHaveBeenCalledWith({ keys: "{enter}" }); | ||
| }); | ||
| }); | ||
|
|
||
| it("should send generic remote actions when platform is unknown", async () => { | ||
| const user = userEvent.setup(); | ||
|
|
||
| render(<RemoteKeyboardModal isOpen close={vi.fn()} />); | ||
|
|
||
| await user.click( | ||
| screen.getByRole("button", { name: "remoteKeyboard.menu" }), | ||
| ); | ||
| await user.click( | ||
| screen.getByRole("button", { name: "remoteKeyboard.select" }), | ||
| ); | ||
|
|
||
| await waitFor(() => { | ||
| expect(CoreAPI.inputKeyboard).toHaveBeenCalledTimes(2); | ||
| }); | ||
| expect(CoreAPI.inputKeyboard).toHaveBeenCalledWith({ keys: "{f12}" }); | ||
| expect(CoreAPI.inputKeyboard).toHaveBeenCalledWith({ | ||
| keys: "{backspace}", | ||
| }); | ||
| }); | ||
|
|
||
| it("should send MiSTer remote actions when platform is MiSTer", async () => { | ||
| const user = userEvent.setup(); | ||
| useStatusStore.setState({ corePlatform: "mister" }); | ||
|
|
||
| render(<RemoteKeyboardModal isOpen close={vi.fn()} />); | ||
|
|
||
| expect( | ||
| screen.getByRole("button", { name: "remoteKeyboard.osd" }), | ||
| ).toBeInTheDocument(); | ||
| expect( | ||
| screen.queryByRole("button", { name: "remoteKeyboard.core" }), | ||
| ).not.toBeInTheDocument(); | ||
|
|
||
| await user.click( | ||
| screen.getByRole("button", { name: "remoteKeyboard.osd" }), | ||
| ); | ||
|
|
||
| await waitFor(() => { | ||
| expect(CoreAPI.inputKeyboard).toHaveBeenCalledWith({ keys: "{f12}" }); | ||
| }); | ||
| }); | ||
|
|
||
| it("should send Batocera remote actions when platform is Batocera", async () => { | ||
| const user = userEvent.setup(); | ||
| useStatusStore.setState({ corePlatform: "batocera" }); | ||
|
|
||
| render(<RemoteKeyboardModal isOpen close={vi.fn()} />); | ||
|
|
||
| expect( | ||
| screen.getByRole("button", { name: "remoteKeyboard.minus" }), | ||
| ).toBeInTheDocument(); | ||
| expect( | ||
| screen.getByRole("button", { name: "remoteKeyboard.equals" }), | ||
| ).toBeInTheDocument(); | ||
|
|
||
| await user.click( | ||
| screen.getByRole("button", { name: "remoteKeyboard.context" }), | ||
| ); | ||
|
|
||
| await waitFor(() => { | ||
| expect(CoreAPI.inputKeyboard).toHaveBeenCalledWith({ | ||
| keys: "{backspace}", | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| it("should capture and show an inline screenshot result", async () => { | ||
| const user = userEvent.setup(); | ||
|
|
||
| render(<RemoteKeyboardModal isOpen close={vi.fn()} />); | ||
|
|
||
| await user.click( | ||
| screen.getByRole("button", { | ||
| name: "remoteKeyboard.screenshotAction", | ||
| }), | ||
| ); | ||
|
|
||
| await waitFor(() => { | ||
| expect(CoreAPI.screenshot).toHaveBeenCalled(); | ||
| }); | ||
| expect( | ||
| screen.queryByRole("radio", { name: "remoteKeyboard.screenshotMode" }), | ||
| ).not.toBeInTheDocument(); | ||
| expect(screen.getByAltText("remoteKeyboard.screenshotAlt")).toHaveAttribute( | ||
| "src", | ||
| "data:image/png;base64,iVBORw0KGgo=", | ||
| ); | ||
| expect( | ||
| screen.getByRole("link", { | ||
| name: "remoteKeyboard.screenshotDownload", | ||
| }), | ||
| ).toHaveAttribute("download", "MiSTer.png"); | ||
|
|
||
| await user.click( | ||
| screen.getByRole("button", { name: "remoteKeyboard.screenshotClear" }), | ||
| ); | ||
|
|
||
| expect( | ||
| screen.queryByAltText("remoteKeyboard.screenshotAlt"), | ||
| ).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("should show loading state while capturing screenshot", async () => { | ||
| const user = userEvent.setup(); | ||
| let resolveScreenshot: ( | ||
| value: Awaited<ReturnType<typeof CoreAPI.screenshot>>, | ||
| ) => void; | ||
| vi.mocked(CoreAPI.screenshot).mockReturnValueOnce( | ||
| new Promise((resolve) => { | ||
| resolveScreenshot = resolve; | ||
| }), | ||
| ); | ||
|
|
||
| render(<RemoteKeyboardModal isOpen close={vi.fn()} />); | ||
|
|
||
| const screenshotButton = screen.getByRole("button", { | ||
| name: "remoteKeyboard.screenshotAction", | ||
| }); | ||
| await user.click(screenshotButton); | ||
|
|
||
| expect(screenshotButton).toBeDisabled(); | ||
| expect(screenshotButton).toHaveAttribute("aria-busy", "true"); | ||
|
|
||
| resolveScreenshot!({ | ||
| path: "/media/fat/screenshots/loading.png", | ||
| data: "iVBORw0KGgo=", | ||
| size: 12, | ||
| }); | ||
|
|
||
| await waitFor(() => { | ||
| expect(screenshotButton).not.toBeDisabled(); | ||
| }); | ||
| }); | ||
|
|
||
| it("should show an error when screenshot capture fails", async () => { | ||
| const user = userEvent.setup(); | ||
| vi.mocked(CoreAPI.screenshot).mockRejectedValueOnce(new Error("failed")); | ||
|
|
||
| render(<RemoteKeyboardModal isOpen close={vi.fn()} />); | ||
|
|
||
| await user.click( | ||
| screen.getByRole("button", { | ||
| name: "remoteKeyboard.screenshotAction", | ||
| }), | ||
| ); | ||
|
|
||
| expect( | ||
| await screen.findByText("remoteKeyboard.screenshotError"), | ||
| ).toBeInTheDocument(); | ||
| expect(toast.error).toHaveBeenCalledWith("remoteKeyboard.screenshotError"); | ||
| }); | ||
|
|
||
| it("should send literal key presses from keyboard mode", async () => { | ||
| const user = userEvent.setup(); | ||
|
|
||
| render(<RemoteKeyboardModal isOpen close={vi.fn()} />); | ||
|
|
||
| await user.click( | ||
| screen.getByRole("radio", { name: "remoteKeyboard.keyboardMode" }), | ||
| ); | ||
| await user.click(screen.getByRole("button", { name: "q" })); | ||
|
|
||
| await waitFor(() => { | ||
| expect(CoreAPI.inputKeyboard).toHaveBeenCalledWith({ keys: "q" }); | ||
| }); | ||
| }); | ||
|
|
||
| it("should send special key macros from keyboard mode", async () => { | ||
| const user = userEvent.setup(); | ||
|
|
||
| render(<RemoteKeyboardModal isOpen close={vi.fn()} />); | ||
|
|
||
| await user.click( | ||
| screen.getByRole("radio", { name: "remoteKeyboard.keyboardMode" }), | ||
| ); | ||
| await user.click(screen.getByRole("button", { name: "Enter" })); | ||
|
|
||
| await waitFor(() => { | ||
| expect(CoreAPI.inputKeyboard).toHaveBeenCalledWith({ keys: "{enter}" }); | ||
| }); | ||
| }); | ||
|
|
||
| it("should send function key macros from keyboard mode", async () => { | ||
| const user = userEvent.setup(); | ||
|
|
||
| render(<RemoteKeyboardModal isOpen close={vi.fn()} />); | ||
|
|
||
| await user.click( | ||
| screen.getByRole("radio", { name: "remoteKeyboard.keyboardMode" }), | ||
| ); | ||
| await user.click(screen.getByRole("button", { name: "Fn" })); | ||
| await user.click(screen.getByRole("button", { name: "F12" })); | ||
|
|
||
| await waitFor(() => { | ||
| expect(CoreAPI.inputKeyboard).toHaveBeenCalledWith({ keys: "{f12}" }); | ||
| }); | ||
| }); | ||
|
|
||
| it("should escape literal brace input from keyboard mode", async () => { | ||
| const user = userEvent.setup(); | ||
|
|
||
| render(<RemoteKeyboardModal isOpen close={vi.fn()} />); | ||
|
|
||
| await user.click( | ||
| screen.getByRole("radio", { name: "remoteKeyboard.keyboardMode" }), | ||
| ); | ||
| await user.click(screen.getByRole("button", { name: "#+=" })); | ||
| await user.click(screen.getByRole("button", { name: "{" })); | ||
|
|
||
| await waitFor(() => { | ||
| expect(CoreAPI.inputKeyboard).toHaveBeenCalledWith({ keys: "\\{" }); | ||
| }); | ||
| }); | ||
|
|
||
| it("should not send remote actions when disconnected", async () => { | ||
| const user = userEvent.setup(); | ||
| useStatusStore.setState({ connected: false }); | ||
|
|
||
| render(<RemoteKeyboardModal isOpen close={vi.fn()} />); | ||
|
|
||
| await user.click( | ||
| screen.getByRole("button", { name: "remoteKeyboard.menu" }), | ||
| ); | ||
|
|
||
| expect(CoreAPI.inputKeyboard).not.toHaveBeenCalled(); | ||
| expect(screen.getByText("remoteKeyboard.disconnected")).toBeInTheDocument(); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.