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

Fix Param types #1058

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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 package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"release": "4c release --conventional-commits",
"tdd": "jest --watch",
"test": "yarn lint && yarn test:ts && yarn testonly -- --coverage",
"test:ts": "dtslint --expectOnly types && yarn tsc --noEmit",
"test:ts": "dtslint --expectOnly types && yarn tsc --noEmit && yarn tsc --noEmit -p test",
"testonly": "jest --runInBand --verbose",
"docs": "yarn --cwd www start"
},
Expand Down
40 changes: 29 additions & 11 deletions test/Matcher.test.js → test/Matcher.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import Matcher from '../src/Matcher';
import {
type IsActiveOptions,
type LocationDescriptorObject,
Match,
} from '../src/typeUtils';

describe('Matcher', () => {
describe('route hierarchies', () => {
Expand Down Expand Up @@ -32,11 +37,11 @@ describe('Matcher', () => {
['nested matching', '/foo/bar', [0, 1]],
['route fallthrough', '/foo/baz', [2]],
].forEach(([scenario, pathname, expectedRouteIndices]) => {
describe(scenario, () => {
describe(scenario as string, () => {
it('should be supported', () => {
expect(
matcher.match({
pathname,
pathname: pathname as string,
}),
).toMatchObject({
routeIndices: expectedRouteIndices,
Expand Down Expand Up @@ -215,11 +220,15 @@ describe('Matcher', () => {
},
]);

expect(
matcher.match({
pathname: '/a',
}),
).toEqual({
const missingMatch = matcher.match({ pathname: '/a' });

if (missingMatch != null && 0 in missingMatch.params) {
// FIXME: This type is wrong. It should be string | undefined.
const param: string = missingMatch.params[0];
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shows what the problem was. The type is string but it is actually undefined.

expect(param).toBeUndefined();
}

expect(missingMatch).toEqual({
routeIndices: [0, 0],
routeParams: [{ foo: 'a' }, { 0: undefined }],
params: {
Expand Down Expand Up @@ -265,7 +274,7 @@ describe('Matcher', () => {
it(`should match ${scenario}`, () => {
expect(
matcher.match({
pathname,
pathname: pathname as string,
}),
).toMatchObject({
routeIndices: expectedRouteIndices,
Expand Down Expand Up @@ -350,7 +359,7 @@ describe('Matcher', () => {
});

describe('#joinPaths', () => {
const matcher = new Matcher();
const matcher = new Matcher([]);

[
['no extra slashes', '/foo', 'bar'],
Expand All @@ -359,6 +368,7 @@ describe('Matcher', () => {
['slashes everywhere', '/foo/', '/bar'],
].forEach(([scenario, basePath, path]) => {
it(`should support ${scenario}`, () => {
// @ts-ignore
expect(matcher.joinPaths(basePath, path)).toBe('/foo/bar');
});
});
Expand Down Expand Up @@ -415,7 +425,11 @@ describe('Matcher', () => {
].forEach(([scenario, matchLocation, location, options]) => {
it(`should be active on ${scenario}`, () => {
expect(
matcher.isActive({ location: matchLocation }, location, options),
matcher.isActive(
{ location: matchLocation } as any as Match,
location as LocationDescriptorObject,
options as IsActiveOptions,
),
).toBe(true);
});
});
Expand Down Expand Up @@ -459,7 +473,11 @@ describe('Matcher', () => {
].forEach(([scenario, matchLocation, location, options]) => {
it(`should not be active on ${scenario}`, () => {
expect(
matcher.isActive({ location: matchLocation }, location, options),
matcher.isActive(
{ location: matchLocation } as any as Match,
location as LocationDescriptorObject,
options as IsActiveOptions,
),
).toBe(false);
});
});
Expand Down
4 changes: 4 additions & 0 deletions test/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "../tsconfig.json",
"include": ["**/*.ts"]
}