one
Version:
One is a new React Framework that makes Vite serve both native and web.
291 lines (290 loc) • 9.52 kB
JavaScript
var import_vitest = require("vitest"), import_testing_utils = require("../testing-utils"), import_getPathFromState = require("./getPathFromState"), import_getStateFromPath = require("./getStateFromPath"), import_getStateFromPath_mods = require("./getStateFromPath-mods");
(0, import_vitest.describe)(import_getStateFromPath_mods.stripBaseUrl, () => {
[
[
// Input
"/",
// Base Path
"",
// Result
"/"
],
["/one/two", "/one", "/two"],
["/one/two", "/one/two", ""],
["/one/two/", "/one/two", "/"],
["///one/", "/one", "/"],
["one/", "/one", "one/"],
["/a/b", "/one", "/a/b"]
].forEach(([path, baseUrl, result]) => {
(0, import_vitest.it)(`strips baseUrl "${path}"`, () => {
(0, import_vitest.expect)((0, import_getStateFromPath_mods.stripBaseUrl)(path, baseUrl)).toBe(result);
});
});
});
(0, import_vitest.describe)("baseUrl", () => {
(0, import_vitest.beforeEach)(() => {
process.env.ONE_DEFAULT_RENDER_MODE = "spa";
}), (0, import_vitest.it)("accounts for baseUrl", () => {
process.env.EXPO_BASE_URL = "/expo/prefix";
const path = "/expo/prefix/bar", config = (0, import_testing_utils.getMockConfig)(["_layout.tsx", "bar.tsx", "index.tsx"]);
(0, import_vitest.expect)((0, import_getStateFromPath.getStateFromPath)(path, config)).toEqual({
routes: [{ name: "bar", path: "/bar" }]
}), (0, import_vitest.expect)((0, import_getPathFromState.getPathFromState)((0, import_getStateFromPath.getStateFromPath)(path, config), config)).toBe(
"/expo/prefix/bar"
);
}), (0, import_vitest.it)("has baseUrl and state that does not match", () => {
process.env.EXPO_BASE_URL = "/expo";
const path = "/bar", config = (0, import_testing_utils.getMockConfig)(["_layout.tsx", "bar.tsx", "index.tsx"]);
(0, import_vitest.expect)((0, import_getStateFromPath.getStateFromPath)(path, config)).toEqual({
routes: [{ name: "bar", path: "/bar" }]
}), (0, import_vitest.expect)((0, import_getPathFromState.getPathFromState)((0, import_getStateFromPath.getStateFromPath)(path, config), config)).toBe("/expo/bar");
});
});
(0, import_vitest.describe)(import_getStateFromPath_mods.getUrlWithReactNavigationConcessions, () => {
(0, import_vitest.beforeEach)(() => {
delete process.env.EXPO_BASE_URL;
}), ["/", "foo/", "foo/bar/", "foo/bar/baz/"].forEach((path) => {
(0, import_vitest.it)(`returns the pathname for ${path}`, () => {
(0, import_vitest.expect)((0, import_getStateFromPath_mods.getUrlWithReactNavigationConcessions)(path).nonstandardPathname).toBe(path);
});
}), [
["", "/"],
["https://acme.com/hello/world?foo=bar#123", "hello/world/"],
["https://acme.com/hello/world/?foo=bar#123", "hello/world/"]
].forEach(([url, expected]) => {
(0, import_vitest.it)(`returns the pathname for ${url}`, () => {
(0, import_vitest.expect)((0, import_getStateFromPath_mods.getUrlWithReactNavigationConcessions)(url).nonstandardPathname).toBe(expected);
});
}), [
["/gh-pages/", "/"],
["https://acme.com/gh-pages/hello/world?foo=bar#123", "hello/world/"],
["https://acme.com/gh-pages/hello/world/?foo=bar#123", "hello/world/"]
].forEach(([url, expected]) => {
(0, import_vitest.it)(`returns the pathname for ${url}`, () => {
(0, import_vitest.expect)((0, import_getStateFromPath_mods.getUrlWithReactNavigationConcessions)(url, "gh-pages").nonstandardPathname).toBe(
expected
);
});
});
});
(0, import_vitest.describe)("hash", () => {
(0, import_vitest.it)("parses hashes", () => {
(0, import_vitest.expect)(
(0, import_getStateFromPath.getStateFromPath)("/hello#123", {
screens: {
hello: "hello"
}
})
).toEqual({
routes: [
{
name: "hello",
path: "/hello#123",
params: {
"#": "123"
}
}
]
});
}), (0, import_vitest.it)("parses hashes with dynamic routes", () => {
(0, import_vitest.expect)((0, import_getStateFromPath.getStateFromPath)("/hello#123", (0, import_testing_utils.getMockConfig)(["[hello]"]))).toEqual({
routes: [
{
name: "[hello]",
params: {
hello: "hello",
"#": "123"
},
path: "/hello#123"
}
]
});
}), (0, import_vitest.it)("parses hashes with query params", () => {
(0, import_vitest.expect)((0, import_getStateFromPath.getStateFromPath)("/?#123", (0, import_testing_utils.getMockConfig)(["index"]))).toEqual({
routes: [
{
name: "index",
path: "/?#123",
params: {
"#": "123"
}
}
]
});
});
});
(0, import_vitest.it)("supports spaces", () => {
(0, import_vitest.expect)(
(0, import_getStateFromPath.getStateFromPath)("/hello%20world", {
screens: {
"hello world": "hello world"
}
})
).toEqual({
routes: [
{
name: "hello world",
path: "/hello%20world"
}
]
}), (0, import_vitest.expect)((0, import_getStateFromPath.getStateFromPath)("/hello%20world", (0, import_testing_utils.getMockConfig)(["[hello world]"]))).toEqual({
routes: [
{
name: "[hello world]",
params: {
"hello world": "hello world"
},
path: "/hello%20world"
}
]
});
});
import_vitest.it.skip("matches against dynamic groups", () => {
(0, import_vitest.expect)(
(0, import_getStateFromPath.getStateFromPath)(
"/(app)/(explore)",
(0, import_testing_utils.getMockConfig)([
"+not-found",
"(app)/_layout",
"(app)/(explore)/_layout",
"(app)/(explore)/[user]/index",
"(app)/(explore)/explore",
"(app)/([user])/_layout",
"(app)/([user])/[user]/index",
"(app)/([user])/explore"
])
)
).toEqual({
routes: [
{
name: "(app)",
params: { user: "(explore)" },
state: {
routes: [
{
name: "([user])",
params: { user: "(explore)" },
state: {
routes: [
{
name: "[user]/index",
params: { user: "(explore)" },
path: ""
}
]
}
}
]
}
}
]
});
});
(0, import_vitest.it)("adds dynamic route params from all levels of the path", () => {
(0, import_vitest.expect)(
(0, import_getStateFromPath.getStateFromPath)(
"/foo/bar/baz/other",
(0, import_testing_utils.getMockConfig)([
"[foo]/_layout.tsx",
"[foo]/bar/_layout.tsx",
"[foo]/bar/[baz]/_layout.tsx",
"[foo]/bar/[baz]/other.tsx"
])
)
).toEqual({
routes: [
{
name: "[foo]",
params: { baz: "baz", foo: "foo" },
state: {
routes: [
{
name: "bar",
params: { baz: "baz", foo: "foo" },
state: {
routes: [
{
name: "[baz]",
params: { baz: "baz", foo: "foo" },
state: {
routes: [
{
name: "other",
params: {
baz: "baz",
foo: "foo"
},
path: "/foo/bar/baz/other"
}
]
}
}
]
}
}
]
}
}
]
});
});
(0, import_vitest.it)("handles not-found routes", () => {
(0, import_vitest.expect)((0, import_getStateFromPath.getStateFromPath)("/missing-page", (0, import_testing_utils.getMockConfig)(["+not-found", "index"]))).toEqual({
routes: [
{
name: "+not-found",
params: {
"not-found": ["missing-page"]
},
path: "/missing-page"
}
]
});
});
(0, import_vitest.it)("handles query params", () => {
(0, import_vitest.expect)(
(0, import_getStateFromPath.getStateFromPath)("/?test=true&hello=world&array=1&array=2", (0, import_testing_utils.getMockConfig)(["index.tsx"]))
).toEqual({
routes: [
{
name: "index",
params: {
test: "true",
hello: "world",
array: ["1", "2"]
},
path: "/?test=true&hello=world&array=1&array=2"
}
]
});
});
(0, import_vitest.it)("handles query params", () => {
(0, import_vitest.expect)(
(0, import_getStateFromPath.getStateFromPath)("/?test=true&hello=world&array=1&array=2", (0, import_testing_utils.getMockConfig)(["index.tsx"]))
).toEqual({
routes: [
{
name: "index",
params: {
test: "true",
hello: "world",
array: ["1", "2"]
},
path: "/?test=true&hello=world&array=1&array=2"
}
]
});
});
import_vitest.it.skip("prioritizes hoisted index routes over dynamic groups", () => {
(0, import_vitest.expect)(
(0, import_getStateFromPath.getStateFromPath)("/(one)", (0, import_testing_utils.getMockConfig)(["(one,two)/index.tsx", "(one,two)/[slug].tsx"]))
).toEqual({
routes: [
{
name: "(one)/index",
path: ""
}
]
});
});
//# sourceMappingURL=getStateFromPath.test.js.map