UNPKG

one

Version:

One is a new React Framework that makes Vite serve both native and web.

234 lines (233 loc) 7.36 kB
import { beforeEach, describe, expect, it } from "vitest"; import { getMockConfig } from "../testing-utils.native.js"; import { getPathFromState } from "./getPathFromState.native.js"; import { getStateFromPath } from "./getStateFromPath.native.js"; import { getUrlWithReactNavigationConcessions, stripBaseUrl } from "./getStateFromPath-mods.native.js"; describe(stripBaseUrl, function () { [[ // 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(function (param) { var [path, baseUrl, result] = param; it(`strips baseUrl "${path}"`, function () { expect(stripBaseUrl(path, baseUrl)).toBe(result); }); }); }); describe("baseUrl", function () { beforeEach(function () { process.env.ONE_DEFAULT_RENDER_MODE = "spa"; }), it("accounts for baseUrl", function () { process.env.EXPO_BASE_URL = "/expo/prefix"; var path = "/expo/prefix/bar", config = getMockConfig(["_layout.tsx", "bar.tsx", "index.tsx"]); expect(getStateFromPath(path, config)).toEqual({ routes: [{ name: "bar", path: "/bar" }] }), expect(getPathFromState(getStateFromPath(path, config), config)).toBe("/expo/prefix/bar"); }), it("has baseUrl and state that does not match", function () { process.env.EXPO_BASE_URL = "/expo"; var path = "/bar", config = getMockConfig(["_layout.tsx", "bar.tsx", "index.tsx"]); expect(getStateFromPath(path, config)).toEqual({ routes: [{ name: "bar", path: "/bar" }] }), expect(getPathFromState(getStateFromPath(path, config), config)).toBe("/expo/bar"); }); }); describe(getUrlWithReactNavigationConcessions, function () { beforeEach(function () { delete process.env.EXPO_BASE_URL; }), ["/", "foo/", "foo/bar/", "foo/bar/baz/"].forEach(function (path) { it(`returns the pathname for ${path}`, function () { expect(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(function (param) { var [url, expected] = param; it(`returns the pathname for ${url}`, function () { expect(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(function (param) { var [url, expected] = param; it(`returns the pathname for ${url}`, function () { expect(getUrlWithReactNavigationConcessions(url, "gh-pages").nonstandardPathname).toBe(expected); }); }); }); describe("hash", function () { it("parses hashes", function () { expect(getStateFromPath("/hello#123", { screens: { hello: "hello" } })).toEqual({ routes: [{ name: "hello", path: "/hello#123", params: { "#": "123" } }] }); }), it("parses hashes with dynamic routes", function () { expect(getStateFromPath("/hello#123", getMockConfig(["[hello]"]))).toEqual({ routes: [{ name: "[hello]", params: { hello: "hello", "#": "123" }, path: "/hello#123" }] }); }), it("parses hashes with query params", function () { expect(getStateFromPath("/?#123", getMockConfig(["index"]))).toEqual({ routes: [{ name: "index", path: "/?#123", params: { "#": "123" } }] }); }); }); it("supports spaces", function () { expect(getStateFromPath("/hello%20world", { screens: { "hello world": "hello world" } })).toEqual({ routes: [{ name: "hello world", path: "/hello%20world" }] }), expect(getStateFromPath("/hello%20world", getMockConfig(["[hello world]"]))).toEqual({ routes: [{ name: "[hello world]", params: { "hello world": "hello world" }, path: "/hello%20world" }] }); }); it.skip("matches against dynamic groups", function () { expect(getStateFromPath("/(app)/(explore)", 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: "" }] } }] } }] }); }); it("adds dynamic route params from all levels of the path", function () { expect(getStateFromPath("/foo/bar/baz/other", 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" }] } }] } }] } }] }); }); it("handles not-found routes", function () { expect(getStateFromPath("/missing-page", getMockConfig(["+not-found", "index"]))).toEqual({ routes: [{ name: "+not-found", params: { "not-found": ["missing-page"] }, path: "/missing-page" }] }); }); it("handles query params", function () { expect(getStateFromPath("/?test=true&hello=world&array=1&array=2", getMockConfig(["index.tsx"]))).toEqual({ routes: [{ name: "index", params: { test: "true", hello: "world", array: ["1", "2"] }, path: "/?test=true&hello=world&array=1&array=2" }] }); }); it("handles query params", function () { expect(getStateFromPath("/?test=true&hello=world&array=1&array=2", getMockConfig(["index.tsx"]))).toEqual({ routes: [{ name: "index", params: { test: "true", hello: "world", array: ["1", "2"] }, path: "/?test=true&hello=world&array=1&array=2" }] }); }); it.skip("prioritizes hoisted index routes over dynamic groups", function () { expect(getStateFromPath("/(one)", getMockConfig(["(one,two)/index.tsx", "(one,two)/[slug].tsx"]))).toEqual({ routes: [{ name: "(one)/index", path: "" }] }); }); //# sourceMappingURL=getStateFromPath.test.native.js.map