Skip to content

Commit

Permalink
Merge pull request #5 from ckastbjerg/add-basic-tests
Browse files Browse the repository at this point in the history
Add basic tests
  • Loading branch information
ckastbjerg authored Mar 21, 2021
2 parents c892eb8 + db105f5 commit b484f1e
Show file tree
Hide file tree
Showing 17 changed files with 35 additions and 7 deletions.
2 changes: 1 addition & 1 deletion example/src/@types/next-type-safe-routes/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint no-use-before-define: 0 */ // --> OFF
// prettier-ignore

// IMPORTANT! This file is autogenerated by the `type-safe-next-pages`
// IMPORTANT! This file is autogenerated by the `type-safe-next-routes`
// package. You should _not_ update these types manually...

declare module "next-type-safe-routes" {
Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
{
"name": "next-type-safe-routes",
"version": "0.0.19-alpha",
"version": "0.0.20-alpha",
"description": "Never should your users experience broken links again!",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"bin": "bin/cli.js",
"files": [
"dist",
"bin"
],
"scripts": {
"build": "tsc",
"prepare": "yarn build",
"test": "yarn jest",
"test": "yarn jest --updateSnapshot",
"lint": "eslint '*/**/*.{js,ts,tsx}' --quiet --fix"
},
"husky": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`plugin/generateTypeScriptFile works as expected 1`] = `
"/* eslint no-use-before-define: 0 */ // --> OFF
// prettier-ignore
// IMPORTANT! This file is autogenerated by the \`type-safe-next-routes\`
// package. You should _not_ update these types manually...
declare module \\"next-type-safe-routes\\" {
export type TypeSafePage = \\"/404\\" | \\"/\\" | { route: \\"/users/[userId]\\", userId: string | string[] | number } | \\"/users\\";
export type TypeSafeApiRoute = { route: \\"/api/[authId]\\", authId: string | string[] | number } | { route: \\"/api/users/[userId]\\", userId: string | string[] | number } | \\"/api/users\\";
export const getPathname = (typeSafeUrl: TypeSafePage | TypeSafeApiRoute) => string;
export const getRoute = (typeSafeUrl: TypeSafePage | TypeSafeApiRoute, query?: any) => string;
}
"
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import generateTypeScriptFile from "./index";

const pagesDir = __dirname + "/mocks/pages";

describe("plugin/generateTypeScriptFile", () => {
it("works as expected", () => {
expect(generateTypeScriptFile(pagesDir)).toMatchSnapshot();
});
});
2 changes: 1 addition & 1 deletion src/plugin/generateTypeScriptFile/getApiRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import getNextPageRoute from "./getNextPageRoute";
import getNextRouteUrlParams from "./getNextRouteUrlParams";
import { ApiRoute } from "./types";

const shouldIncludeEntry = (endpoint: string) => !endpoint.match("README");
const shouldIncludeEntry = (endpoint: string) => endpoint.match(".ts");

const getApiRoutes = (fileNames: string[]): ApiRoute[] => {
return fileNames.filter(shouldIncludeEntry).map((fileName) => {
Expand Down
2 changes: 1 addition & 1 deletion src/plugin/generateTypeScriptFile/getFileContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const getFileContent = ({
}: Args) => `/* eslint no-use-before-define: 0 */ // --> OFF
// prettier-ignore
// IMPORTANT! This file is autogenerated by the \`type-safe-next-pages\`
// IMPORTANT! This file is autogenerated by the \`type-safe-next-routes\`
// package. You should _not_ update these types manually...
declare module "next-type-safe-routes" {
Expand Down
4 changes: 3 additions & 1 deletion src/plugin/generateTypeScriptFile/getPages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import getNextPageRoute from "./getNextPageRoute";
import getNextRouteUrlParams from "./getNextRouteUrlParams";
import { Page } from "./types";

const ignoreRoutes = ["/_app.tsx", "/_document.tsx"];

const shouldIncludeEntry = (route: string) =>
!route.startsWith("/_") && !route.match(".md");
route.match(".tsx") && !ignoreRoutes.includes(route);

const getPages = (fileNames: string[]): Page[] => {
return fileNames.filter(shouldIncludeEntry).map((fileName) => {
Expand Down
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.

0 comments on commit b484f1e

Please sign in to comment.