Skip to content

Commit

Permalink
Merge pull request #3 from ckastbjerg/add-basic-tests
Browse files Browse the repository at this point in the history
Setup testing solution + add unit tests for utils
  • Loading branch information
ckastbjerg authored Mar 21, 2021
2 parents f17168d + 6160f24 commit c892eb8
Show file tree
Hide file tree
Showing 8 changed files with 3,407 additions and 147 deletions.
20 changes: 10 additions & 10 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
# Setup .npmrc file to publish to npm
- uses: actions/setup-node@v2
with:
node-version: '14.x'
registry-url: 'https://registry.npmjs.org'
- run: npm install
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
- uses: actions/checkout@v2
# Setup .npmrc file to publish to npm
- uses: actions/setup-node@v2
with:
node-version: "14.x"
registry-url: "https://registry.npmjs.org"
- run: npm install
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
11 changes: 11 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: CI
on: push
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install modules
run: yarn
- name: Run tests
run: yarn test
42 changes: 0 additions & 42 deletions bin/cli.js

This file was deleted.

4 changes: 4 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};
13 changes: 9 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "next-type-safe-routes",
"version": "0.0.18-alpha",
"description": "Ensure that you only link to pages in your Next.js app that actually exists",
"version": "0.0.19-alpha",
"description": "Never should your users experience broken links again!",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"bin": "bin/cli.js",
Expand All @@ -11,7 +11,8 @@
],
"scripts": {
"build": "tsc",
"prepare": "npm run build",
"prepare": "yarn build",
"test": "yarn jest",
"lint": "eslint '*/**/*.{js,ts,tsx}' --quiet --fix"
},
"husky": {
Expand All @@ -30,13 +31,17 @@
"walk-sync": "^2.2.0"
},
"devDependencies": {
"@types/jest": "^26.0.21",
"@typescript-eslint/eslint-plugin": "^4.17.0",
"@typescript-eslint/parser": "^4.17.0",
"commander": "^7.1.0",
"eslint": "^7.22.0",
"eslint-config-prettier": "^8.1.0",
"eslint-plugin-prettier": "^3.3.1",
"prettier": "^2.2.1"
"jest": "^26.6.3",
"prettier": "^2.2.1",
"ts-jest": "^26.5.4",
"typescript": "^4.2.3"
},
"repository": {
"type": "git",
Expand Down
35 changes: 35 additions & 0 deletions src/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { getRoute, getPathname } from "./utils";

describe("utils/getRoute", () => {
it("works as expected for basic routes", () => {
expect(getRoute("/routes")).toBe("/routes");
});

it("works as expected when having (untyped) query params", () => {
expect(getRoute("/routes", { a: "b" })).toBe("/routes?a=b");
});

it("works as expected for dynamic routes", () => {
const route = getRoute({ route: "/routes/[routeId]", routeId: 1 });
expect(route).toBe("/routes/1");
});

it("works as expected when having (untyped) query params for dynamic routes", () => {
const route = getRoute(
{ route: "/routes/[routeId]", routeId: 1 },
{ a: "b" }
);
expect(route).toBe("/routes/1?a=b");
});
});

describe("utils/getPathname", () => {
it("works as expected for basic routes", () => {
expect(getPathname("/routes")).toBe("/routes");
});

it("works as expected for dynamic routes", () => {
const route = getPathname({ route: "/routes/[routeId]", routeId: 1 });
expect(route).toBe("/routes/[routeId]");
});
});
8 changes: 4 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// NOTE, these will be replaced with the "real" TypeSafePage type
// when generating types for a project
type TypeSafePage = string | { route: string };
type TypeSafeApiRoute = string | { route: string };
type TypeSafePage = string | { route: string; routeId: string | number };
type TypeSafeApiRoute = string | { route: string; routeId: string | number };

export const getPathname = (typeSafeUrl: TypeSafePage | TypeSafeApiRoute) => {
if (typeof typeSafeUrl === "string") {
Expand All @@ -24,11 +24,11 @@ export const getRoute = (
typeSafeUrl: TypeSafePage | TypeSafeApiRoute,
query?: any
) => {
const searchParams = getSearchParams(query);
if (typeof typeSafeUrl === "string") {
return typeSafeUrl;
return `${typeSafeUrl}${searchParams}`;
}

const searchParams = getSearchParams(query);
const { route, ...params } = typeSafeUrl;
let href = route as string;
Object.keys(params).forEach((param) => {
Expand Down
Loading

0 comments on commit c892eb8

Please sign in to comment.