one
Version:
One is a new React Framework that makes Vite serve both native and web.
110 lines (109 loc) • 3.63 kB
JavaScript
import TestRenderer, { act } from "react-test-renderer";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { registerScrollGroup, ScrollBehavior } from "./ScrollBehavior.mjs";
import { jsx } from "react/jsx-runtime";
const subscriptions = vi.hoisted(() => ({
loading: void 0,
root: void 0
}));
vi.mock("../router/lastAction", () => ({
setLastAction: vi.fn()
}));
vi.mock("../router/router", () => ({
subscribeToLoadingState: subscriber => {
subscriptions.loading = subscriber;
return () => {
subscriptions.loading = void 0;
};
},
subscribeToRootState: subscriber => {
subscriptions.root = subscriber;
return () => {
subscriptions.root = void 0;
};
}
}));
globalThis.IS_REACT_ACT_ENVIRONMENT = true;
let renderer;
let location;
let scrollTo;
beforeEach(() => {
process.env.VITE_ENVIRONMENT = "client";
location = {
pathname: "/",
search: "",
hash: ""
};
scrollTo = vi.fn();
const windowMock = Object.assign(new EventTarget(), {
location,
scrollTo,
scrollY: 500
});
const storage = /* @__PURE__ */new Map();
vi.stubGlobal("window", windowMock);
vi.stubGlobal("document", {
getElementById: vi.fn()
});
vi.stubGlobal("sessionStorage", {
getItem: key => storage.get(key) ?? null,
setItem: (key, value) => storage.set(key, value),
removeItem: key => storage.delete(key)
});
act(() => {
renderer = TestRenderer.create(/* @__PURE__ */jsx(ScrollBehavior, {}));
});
});
afterEach(() => {
act(() => renderer?.unmount());
renderer = void 0;
vi.useRealTimers();
vi.unstubAllGlobals();
delete process.env.VITE_ENVIRONMENT;
});
describe("ScrollBehavior initial navigation", () => {
it("does not scroll when the navigator publishes its initial state after mount", () => {
act(() => subscriptions.root?.({}));
expect(scrollTo).not.toHaveBeenCalled();
});
it("scrolls to the top when the first state notification is a real navigation", () => {
location.pathname = "/compat/react-native-gesture-handler";
act(() => subscriptions.root?.({}));
expect(scrollTo).toHaveBeenCalledWith(0, 0);
});
it("handles a query change as a first navigation", () => {
location.search = "?platform=ios";
act(() => subscriptions.root?.({}));
expect(scrollTo).toHaveBeenCalledWith(0, 0);
});
});
describe("ScrollBehavior groups", () => {
it("preserves position between routes in the same group", () => {
vi.useFakeTimers();
const unregister = registerScrollGroup("/compat/react-native-gesture-handler");
act(() => subscriptions.root?.({}));
location.pathname = "/compat/react-native-gesture-handler";
act(() => subscriptions.root?.({}));
scrollTo.mockClear();
act(() => subscriptions.loading?.("loading"));
location.pathname = "/compat/react-native-gesture-handler/pan-gesture";
act(() => subscriptions.root?.({}));
vi.runAllTimers();
expect(scrollTo).toHaveBeenCalledWith(0, 500);
unregister();
vi.useRealTimers();
});
it("does not match a different route whose slug has the same prefix", () => {
const unregister = registerScrollGroup("/compat/react-native");
act(() => subscriptions.root?.({}));
location.pathname = "/compat/react-native/core";
act(() => subscriptions.root?.({}));
scrollTo.mockClear();
act(() => subscriptions.loading?.("loading"));
location.pathname = "/compat/react-native-gesture-handler";
act(() => subscriptions.root?.({}));
expect(scrollTo).toHaveBeenCalledWith(0, 0);
unregister();
});
});
//# sourceMappingURL=ScrollBehavior.test.mjs.map