one
Version:
One is a new React Framework that makes Vite serve both native and web.
161 lines • 5.46 kB
JavaScript
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
get: () => from[key],
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
});
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
value: mod,
enumerable: true
}) : target, mod));
var import_react_test_renderer = __toESM(require("react-test-renderer"), 1);
var import_vitest = require("vitest");
var import_getLinkingConfig = require("../router/getLinkingConfig.cjs");
var import_getRoutes = require("../router/getRoutes.cjs");
var import_testing_utils = require("../testing-utils.cjs");
var import_getStateFromPath = require("./getStateFromPath.cjs");
var import_useLinking = require("./useLinking.native");
var import_jsx_runtime = require("react/jsx-runtime");
globalThis.IS_REACT_ACT_ENVIRONMENT = true;
const linking = (0, import_getLinkingConfig.getLinkingConfig)((0, import_getRoutes.getRoutes)((0, import_testing_utils.getMockContext)(["_layout.tsx", "home/_layout.tsx", "home/[projectId]/_layout.tsx", "home/[projectId]/index.tsx", "settings.tsx"])));
const config = linking.config;
const currentState = {
stale: false,
type: "stack",
key: "root-stack",
index: 1,
routeNames: ["home", "settings"],
routes: [{
name: "home",
key: "home-travelo",
params: {
projectId: "proj_example_travelo"
},
state: {
stale: false,
type: "stack",
key: "home-stack",
index: 0,
routeNames: ["[projectId]"],
routes: [{
name: "[projectId]",
key: "project-travelo",
params: {
projectId: "proj_example_travelo"
}
}]
}
}, {
name: "settings",
key: "settings"
}]
};
async function captureIncomingAction(overrides = {}) {
const linkedState = (0, import_getStateFromPath.getStateFromPath)("/home/proj_example_pennywise?tab=design", config);
const dispatch = import_vitest.vi.fn();
let listener;
const navigation = {
dispatch,
getRootState: () => currentState
};
const ref = {
current: navigation
};
function Harness() {
(0, import_useLinking.useLinking)(ref, {
...linking,
enabled: true,
prefixes: ["contrast:///"],
getInitialURL: () => null,
getStateFromPath: () => linkedState,
subscribe: nextListener => {
listener = nextListener;
return () => {};
},
...overrides
}, () => {});
return null;
}
let renderer;
await (0, import_react_test_renderer.act)(async () => {
renderer = import_react_test_renderer.default.create(/* @__PURE__ */(0, import_jsx_runtime.jsx)(Harness, {}));
});
(0, import_react_test_renderer.act)(() => listener("contrast:///home/proj_example_pennywise?tab=design"));
return {
action: dispatch.mock.calls[0]?.[0],
linkedState,
renderer
};
}
(0, import_vitest.afterEach)(() => {
import_vitest.vi.restoreAllMocks();
});
(0, import_vitest.describe)("native incoming links", () => {
(0, import_vitest.it)("applies every nested dynamic param when leaving a sibling route", async () => {
const {
action,
renderer
} = await captureIncomingAction();
(0, import_vitest.expect)(action).toStrictEqual({
type: "NAVIGATE",
target: "root-stack",
payload: {
key: void 0,
name: "home",
params: {
projectId: "proj_example_pennywise",
screen: "[projectId]",
params: {
projectId: "proj_example_pennywise",
tab: "design",
screen: "index",
params: {
projectId: "proj_example_pennywise",
tab: "design"
}
}
}
}
});
(0, import_react_test_renderer.act)(() => renderer.unmount());
});
(0, import_vitest.it)("keeps a configured getActionFromState in control", async () => {
const customAction = {
type: "NAVIGATE",
payload: {
name: "custom"
}
};
const getActionFromState = import_vitest.vi.fn(_state => customAction);
const {
action,
linkedState,
renderer
} = await captureIncomingAction({
getActionFromState
});
(0, import_vitest.expect)(action).toBe(customAction);
(0, import_vitest.expect)(getActionFromState).toHaveBeenCalledOnce();
(0, import_vitest.expect)(getActionFromState.mock.calls[0][0]).toEqual({
...linkedState,
routes: [import_vitest.expect.not.objectContaining({
key: import_vitest.expect.anything()
})]
});
(0, import_react_test_renderer.act)(() => renderer.unmount());
});
});