Skip to content

Commit

Permalink
V2 (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
zoontek authored Mar 11, 2024
1 parent ab62f7a commit 8fd358b
Show file tree
Hide file tree
Showing 61 changed files with 6,930 additions and 5,765 deletions.
2 changes: 1 addition & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ dist/
example/
docs/

vite.config.ts
vite.config.mts
vitest-setup.ts
.eslintrc.js
32 changes: 32 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Build

on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- uses: actions/setup-node@v4
with:
node-version: 20
cache: yarn

- run: yarn install --pure-lockfile
- run: yarn prepack

- name: Build docs
run: cd docs && yarn && yarn build

- name: Deploy
if: "contains('refs/heads/main', github.ref)"
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./docs/build
38 changes: 0 additions & 38 deletions .github/workflows/nodejs.yml

This file was deleted.

4 changes: 2 additions & 2 deletions ADOPTION.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { createRouter } from "@swan-io/chicane";
// Here we list all our application pages
const routes = {
Home: "/",
Teams: "/teams?:created", // chicane support search / hash params declaration
Teams: "/teams?:created", // chicane supports search params declaration
Team: "/teams/:teamId",
NewTeam: "/teams/new",
// Note that chicane "createGroup" works perfectly here! (for routes nesting)
Expand All @@ -26,7 +26,7 @@ const { getRoute, useRoute, push, replace, ...rest } = createRouter(routes);
// We exports all the link creation functions
export const Router = rest;

// We export paths (without search and hash params, as react-router-dom doesn't support them)
// We export paths (without search params, as react-router-dom doesn't support them)
export const paths = (Object.keys(routes) as (keyof typeof routes)[]).reduce(
(acc, key) => ({ ...acc, [key]: routes[key].replace(/[?#].*/, "") }),
{} as Record<keyof typeof routes, string>,
Expand Down
6 changes: 3 additions & 3 deletions __tests__/__snapshots__/server.tsx.snap
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`Should render home page correctly 1`] = `"<div><a href=\\"/\\" class=\\"active\\">Home</a><a href=\\"/users\\">Users</a><a href=\\"/users/123\\">User</a><div>Home</div></div>"`;
exports[`Should render home page correctly 1`] = `"<div><a href="/" class="active">Home</a><a href="/users">Users</a><a href="/users/123">User</a><div>Home</div></div>"`;

exports[`Should render user page correctly 1`] = `"<div><a href=\\"/\\">Home</a><a href=\\"/users\\">Users</a><a href=\\"/users/123\\" class=\\"active\\">User</a><div>User 123</div></div>"`;
exports[`Should render user page correctly 1`] = `"<div><a href="/">Home</a><a href="/users">Users</a><a href="/users/123" class="active">User</a><div>User 123</div></div>"`;

exports[`Should render users page correctly 1`] = `"<div><a href=\\"/\\">Home</a><a href=\\"/users\\" class=\\"active\\">Users</a><a href=\\"/users/123\\">User</a><div>Users</div></div>"`;
exports[`Should render users page correctly 1`] = `"<div><a href="/">Home</a><a href="/users" class="active">Users</a><a href="/users/123">User</a><div>Users</div></div>"`;
13 changes: 2 additions & 11 deletions __tests__/concatRoutes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { expect, test } from "vitest";
import { concatRoutes, parseRoute } from "../src/concatRoutes";
import { concatRoutes } from "../src/concatRoutes";
import { parseRoute } from "../src/history";

const concatRouteStrings = (routeA: string, routeB: string): string =>
concatRoutes(parseRoute(routeA), parseRoute(routeB));
Expand All @@ -20,19 +21,9 @@ test("concatRoutes perform proper routes concatenation", () => {
expect(concatRouteStrings("/", "/?:bar")).toBe("/?:bar");
expect(concatRouteStrings("/?:foo", "/?:bar")).toBe("/?:foo&:bar");

expect(concatRouteStrings("/#", "/#")).toBe("/");
expect(concatRouteStrings("/#:foo", "/")).toBe("/#:foo");
expect(concatRouteStrings("/", "/#:bar")).toBe("/#:bar");
expect(concatRouteStrings("/#:foo", "/#:bar")).toBe("/#:bar");

expect(concatRouteStrings("/foo", "/bar?:baz")).toBe("/foo/bar?:baz");
expect(concatRouteStrings("/foo#:bar", "/baz#:qux")).toBe("/foo/baz#:qux");

expect(
concatRouteStrings("/foo?:bar&:baz&:qux[]", "/quux?:corge&:grault[]"),
).toBe("/foo/quux?:bar&:baz&:qux[]&:corge&:grault[]");

expect(
concatRouteStrings("/foo?:bar&:baz[]#:qux", "/quux?:corge#:grault"),
).toBe("/foo/quux?:bar&:baz[]&:corge#:grault");
});
2 changes: 1 addition & 1 deletion __tests__/createGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ test("createGroup prefix routes keys / paths", () => {
}),
} as const;

expect(routes).toEqual({
expect(routes).toStrictEqual({
Home: "/",
User: "/:userName",

Expand Down
93 changes: 40 additions & 53 deletions __tests__/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,76 +1,63 @@
import { describe, expect, test } from "vitest";
import { expect, test } from "vitest";
import {
areParamsArrayEqual,
extractParamNameUnion,
first,
isMultipleParam,
isNonEmpty,
isParam,
} from "../src/helpers";

describe("first", () => {
test("returns the first element of an array", () => {
expect(first(["a", "b", "c"])).toBe("a");
expect(first([1, 2, 3])).toBe(1);
});
test("first", () => {
expect(first(["a", "b", "c"])).toBe("a");
expect(first([1, 2, 3])).toBe(1);

test("returns undefined if the array is empty", () => {
expect(first([])).toBeUndefined();
});
expect(first([])).toBeUndefined();
});

describe("isNonEmpty", () => {
test("returns true if the value is not an empty string", () => {
expect(isNonEmpty("test")).toBe(true);
expect(isNonEmpty(" ")).toBe(true);
});
test("isNonEmpty", () => {
expect(isNonEmpty("test")).toBe(true);
expect(isNonEmpty(" ")).toBe(true);

test("returns false if the value is an empty string", () => {
expect(isNonEmpty("")).toBe(false);
});
expect(isNonEmpty("")).toBe(false);
});

describe("isParam", () => {
test("returns true if the value is a well-formed route param", () => {
expect(isParam(":test")).toBe(true);
expect(isParam(":")).toBe(true);
expect(isParam(": ")).toBe(true);
});
test("isParam", () => {
expect(isParam(":test")).toBe(true);
expect(isParam(":")).toBe(true);
expect(isParam(": ")).toBe(true);

test("returns false if the value is not a well-formed route param", () => {
expect(isParam(" :test")).toBe(false);
expect(isParam("test")).toBe(false);
});
expect(isParam(" :test")).toBe(false);
expect(isParam("test")).toBe(false);
});

describe("areParamsArrayEqual", () => {
test("returns true with identical arrays", () => {
expect(areParamsArrayEqual([], [])).toBe(true);
expect(areParamsArrayEqual(["foo"], ["foo"])).toBe(true);
expect(areParamsArrayEqual(["foo", "bar"], ["foo", "bar"])).toBe(true);
});
test("areParamsArrayEqual", () => {
expect(areParamsArrayEqual([], [])).toBe(true);
expect(areParamsArrayEqual(["foo"], ["foo"])).toBe(true);
expect(areParamsArrayEqual(["foo", "bar"], ["foo", "bar"])).toBe(true);

test("returns false with different arrays", () => {
expect(areParamsArrayEqual(["foo"], [])).toBe(false);
expect(areParamsArrayEqual(["foo"], ["bar"])).toBe(false);
expect(areParamsArrayEqual(["foo", "bar"], ["bar", "foo"])).toBe(false);
});
expect(areParamsArrayEqual(["foo"], [])).toBe(false);
expect(areParamsArrayEqual(["foo"], ["bar"])).toBe(false);
expect(areParamsArrayEqual(["foo", "bar"], ["bar", "foo"])).toBe(false);
});

describe("isMultipleParam", () => {
test("returns true if the value is a well-formed route multiple param", () => {
expect(isMultipleParam(":test[]")).toBe(true);
expect(isMultipleParam(":[]")).toBe(true);
expect(isMultipleParam(": []")).toBe(true);
test("extractParamNameUnion", () => {
expect(extractParamNameUnion("foo")).toStrictEqual({ name: "foo" });
expect(extractParamNameUnion("foo{")).toStrictEqual({ name: "foo{" });
expect(extractParamNameUnion("foo}")).toStrictEqual({ name: "foo}" });
expect(extractParamNameUnion("{}foo")).toStrictEqual({ name: "{}foo" });

expect(extractParamNameUnion("foo{}")).toStrictEqual({
name: "foo",
union: [],
});

expect(extractParamNameUnion("foo{a}")).toStrictEqual({
name: "foo",
union: ["a"],
});

test("returns false if the value is not a well-formed route multiple param", () => {
expect(isMultipleParam(" :test[]")).toBe(false);
expect(isMultipleParam("test[]")).toBe(false);
expect(isMultipleParam(":test]")).toBe(false);
expect(isMultipleParam(":test[")).toBe(false);
expect(isMultipleParam(": ")).toBe(false);
expect(isMultipleParam(":")).toBe(false);
expect(isMultipleParam(":test")).toBe(false);
expect(isMultipleParam("test")).toBe(false);
expect(extractParamNameUnion("foo{a|b}")).toStrictEqual({
name: "foo",
union: ["a", "b"],
});
});
Loading

0 comments on commit 8fd358b

Please sign in to comment.