|
| 1 | +/* |
| 2 | + * Copyright (C) 2021 Sienci Labs Inc. |
| 3 | + * |
| 4 | + * This file is part of gSender. |
| 5 | + * |
| 6 | + * gSender is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU General Public License as published by |
| 8 | + * the Free Software Foundation, under version 3 of the License. |
| 9 | + * |
| 10 | + * gSender is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with gSender. If not, see <https://www.gnu.org/licenses/>. |
| 17 | + * |
| 18 | + * Contact for information regarding this program and its license |
| 19 | + * can be sent through gSender@sienci.com or mailed to the main office |
| 20 | + * of Sienci Labs Inc. in Waterloo, Ontario, Canada. |
| 21 | + * |
| 22 | + */ |
| 23 | + |
| 24 | +import { act, fireEvent, render, screen } from "@testing-library/react"; |
| 25 | +import { JogWheel } from "../JogWheel"; |
| 26 | + |
| 27 | +// Mock-prefixed so babel-plugin-jest-hoist allows the jest.mock() factory below |
| 28 | +// (hoisted above this declaration) to close over it. |
| 29 | +const mockCommand = jest.fn(); |
| 30 | + |
| 31 | +jest.mock("app/lib/controller", () => ({ |
| 32 | + __esModule: true, |
| 33 | + default: { |
| 34 | + command: (...args: unknown[]) => mockCommand(...args), |
| 35 | + }, |
| 36 | +})); |
| 37 | + |
| 38 | +jest.mock("app/store", () => ({ |
| 39 | + __esModule: true, |
| 40 | + default: { get: (_key: string, fallback: unknown) => fallback }, |
| 41 | +})); |
| 42 | + |
| 43 | +jest.mock("@posthog/react", () => ({ |
| 44 | + usePostHog: () => null, |
| 45 | +})); |
| 46 | + |
| 47 | +jest.mock("app/hooks/useWorkspaceState", () => ({ |
| 48 | + useWorkspaceState: () => ({ mode: "DEFAULT" }), |
| 49 | +})); |
| 50 | + |
| 51 | +const THRESHOLD = 200; |
| 52 | + |
| 53 | +const renderWheel = () => |
| 54 | + render( |
| 55 | + <JogWheel canClick feedrate={1000} distance={1} threshold={THRESHOLD} />, |
| 56 | + ); |
| 57 | + |
| 58 | +const commandsNamed = (name: string) => |
| 59 | + mockCommand.mock.calls.filter((call) => call[0] === name); |
| 60 | + |
| 61 | +beforeEach(() => { |
| 62 | + jest.useFakeTimers(); |
| 63 | + mockCommand.mockClear(); |
| 64 | +}); |
| 65 | + |
| 66 | +afterEach(() => { |
| 67 | + jest.useRealTimers(); |
| 68 | +}); |
| 69 | + |
| 70 | +describe("JogWheel continuous jog release", () => { |
| 71 | + it("stops the jog when the browser cancels the pointer", () => { |
| 72 | + renderWheel(); |
| 73 | + const wedge = screen.getByLabelText("Jog X plus"); |
| 74 | + |
| 75 | + fireEvent.pointerDown(wedge, { button: 0 }); |
| 76 | + act(() => { |
| 77 | + jest.advanceTimersByTime(THRESHOLD + 10); |
| 78 | + }); |
| 79 | + expect(commandsNamed("jog:start")).toHaveLength(1); |
| 80 | + |
| 81 | + // A cancelled pointer never produces the pointerup that use-long-press |
| 82 | + // listens for, so without our own handler the machine would keep going. |
| 83 | + fireEvent.pointerCancel(wedge); |
| 84 | + |
| 85 | + expect(commandsNamed("jog:stop")).toHaveLength(1); |
| 86 | + }); |
| 87 | + |
| 88 | + it("still stops on an ordinary release", () => { |
| 89 | + renderWheel(); |
| 90 | + const wedge = screen.getByLabelText("Jog X plus"); |
| 91 | + |
| 92 | + fireEvent.pointerDown(wedge, { button: 0 }); |
| 93 | + act(() => { |
| 94 | + jest.advanceTimersByTime(THRESHOLD + 10); |
| 95 | + }); |
| 96 | + fireEvent.pointerUp(wedge); |
| 97 | + |
| 98 | + expect(commandsNamed("jog:start")).toHaveLength(1); |
| 99 | + expect(commandsNamed("jog:stop")).toHaveLength(1); |
| 100 | + }); |
| 101 | + |
| 102 | + it("treats a press shorter than the threshold as a single step jog", () => { |
| 103 | + renderWheel(); |
| 104 | + const wedge = screen.getByLabelText("Jog X plus"); |
| 105 | + |
| 106 | + fireEvent.pointerDown(wedge, { button: 0 }); |
| 107 | + act(() => { |
| 108 | + jest.advanceTimersByTime(THRESHOLD / 2); |
| 109 | + }); |
| 110 | + fireEvent.pointerUp(wedge); |
| 111 | + |
| 112 | + expect(commandsNamed("jog:start")).toHaveLength(0); |
| 113 | + expect(commandsNamed("gcode")).toHaveLength(1); |
| 114 | + }); |
| 115 | + |
| 116 | + it("ignores a secondary-button press so no jog is left running", () => { |
| 117 | + renderWheel(); |
| 118 | + const wedge = screen.getByLabelText("Jog X plus"); |
| 119 | + |
| 120 | + // The context menu swallows the release, so a right-click must never |
| 121 | + // start a jog in the first place. Dispatched as a MouseEvent because |
| 122 | + // jsdom has no PointerEvent, and fireEvent's fallback plain Event drops |
| 123 | + // the `button` this case is entirely about. |
| 124 | + fireEvent( |
| 125 | + wedge, |
| 126 | + new MouseEvent("pointerdown", { bubbles: true, button: 2 }), |
| 127 | + ); |
| 128 | + act(() => { |
| 129 | + jest.advanceTimersByTime(THRESHOLD + 10); |
| 130 | + }); |
| 131 | + |
| 132 | + expect(commandsNamed("jog:start")).toHaveLength(0); |
| 133 | + }); |
| 134 | +}); |
0 commit comments