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 tests for useRouteFinder and rename it to useVersionSwitcherPath #6794

Merged
merged 10 commits into from
Jan 25, 2024
Prev Previous commit
Next Next commit
Fix typo
  • Loading branch information
timngyn committed Jan 20, 2024
commit 289855ac223e1b897851cdc40fc8e3e5073a5011
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { renderHook } from '@testing-library/react';
import { useVersionSwitchPath } from '../useVersionSwitchPath';
import { useVersionSwitcherPath } from '../useVersionSwitcherPath';

const routerMock = {
__esModule: true,
Expand All @@ -16,7 +16,7 @@ const flatDirectoryMock = {};

jest.mock('@/directory/flatDirectory.json', () => flatDirectoryMock);

describe('useVersionSwitchPath', () => {
describe('useVersionSwitcherPath', () => {
it('should replace "/[platform]" with "/[platform]/prev" when isPrev is false', () => {
routerMock.useRouter = () => {
return {
Expand All @@ -30,7 +30,7 @@ describe('useVersionSwitchPath', () => {
route: '/[platform]/prev/build-a-backend/auth/set-up-auth'
};

const { result } = renderHook(() => useVersionSwitchPath('react', false));
const { result } = renderHook(() => useVersionSwitcherPath('react', false));

expect(result.current).toEqual(
'/[platform]/prev/build-a-backend/auth/set-up-auth'
Expand All @@ -50,7 +50,7 @@ describe('useVersionSwitchPath', () => {
route: '/[platform]/build-a-backend/auth/set-up-auth'
};

const { result } = renderHook(() => useVersionSwitchPath('react', true));
const { result } = renderHook(() => useVersionSwitcherPath('react', true));

expect(result.current).toEqual(
'/[platform]/build-a-backend/auth/set-up-auth'
Expand All @@ -70,7 +70,9 @@ describe('useVersionSwitchPath', () => {
route: '/[platform]/build-a-backend/auth/set-up-auth'
};

const { result } = renderHook(() => useVersionSwitchPath('angular', true));
const { result } = renderHook(() =>
useVersionSwitcherPath('angular', true)
);

expect(result.current).toEqual(undefined);
});
Expand Down
4 changes: 2 additions & 2 deletions src/components/VersionSwitcher/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import { IconCheck } from '@/components/Icons';
import { PLATFORM_VERSIONS } from '@/data/platforms';
import classNames from 'classnames';
import { trackVersionChange } from '@/utils/track';
import { useVersionSwitchPath } from './useVersionSwitchPath';
import { useVersionSwitcherPath } from './useVersionSwitcherPath';
import { BUILD_A_BACKEND, PREV_BUILD_A_BACKEND } from '@/data/routes';

export const VersionSwitcher = ({ platform, isPrev, ...rest }) => {
const router = useRouter();
const pathname = router.pathname;
const versions = PLATFORM_VERSIONS[platform];
const switchPath = useVersionSwitchPath(platform, isPrev);
const switchPath = useVersionSwitcherPath(platform, isPrev);
let path = isPrev ? BUILD_A_BACKEND : PREV_BUILD_A_BACKEND;
if (
switchPath &&
Expand Down
Copy link
Contributor

Choose a reason for hiding this comment

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

Thank you for adding the comments and simplifying the params so they're less confusing for future us 🙏

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import flatDirectory from '@/directory/flatDirectory.json';
import { useRouter } from 'next/router';
import { Platform } from '@/data/platforms';

export const useVersionSwitchPath = (platform: Platform, isPrev: boolean) => {
export const useVersionSwitcherPath = (platform: Platform, isPrev: boolean) => {
const router = useRouter();
const path = router.pathname;
const newRoute = isPrev
Expand Down
Loading