UNPKG

one

Version:

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

1,581 lines (1,580 loc) 39.8 kB
import { expect, test } from "vitest"; import { getPathFromState } from "../getPathFromState.native.js"; import { getStateFromPath } from "../getStateFromPath.native.js"; test("converts state to path string", function () { var state = { routes: [{ name: "foo", state: { index: 1, routes: [{ name: "boo" }, { name: "bar", params: { fruit: "apple" }, state: { routes: [{ name: "baz qux", params: { author: "jane", valid: !0 } }] } }] } }] }, path = "/foo/bar/baz%20qux?author=jane&valid=true"; expect(getPathFromState(state)).toBe(path), expect(getPathFromState(getStateFromPath(path))).toBe(path); }); test("converts state to path string with config", function () { var path = "/few/bar/sweet/apple/baz/jane?id=x10&valid=true", config = { screens: { Foo: { path: "few", screens: { Bar: { path: "bar/:type/:fruit", screens: { Baz: { path: "baz/:author", parse: { author: function (author) { return author.replace(/^\w/, function (c) { return c.toUpperCase(); }); }, id: function (id) { return Number(id.replace(/^x/, "")); }, valid: Boolean }, stringify: { author: function (author) { return author.toLowerCase(); }, id: function (id) { return `x${id}`; } } } } } } } } }, state = { routes: [{ name: "Foo", state: { index: 1, routes: [{ name: "boo" }, { name: "Bar", params: { fruit: "apple", type: "sweet", avaliable: !1 }, state: { routes: [{ name: "Baz", params: { author: "Jane", id: 10, valid: !0 } }] } }] } }] }; expect(getPathFromState(state, config)).toBe(path), expect(getPathFromState(getStateFromPath(path, config), config)).toBe(path); }); test("prepends trailing slash to path", function () { expect(getPathFromState({ routes: [{ name: "foo", state: { routes: [{ name: "bar" }] } }] })).toBe("/foo/bar"), expect(getPathFromState({ routes: [{ name: "foo", state: { routes: [{ name: "bar", path: "foo/bar" }] } }] })).toBe("/foo/bar"); }); test("handles route without param", function () { var path = "/foo/bar", state = { routes: [{ name: "foo", state: { routes: [{ name: "bar" }] } }] }; expect(getPathFromState(state)).toBe(path), expect(getPathFromState(getStateFromPath(path))).toBe(path); }); test("doesn't add query param for empty params", function () { var path = "/foo", state = { routes: [{ name: "foo", params: {} }] }; expect(getPathFromState(state)).toBe(path), expect(getPathFromState(getStateFromPath(path))).toBe(path); }); test("handles state with config with nested screens", function () { var path = "/foo/foe/bar/sweet/apple/baz/jane?answer=42&count=10&valid=true", config = { screens: { Foo: { path: "foo", screens: { Foe: { path: "foe", screens: { Bar: { path: "bar/:type/:fruit", screens: { Baz: { path: "baz/:author", parse: { author: function (author) { return author.replace(/^\w/, function (c) { return c.toUpperCase(); }); }, count: Number, valid: Boolean }, stringify: { author: function (author) { return author.toLowerCase(); }, id: function (id) { return `x${id}`; }, unknown: function (_) { return "x"; } } } } } } } } } } }, state = { routes: [{ name: "Foo", state: { routes: [{ name: "Foe", state: { routes: [{ name: "Bar", params: { fruit: "apple", type: "sweet" }, state: { routes: [{ name: "Baz", params: { answer: "42", author: "Jane", count: "10", valid: !0 } }] } }] } }] } }] }; expect(getPathFromState(state, config)).toBe(path), expect(getPathFromState(getStateFromPath(path, config), config)).toBe(path); }); test("handles state with config with nested screens and exact", function () { var path = "/foe/bar/sweet/apple/baz/jane?answer=42&count=10&valid=true", config = { screens: { Foo: { path: "foo", screens: { Foe: { path: "foe", exact: !0, screens: { Bar: { path: "bar/:type/:fruit", screens: { Baz: { path: "baz/:author", parse: { author: function (author) { return author.replace(/^\w/, function (c) { return c.toUpperCase(); }); }, count: Number, valid: Boolean }, stringify: { author: function (author) { return author.toLowerCase(); }, id: function (id) { return `x${id}`; }, unknown: function (_) { return "x"; } } } } } } } } } } }, state = { routes: [{ name: "Foo", state: { routes: [{ name: "Foe", state: { routes: [{ name: "Bar", params: { fruit: "apple", type: "sweet" }, state: { routes: [{ name: "Baz", params: { answer: "42", author: "Jane", count: "10", valid: !0 } }] } }] } }] } }] }; expect(getPathFromState(state, config)).toBe(path), expect(getPathFromState(getStateFromPath(path, config), config)).toBe(path); }); test("handles state with config with nested screens and unused configs", function () { var path = "/foo/foe/baz/jane?answer=42&count=10&valid=true", config = { screens: { Foo: { path: "foo", screens: { Foe: { path: "foe", screens: { Baz: { path: "baz/:author", parse: { author: function (author) { return author.replace(/^\w/, function (c) { return c.toUpperCase(); }); }, count: Number, valid: Boolean }, stringify: { author: function (author) { return author.replace(/^\w/, function (c) { return c.toLowerCase(); }); }, unknown: function (_) { return "x"; } } } } } } } } }, state = { routes: [{ name: "Foo", state: { routes: [{ name: "Foe", state: { routes: [{ name: "Baz", params: { answer: "42", author: "Jane", count: 10, valid: !0 } }] } }] } }] }; expect(getPathFromState(state, config)).toBe(path), expect(getPathFromState(getStateFromPath(path, config), config)).toBe(path); }); test("handles state with config with nested screens and unused configs with exact", function () { var path = "/foe/baz/jane?answer=42&count=10&valid=true", config = { screens: { Foo: { path: "foo", screens: { Foe: { path: "foe", exact: !0, screens: { Baz: { path: "baz/:author", parse: { author: function (author) { return author.replace(/^\w/, function (c) { return c.toUpperCase(); }); }, count: Number, valid: Boolean }, stringify: { author: function (author) { return author.replace(/^\w/, function (c) { return c.toLowerCase(); }); }, unknown: function (_) { return "x"; } } } } } } } } }, state = { routes: [{ name: "Foo", state: { routes: [{ name: "Foe", state: { routes: [{ name: "Baz", params: { answer: "42", author: "Jane", count: 10, valid: !0 } }] } }] } }] }; expect(getPathFromState(state, config)).toBe(path), expect(getPathFromState(getStateFromPath(path, config), config)).toBe(path); }); test("handles nested object with stringify in it", function () { var path = "/bar/sweet/apple/foo/bis/jane?answer=42&count=10&valid=true", config = { screens: { Bar: { path: "bar/:type/:fruit", screens: { Foo: { path: "foo", screens: { Foe: { path: "foe" }, Baz: { screens: { Bos: "bos", Bis: { path: "bis/:author", stringify: { author: function (author) { return author.replace(/^\w/, function (c) { return c.toLowerCase(); }); } }, parse: { author: function (author) { return author.replace(/^\w/, function (c) { return c.toUpperCase(); }); }, count: Number, valid: Boolean } } } } } } } } } }, state = { routes: [{ name: "Bar", params: { fruit: "apple", type: "sweet" }, state: { routes: [{ name: "Foo", state: { routes: [{ name: "Baz", state: { routes: [{ name: "Bis", params: { answer: "42", author: "Jane", count: 10, valid: !0 } }] } }] } }] } }] }; expect(getPathFromState(state, config)).toBe(path), expect(getPathFromState(getStateFromPath(path, config), config)).toBe(path); }); test("handles nested object with stringify in it with exact", function () { var path = "/bis/jane?answer=42&count=10&valid=true", config = { screens: { Bar: { path: "bar/:type/:fruit", screens: { Foo: { path: "foo", screens: { Foe: { path: "foe" }, Baz: { path: "baz", screens: { Bos: "bos", Bis: { path: "bis/:author", exact: !0, stringify: { author: function (author) { return author.replace(/^\w/, function (c) { return c.toLowerCase(); }); } }, parse: { author: function (author) { return author.replace(/^\w/, function (c) { return c.toUpperCase(); }); }, count: Number, valid: Boolean } } } } } } } } } }, state = { routes: [{ name: "Bar", params: { fruit: "apple", type: "sweet" }, state: { routes: [{ name: "Foo", state: { routes: [{ name: "Baz", state: { routes: [{ name: "Bis", params: { answer: "42", author: "Jane", count: 10, valid: !0 } }] } }] } }] } }] }; expect(getPathFromState(state, config)).toBe(path), expect(getPathFromState(getStateFromPath(path, config), config)).toBe(path); }); test("handles nested object for second route depth", function () { var path = "/foo/bar/baz", config = { screens: { Foo: { path: "foo", screens: { Foe: "foe", Bar: { path: "bar", screens: { Baz: "baz" } } } } } }, state = { routes: [{ name: "Foo", state: { routes: [{ name: "Bar", state: { routes: [{ name: "Baz" }] } }] } }] }; expect(getPathFromState(state, config)).toBe(path), expect(getPathFromState(getStateFromPath(path, config), config)).toBe(path); }); test("handles nested object for second route depth with exact", function () { var path = "/baz", config = { screens: { Foo: { path: "foo", screens: { Foe: "foe", Bar: { path: "bar", screens: { Baz: { path: "baz", exact: !0 } } } } } } }, state = { routes: [{ name: "Foo", state: { routes: [{ name: "Bar", state: { routes: [{ name: "Baz" }] } }] } }] }; expect(getPathFromState(state, config)).toBe(path), expect(getPathFromState(getStateFromPath(path, config), config)).toBe(path); }); test("handles nested object for second route depth and path and stringify in roots", function () { var path = "/foo/dathomir/bar/42/baz", config = { screens: { Foo: { path: "foo/:planet", stringify: { id: function (id) { return `planet=${id}`; } }, screens: { Foe: "foe", Bar: { path: "bar/:id", parse: { id: Number }, screens: { Baz: "baz" } } } } } }, state = { routes: [{ name: "Foo", params: { planet: "dathomir" }, state: { routes: [{ name: "Bar", state: { routes: [{ name: "Baz", params: { id: 42 } }] } }] } }] }; expect(getPathFromState(state, config)).toBe(path), expect(getPathFromState(getStateFromPath(path, config), config)).toBe(path); }); test("handles nested object for second route depth and path and stringify in roots with exact", function () { var path = "/baz", config = { screens: { Foo: { path: "foo/:id", stringify: { id: function (id) { return `id=${id}`; } }, screens: { Foe: "foe", Bar: { path: "bar/:id", stringify: { id: function (id) { return `id=${id}`; } }, parse: { id: Number }, screens: { Baz: { path: "baz", exact: !0 } } } } } } }, state = { routes: [{ name: "Foo", state: { routes: [{ name: "Bar", state: { routes: [{ name: "Baz" }] } }] } }] }; expect(getPathFromState(state, config)).toBe(path), expect(getPathFromState(getStateFromPath(path, config), config)).toBe(path); }); test("ignores empty string paths", function () { var path = "/bar", config = { screens: { Foo: { path: "", screens: { Foe: "foe" } }, Bar: "bar" } }, state = { routes: [{ name: "Foo", state: { routes: [{ name: "Bar" }] } }] }; expect(getPathFromState(state, config)).toBe(path), expect(getPathFromState(getStateFromPath(path, config), config)).toBe(path); }); test("keeps query params if path is empty", function () { var path = "/?foo=42", config = { screens: { Foo: { screens: { Foe: "foe", Bar: { screens: { Qux: { path: "", parse: { foo: Number } }, Baz: "baz" } } } } } }, state = { routes: [{ name: "Foo", state: { routes: [{ name: "Bar", state: { routes: [{ name: "Qux", params: { foo: 42 } }] } }] } }] }; expect(getPathFromState(state, config)).toBe(path), expect(getPathFromState(getStateFromPath(path, config), config)).toEqual(path); }); test("does not use Object.prototype properties as parsing functions", function () { var path = "/?toString=42", config = { screens: { Foo: { screens: { Foe: "foe", Bar: { screens: { Qux: { path: "", parse: {} }, Baz: "baz" } } } } } }, state = { routes: [{ name: "Foo", state: { routes: [{ name: "Bar", state: { routes: [{ name: "Qux", params: { toString: 42 } }] } }] } }] }; expect(getPathFromState(state, config)).toBe(path), expect(getPathFromState(getStateFromPath(path, config), config)).toEqual(path); }); test("cuts nested configs too", function () { var path = "/foo/baz", config = { screens: { Foo: { path: "foo", screens: { Bar: { path: "", screens: { Baz: { path: "baz" } } } } } } }, state = { routes: [{ name: "Foo", state: { routes: [{ name: "Bar", state: { routes: [{ name: "Baz" }] } }] } }] }; expect(getPathFromState(state, config)).toBe(path), expect(getPathFromState(getStateFromPath(path, config), config)).toBe(path); }); test("cuts nested configs too with exact", function () { var path = "/baz", config = { screens: { Foo: { path: "foo", screens: { Bar: { path: "", exact: !0, screens: { Baz: { path: "baz" } } } } } } }, state = { routes: [{ name: "Foo", state: { routes: [{ name: "Bar", state: { routes: [{ name: "Baz" }] } }] } }] }; expect(getPathFromState(state, config)).toBe(path), expect(getPathFromState(getStateFromPath(path, config), config)).toBe(path); }); test("handles empty path at the end", function () { var path = "/foo/bar", config = { screens: { Foo: { path: "foo", screens: { Bar: "bar" } }, Baz: { path: "" } } }, state = { routes: [{ name: "Foo", state: { routes: [{ name: "Bar", state: { routes: [{ name: "Baz" }] } }] } }] }; expect(getPathFromState(state, config)).toBe(path), expect(getPathFromState(getStateFromPath(path, config), config)).toBe(path); }); test('returns "/" for empty path', function () { var path = "/", config = { screens: { Foo: { path: "", screens: { Bar: "" } } } }, state = { routes: [{ name: "Foo", state: { routes: [{ name: "Bar" }] } }] }; expect(getPathFromState(state, config)).toBe(path), expect(getPathFromState(getStateFromPath(path, config), config)).toBe(path); }); test("parses no path specified", function () { var path = "/bar", config = { screens: { Foo: { screens: { Foe: {}, Bar: "bar" } } } }, state = { routes: [{ name: "Foo", state: { routes: [{ name: "Bar" }] } }] }; expect(getPathFromState(state, config)).toBe(path), expect(getPathFromState(getStateFromPath(path, config), config)).toBe(path); }); test("strips undefined query params", function () { var path = "/bar/sweet/apple/foo/bis/jane?count=10&valid=true", config = { screens: { Bar: { path: "bar/:type/:fruit", screens: { Foo: { path: "foo", screens: { Foe: { path: "foe" }, Baz: { screens: { Bos: "bos", Bis: { path: "bis/:author", stringify: { author: function (author) { return author.replace(/^\w/, function (c) { return c.toLowerCase(); }); } }, parse: { author: function (author) { return author.replace(/^\w/, function (c) { return c.toUpperCase(); }); }, count: Number, valid: Boolean } } } } } } } } } }, state = { routes: [{ name: "Bar", params: { fruit: "apple", type: "sweet" }, state: { routes: [{ name: "Foo", state: { routes: [{ name: "Baz", state: { routes: [{ name: "Bis", params: { author: "Jane", count: 10, valid: !0 } }] } }] } }] } }] }; expect(getPathFromState(state, config)).toBe(path), expect(getPathFromState(getStateFromPath(path, config), config)).toBe(path); }); test("strips undefined query params with exact", function () { var path = "/bis/jane?count=10&valid=true", config = { screens: { Bar: { path: "bar/:type/:fruit", screens: { Foo: { path: "foo", screens: { Foe: { path: "foe" }, Baz: { screens: { Bos: "bos", Bis: { path: "bis/:author", exact: !0, stringify: { author: function (author) { return author.replace(/^\w/, function (c) { return c.toLowerCase(); }); } }, parse: { author: function (author) { return author.replace(/^\w/, function (c) { return c.toUpperCase(); }); }, count: Number, valid: Boolean } } } } } } } } } }, state = { routes: [{ name: "Bar", params: { fruit: "apple", type: "sweet" }, state: { routes: [{ name: "Foo", state: { routes: [{ name: "Baz", state: { routes: [{ name: "Bis", params: { author: "Jane", count: 10, valid: !0 } }] } }] } }] } }] }; expect(getPathFromState(state, config)).toBe(path), expect(getPathFromState(getStateFromPath(path, config), config)).toBe(path); }); test("handles stripping all query params", function () { var path = "/bar/sweet/apple/foo/bis/jane", config = { screens: { Bar: { path: "bar/:type/:fruit", screens: { Foo: { path: "foo", screens: { Foe: { path: "foe" }, Baz: { screens: { Bos: "bos", Bis: { path: "bis/:author", stringify: { author: function (author) { return author.replace(/^\w/, function (c) { return c.toLowerCase(); }); } }, parse: { author: function (author) { return author.replace(/^\w/, function (c) { return c.toUpperCase(); }); }, count: Number, valid: Boolean } } } } } } } } } }, state = { routes: [{ name: "Bar", params: { fruit: "apple", type: "sweet" }, state: { routes: [{ name: "Foo", state: { routes: [{ name: "Baz", state: { routes: [{ name: "Bis", params: { author: "Jane" } }] } }] } }] } }] }; expect(getPathFromState(state, config)).toBe(path), expect(getPathFromState(getStateFromPath(path, config), config)).toBe(path); }); test("handles stripping all query params with exact", function () { var path = "/bis/jane", config = { screens: { Bar: { path: "bar/:type/:fruit", screens: { Foo: { path: "foo", screens: { Foe: { path: "foe" }, Baz: { path: "baz", screens: { Bos: "bos", Bis: { path: "bis/:author", exact: !0, stringify: { author: function (author) { return author.replace(/^\w/, function (c) { return c.toLowerCase(); }); } }, parse: { author: function (author) { return author.replace(/^\w/, function (c) { return c.toUpperCase(); }); }, count: Number, valid: Boolean } } } } } } } } } }, state = { routes: [{ name: "Bar", params: { fruit: "apple", type: "sweet" }, state: { routes: [{ name: "Foo", state: { routes: [{ name: "Baz", state: { routes: [{ name: "Bis", params: { author: "Jane" } }] } }] } }] } }] }; expect(getPathFromState(state, config)).toBe(path), expect(getPathFromState(getStateFromPath(path, config), config)).toBe(path); }); test("replaces undefined query params", function () { var path = "/bar/undefined/apple", config = { screens: { Bar: "bar/:type/:fruit" } }, state = { routes: [{ name: "Bar", params: { fruit: "apple" } }] }; expect(getPathFromState(state, config)).toBe(path), expect(getPathFromState(getStateFromPath(path, config), config)).toBe(path); }); test.skip("matches wildcard patterns at root", function () { var path = "/test/bar/42/whatever", config = { screens: { 404: "*", Foo: { screens: { Bar: { path: "/bar/:id/" } } } } }, state = { routes: [{ name: "404" }] }; expect(getPathFromState(state, config)).toBe("/404"), expect(getPathFromState(getStateFromPath(path, config), config)).toBe("/404"); }); test.skip("matches wildcard patterns at nested level", function () { var path = "/bar/42/whatever/baz/initt", config = { screens: { Foo: { screens: { Bar: { path: "/bar/:id/", screens: { 404: "*" } } } } } }, state = { routes: [{ name: "Foo", state: { routes: [{ name: "Bar", params: { id: "42" }, state: { routes: [{ name: "404" }] } }] } }] }; expect(getPathFromState(state, config)).toBe("/bar/42/404"), expect(getPathFromState(getStateFromPath(path, config), config)).toBe("/bar/42/404"); }); test.skip("matches wildcard patterns at nested level with exact", function () { var path = "/whatever", config = { screens: { Foo: { screens: { Bar: { path: "/bar/:id/", screens: { 404: { path: "*", exact: !0 } } }, Baz: {} } } } }, state = { routes: [{ name: "Foo", state: { routes: [{ name: "Bar", state: { routes: [{ name: "404" }] } }] } }] }; expect(getPathFromState(state, config)).toBe("/404"), expect(getPathFromState(getStateFromPath(path, config), config)).toBe("/404"); }); test("tries to match wildcard patterns at the end", function () { var path = "/bar/42/test", config = { screens: { Foo: { screens: { Bar: { path: "/bar/:id/", screens: { 404: "*", Test: "test" } } } } } }, state = { routes: [{ name: "Foo", state: { routes: [{ name: "Bar", params: { id: "42" }, state: { routes: [{ name: "Test" }] } }] } }] }; expect(getPathFromState(state, config)).toBe(path), expect(getPathFromState(getStateFromPath(path, config), config)).toBe(path); }); test.skip("uses nearest parent wildcard match for unmatched paths", function () { var path = "/bar/42/baz/test", config = { screens: { Foo: { screens: { Bar: { path: "/bar/:id/", screens: { Baz: "baz" } }, 404: "*" } } } }, state = { routes: [{ name: "Foo", state: { routes: [{ name: "404" }] } }] }; expect(getPathFromState(state, config)).toBe("/404"), expect(getPathFromState(getStateFromPath(path, config), config)).toBe("/404"); }); test.skip("handles path at top level", function () { var path = "foo/fruits/apple", config = { path: "foo", screens: { Foo: { screens: { Fruits: "fruits/:fruit" } } } }, state = { routes: [{ name: "Foo", state: { routes: [{ name: "Fruits", params: { fruit: "apple" } }] } }] }; expect(getPathFromState(state, config)).toBe(`/${path}`), expect(getPathFromState(getStateFromPath(path, config), config)).toBe(`/${path}`); }); test.skip("ignores regexp patterns when provided", function () { var config = { screens: { Foo: { path: "foo/:id(\\d+)", parse: { id: Number } }, Bar: { path: "foo/:id([a-z]+)" }, Baz: { path: "foo/:id(\\d+)/:name([a-z]+)" }, Qux: { path: "foo/:id(@[a-z]+)", stringify: { id: function (id) { return `@${id}`; } } } } }; expect(getPathFromState({ routes: [{ name: "Foo", params: { id: 42 } }] }, config)).toBe("/foo/42"), expect(getPathFromState({ routes: [{ name: "Bar", params: { id: "bar" } }] }, config)).toBe("/foo/bar"), expect(getPathFromState({ routes: [{ name: "Baz", params: { id: 42, name: "bar" } }] }, config)).toBe("/foo/42/bar"), expect(getPathFromState({ routes: [{ name: "Qux", params: { id: "bar" } }] }, config)).toBe("/foo/@bar"); }); test.skip("correctly handles regex pattern with slash", function () { var config = { screens: { Foo: { path: "foo/:id([a-z]+\\/)" } } }; expect(getPathFromState({ routes: [{ name: "Foo", params: { id: "bar" } }] }, config)).toBe("/foo/bar"); }); //# sourceMappingURL=getPathFromState.test.native.js.map