-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add typings tests and fix a bug with route constructor params
- Loading branch information
Showing
7 changed files
with
779 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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({}, {})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"], | ||
}; |
Oops, something went wrong.