Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

SettingsDragHandle.test.tsx

75 lines2.3 KB

1import type { ReactNode } from "react";
2import { act } from "react";
3import { createRoot } from "react-dom/client";
4import { describe, expect, it, vi } from "vite-plus/test";
5​
6type PanEvent = { readonly translationY: number };
7type PanHandlers = {
8 onStart?: () => void;
9 onUpdate?: (event: PanEvent) => void;
10 onFinalize?: (event: PanEvent, success: boolean) => void;
11};
12​
13const pans = vi.hoisted(() => [] as PanHandlers[]);
14​
15vi.mock("react-native", () => ({
16 View: ({ children }: { children?: ReactNode }) => <div>{children}</div>,
17}));
18vi.mock("react-native-gesture-handler", () => ({
19 GestureDetector: ({ children }: { children?: ReactNode }) => children,
20 Gesture: {
21 Pan: () => {
22 const handlers: PanHandlers = {};
23 pans.push(handlers);
24 const pan = {
25 minDistance: () => pan,
26 shouldCancelWhenOutside: () => pan,
27 runOnJS: () => pan,
28 onStart: (callback: PanHandlers["onStart"]) => ((handlers.onStart = callback), pan),
29 onUpdate: (callback: PanHandlers["onUpdate"]) => ((handlers.onUpdate = callback), pan),
30 onFinalize: (callback: PanHandlers["onFinalize"]) => (
31 (handlers.onFinalize = callback),
32 pan
33 ),
34 };
35 return pan;
36 },
37 },
38}));
39vi.mock("../../../components/ui/AppSymbol", () => ({ SymbolView: () => null }));
40​
41import { SettingsDragHandle } from "./SettingsDragHandle";
42​
43describe("SettingsDragHandle", () => {
44 // The worklets Babel plugin copies values a gesture callback closes over when the gesture is
45 // built, so a drop must not read a translation tracked in a local variable.
46 it("reports the released translation when the pan ends", () => {
47 pans.length = 0;
48 const onEnd = vi.fn();
49 const root = createRoot(document.createElement("div"));
50 act(() =>
51 root.render(
52 <SettingsDragHandle
53 title="Built-in microphone"
54 canMoveUp
55 canMoveDown
56 onStart={() => {}}
57 onMove={() => {}}
58 onEnd={onEnd}
59 onStep={() => {}}
60 />,
61 ),
62 );
63 const pan = pans.at(-1)!;
64​
65 act(() => {
66 pan.onStart?.();
67 pan.onUpdate?.({ translationY: 64 });
68 pan.onUpdate?.({ translationY: 130 });
69 pan.onFinalize?.({ translationY: 130 }, true);
70 });
71​
72 expect(onEnd).toHaveBeenCalledWith(130, false);
73 act(() => root.unmount());
74 });
75});