Skip to content

Commit

Permalink
chore: upgrade eslint with fixed rule and lint files
Browse files Browse the repository at this point in the history
  • Loading branch information
lukemorales committed Feb 2, 2024
1 parent 635f8bf commit e9d1809
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 214 deletions.
Binary file modified bun.lockb
Binary file not shown.
1 change: 1 addition & 0 deletions src/convert-url-search-params-to-object.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { ReadonlyURLSearchParams } from 'next/navigation';

import { convertURLSearchParamsToObject } from './convert-url-search-params-to-object';

describe('convertURLSearchParamsToObject', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/convert-url-search-params-to-object.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ReadonlyURLSearchParams } from 'next/navigation';
import { type ReadonlyURLSearchParams } from 'next/navigation';

export function convertURLSearchParamsToObject(
params: ReadonlyURLSearchParams | null,
Expand Down
225 changes: 33 additions & 192 deletions src/create-navigation-config.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { renderHook } from '@testing-library/react';
import { useParams, useSearchParams } from 'next/navigation';

import { renderHook } from '@testing-library/react';
import type { Mock } from 'vitest';
import { z } from 'zod';

import { createNavigationConfig } from './create-navigation-config';
import { suppressConsoleErrors } from './test-utils';

Expand All @@ -27,9 +29,9 @@ describe('createNavigationConfig', () => {
});

it('does not expose methods to validate params or searchParams', () => {
//@ts-expect-error about should not have $parseParams
// @ts-expect-error about should not have $parseParams
expect(routes.about.$parseParams).toBeUndefined();
//@ts-expect-error about should not have $parseSearchParams
// @ts-expect-error about should not have $parseSearchParams
expect(routes.about.$parseSearchParams).toBeUndefined();
});
});
Expand All @@ -55,14 +57,14 @@ describe('createNavigationConfig', () => {
});

it('exposes method to validate only searchParams', () => {
//@ts-expect-error team should not have $parseParams
// @ts-expect-error team should not have $parseParams
expect(routes.team.$parseParams).toBeUndefined();

expect(routes.team.$parseSearchParams).toBeDefined();

expect(() =>
routes.team.$parseSearchParams(INVALID_PARAM),
).toThrowError('Invalid search params for route "team"');
expect(() => routes.team.$parseSearchParams(INVALID_PARAM)).toThrow(
'Invalid search params for route "team"',
);
expect(() =>
routes.team.$parseSearchParams({ q: 'john doe', order: 'asc' }),
).not.toThrow();
Expand Down Expand Up @@ -91,14 +93,14 @@ describe('createNavigationConfig', () => {
});

it('exposes method to validate only searchParams', () => {
//@ts-expect-error team should not have $parseParams
// @ts-expect-error team should not have $parseParams
expect(routes.team.$parseParams).toBeUndefined();

expect(routes.team.$parseSearchParams).toBeDefined();

expect(() =>
routes.team.$parseSearchParams(INVALID_PARAM),
).toThrowError('Invalid search params for route "team"');
expect(() => routes.team.$parseSearchParams(INVALID_PARAM)).toThrow(
'Invalid search params for route "team"',
);
expect(() =>
routes.team.$parseSearchParams({ q: 'john doe', order: 'asc' }),
).not.toThrow();
Expand Down Expand Up @@ -132,12 +134,12 @@ describe('createNavigationConfig', () => {
it('exposes method to validate only params', () => {
expect(routes.organization.$parseParams).toBeDefined();

//@ts-expect-error team should not have $parseSearchParams
// @ts-expect-error team should not have $parseSearchParams
expect(routes.organization.$parseSearchParams).toBeUndefined();

expect(() =>
routes.organization.$parseParams(INVALID_PARAM),
).toThrowError('Invalid route params for route "organization"');
expect(() => routes.organization.$parseParams(INVALID_PARAM)).toThrow(
'Invalid route params for route "organization"',
);
expect(() =>
routes.organization.$parseParams({ orgId: 'org_123' }),
).not.toThrow();
Expand Down Expand Up @@ -173,12 +175,12 @@ describe('createNavigationConfig', () => {
it('exposes method to validate only params', () => {
expect(routes.organizationUser.$parseParams).toBeDefined();

//@ts-expect-error team should not have $parseSearchParams
// @ts-expect-error team should not have $parseSearchParams
expect(routes.organizationUser.$parseSearchParams).toBeUndefined();

expect(() =>
routes.organizationUser.$parseParams(INVALID_PARAM),
).toThrowError('Invalid route params for route "organizationUser"');
).toThrow('Invalid route params for route "organizationUser"');
expect(() =>
routes.organizationUser.$parseParams({
orgId: 'org_123',
Expand Down Expand Up @@ -370,22 +372,22 @@ describe('createNavigationConfig', () => {
useParamsMock.mockClear();
});

it('throws when called with a route name that does not have route parameters ', () => {
it('throws when called with a route name that does not have route parameters', () => {
suppressConsoleErrors(() => {
expect(() =>
//@ts-expect-error teams should not be a valid route
// @ts-expect-error teams should not be a valid route
renderHook(() => useSafeParams('team')),
).toThrowError('Route "team" does not have params validation');
).toThrow('Route "team" does not have params validation');
});
});

it('throws when called with bad route parameters ', () => {
it('throws when called with bad route parameters', () => {
suppressConsoleErrors(() => {
useParamsMock.mockImplementation(() => ({ orgId: 'invalid-org-id' }));

expect(() =>
renderHook(() => useSafeParams('organization')),
).toThrowError('Invalid route params for route "organization"');
expect(() => renderHook(() => useSafeParams('organization'))).toThrow(
'Invalid route params for route "organization"',
);
});
});

Expand Down Expand Up @@ -444,24 +446,24 @@ describe('createNavigationConfig', () => {
useSearchParamsMock.mockClear();
});

it('throws when called with a route name that does not have searchParams ', () => {
it('throws when called with a route name that does not have searchParams', () => {
suppressConsoleErrors(() => {
expect(() =>
//@ts-expect-error about should not be a valid route
// @ts-expect-error about should not be a valid route
renderHook(() => useSafeSearchParams('about')),
).toThrowError('Route "about" does not have searchParams validation');
).toThrow('Route "about" does not have searchParams validation');
});
});

it('throws when called with bad searchParams ', () => {
it('throws when called with bad searchParams', () => {
suppressConsoleErrors(() => {
useSearchParamsMock.mockImplementation(
() => new URLSearchParams('q=john+doe&order=invalid'),
);

expect(() =>
renderHook(() => useSafeSearchParams('team')),
).toThrowError('Invalid search params for route "team"');
expect(() => renderHook(() => useSafeSearchParams('team'))).toThrow(
'Invalid search params for route "team"',
);
});
});

Expand Down Expand Up @@ -509,165 +511,4 @@ describe('createNavigationConfig', () => {
});
});
});

describe.skip('full test suite', () => {
function makeTestConfig() {
return createNavigationConfig((defineRoute) => ({
team: defineRoute('/team'),
teamMembers: defineRoute('/team/members', {
search: z
.object({
q: z.string().optional(),
order: z.enum(['asc', 'desc']),
})
.optional(),
}),
organizations: defineRoute('/organizations', {
search: z.object({
q: z.string().optional(),
order: z.enum(['asc', 'desc']).optional(),
}),
}),
organization: defineRoute('/organizations/[orgId]', {
params: z.object({
orgId: z.coerce.number(),
}),
}),
organizationUsers: defineRoute('/organizations/[orgId]/users', {
params: z.object({
orgId: z.coerce.number(),
}),
search: z.object({
q: z.string().optional(),
order: z.enum(['asc', 'desc']).optional(),
}),
}),
}));
}

it('creates a safe context for navigating', () => {
const { routes } = makeTestConfig();

expectTypeOf(routes.team).toEqualTypeOf<() => string>();
expect(routes.team()).toBe('/team');

type ExpectedTeamMembersType = (options: {
search: { q?: string; order: 'asc' | 'desc' };
}) => string;

expectTypeOf(routes.teamMembers).toMatchTypeOf<ExpectedTeamMembersType>();
expect(routes.teamMembers({ search: { order: 'asc' } })).toBe(
'/team/members?order=asc',
);

type ExpectedOrganizationsType = (options: {
search: { q?: string; order?: 'asc' | 'desc' };
}) => string;

expectTypeOf(
routes.organizations,
).toMatchTypeOf<ExpectedOrganizationsType>();

// @ts-expect-error organizations search is required
expect(routes.organizations()).toBe('/organizations');
expect(
routes.organizations({ search: { q: 'foo corp', order: 'desc' } }),
).toBe('/organizations?q=foo+corp&order=desc');

expect(routes.organization({ orgId: 1 })).toBe('/organizations/1');

type ExpectedOrganizationUsersType = (options: {
orgId: number;
search: { q?: string; order?: 'asc' | 'desc' };
}) => string;

expectTypeOf(
routes.organizationUsers,
).toMatchTypeOf<ExpectedOrganizationUsersType>();

// @ts-expect-error organizationUsers search is required
expect(routes.organizationUsers({ orgId: 1 })).toBe(
'/organizations/1/users',
);
expect(
routes.organizationUsers({
orgId: 1,
search: { q: 'john doe', order: 'asc' },
}),
).toBe('/organizations/1/users?q=john+doe&order=asc');
});

it('exposes methods to validade params and searchParams', () => {
const { routes } = makeTestConfig();
{
//@ts-expect-error team should not have $parseParams
routes.team.$parseParams;
//@ts-expect-error team should not have $parseSearchParams
routes.team.$parseSearchParams;
}

{
//@ts-expect-error teamMembers should not have $parseParams
routes.teamMembers.$parseParams;

expect(() =>
routes.teamMembers.$parseSearchParams(INVALID_PARAM),
).toThrowError('Invalid search params for route "teamMembers"');

expect(() =>
routes.teamMembers.$parseSearchParams({
q: 'john doe',
order: 'asc',
}),
).not.toThrow();
}

{
//@ts-expect-error organizations should not have $parseParams
routes.organizations.$parseParams;

expect(() =>
routes.organizations.$parseSearchParams(INVALID_PARAM),
).toThrowError('Invalid search params for route "organizations"');

expect(() =>
routes.organizations.$parseSearchParams({
q: 'john doe',
order: 'asc',
}),
).not.toThrow();
}

{
expect(() =>
routes.organization.$parseParams(INVALID_PARAM),
).toThrowError('Invalid route params for route "organization"');
expect(() =>
routes.organization.$parseParams({ orgId: '1' }),
).not.toThrow();

//@ts-expect-error organization should not have $parseParams
routes.organization.$parseSearchParams;
}

{
expect(() =>
routes.organizationUsers.$parseParams(INVALID_PARAM),
).toThrowError('Invalid route params for route "organizationUsers"');
expect(() =>
routes.organizationUsers.$parseParams({ orgId: 1 }),
).not.toThrow();

expect(() =>
routes.organizationUsers.$parseSearchParams(INVALID_PARAM),
).toThrowError('Invalid search params for route "organizationUsers"');
expect(() =>
routes.organizationUsers.$parseSearchParams({
q: 'john doe',
order: 'asc',
}),
).not.toThrow();
}
});
});
});
6 changes: 4 additions & 2 deletions src/create-navigation-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import {
useParams as useNextParams,
useSearchParams as useNextSearchParams,
} from 'next/navigation';
import { z } from 'zod';

import { type z } from 'zod';

import { convertURLSearchParamsToObject } from './convert-url-search-params-to-object';
import { makeRouteBuilder, type RouteBuilder } from './make-route-builder';
import type { Prettify } from './types';
Expand Down Expand Up @@ -152,7 +154,7 @@ export function createNavigationConfig<Config extends NavigationConfig>(
for (const [route, builder] of Object.entries(navigationConfig)) {
const schemas = builder.getSchemas();

//@ts-expect-error overwriting runtime implementation
// @ts-expect-error overwriting runtime implementation
builder.getSchemas = undefined;

if (schemas.params != null || schemas.search != null) {
Expand Down
Loading

0 comments on commit e9d1809

Please sign in to comment.