Skip to content

Commit

Permalink
Add typings tests and fix a bug with route constructor params
Browse files Browse the repository at this point in the history
  • Loading branch information
mchudy committed Sep 16, 2020
1 parent 2941a1f commit 4efa2da
Show file tree
Hide file tree
Showing 7 changed files with 779 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,7 @@ jobs:
run: npm run lint
- name: Run tests
run: npm test
- name: Test types
run: npm run test:types
- name: Build
run: npm run build
2 changes: 1 addition & 1 deletion __tests__/routing.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createRouting, number, QueryParamsFor, segment } from "../src/index";
import { createRouting, number, segment } from "../src/index";

describe("createRouting", () => {
it("creates a simple route", () => {
Expand Down
57 changes: 57 additions & 0 deletions __tests__/types/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { createRouting, number, segment, query } from "../../lib/index";
import { expectError, expectType } from "tsd";

const routes = createRouting({
user: segment`/users/${number("userId")}`,
products: {
...segment`/products${query({ filter: true, optionalFilter: false })}`,
children: {
product: segment`/${number("productId")}`,
},
},
order: segment`/orders/${number("orderId", true)}`,
} as const);

// Passes when the required param is specified
expectType<string>(routes.user({ userId: "12" }));

// Expects the required param to be passed
expectError(routes.user());
expectError(routes.user({}));

// Does not allow undefined query params
expectError(routes.user({ userId: "12" }, { filter: "a" }));

// Does not allow other params than the productId
expectError(routes.user({ otherId: "12" }));

// Does not allow any extra params that are not defined
expectError(routes.user({ userId: "12", otherId: "12" }));

// Passes when only required query params are specified
expectType<string>(routes.products({}, { filter: "some" }));

// Passes with both required and optional query params specified
expectType<string>(routes.products({}, { filter: "some", optionalFilter: "2" }));

// Expects the required query param to be passed
expectError(routes.products({}, {}));

// Does not allow undefined route params
expectError(routes.products({ otherId: "121212" }, { filter: "some" }));

// Does not allow undefined query params
expectError(routes.products({}, { filter: "some", a: "a" }));

// Passes when all required params from parent routes are specified
expectType<string>(routes.products.product({ productId: "12" }, { filter: "a" }));

// Expects parent query params in child routes
expectError(routes.products.product({ productId: "12" }));

// Passes when optional route params are not specified
expectType<string>(routes.order());
expectType<string>(routes.order({}));

// Does not allow query argument when no query params are defined
expectError(routes.order({}, {}));
7 changes: 4 additions & 3 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
moduleDirectories: ['node_modules', 'src'],
preset: "ts-jest",
testEnvironment: "node",
moduleDirectories: ["node_modules", "src"],
testPathIgnorePatterns: ["__tests__/types"],
};
Loading

0 comments on commit 4efa2da

Please sign in to comment.