Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic tests #5

Merged
merged 7 commits into from
Mar 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.