fix(settings routing): handle trailing slashes in base paths (#10055)
Adjusted URL construction to properly handle trailing slashes in base paths, ensuring consistent matching logic. Added logic for setting the hotkey scope when navigating to the domain settings path.
This commit is contained in:
@ -1,39 +1,74 @@
|
||||
import { MemoryRouter } from 'react-router-dom';
|
||||
import { renderHook } from '@testing-library/react';
|
||||
import { useIsMatchingLocation } from '../useIsMatchingLocation';
|
||||
import { MemoryRouter } from 'react-router-dom';
|
||||
import { AppBasePath } from '@/types/AppBasePath';
|
||||
|
||||
import { useIsMatchingLocation } from '~/hooks/useIsMatchingLocation';
|
||||
|
||||
const Wrapper = ({ children }: { children: React.ReactNode }) => (
|
||||
<MemoryRouter
|
||||
initialEntries={['/one', '/two', { pathname: '/three' }]}
|
||||
initialIndex={1}
|
||||
>
|
||||
{children}
|
||||
</MemoryRouter>
|
||||
);
|
||||
const Wrapper =
|
||||
(initialIndex = 0) =>
|
||||
({ children }: { children: React.ReactNode }) => (
|
||||
<MemoryRouter
|
||||
initialEntries={['/example', '/other', `${AppBasePath.Settings}/example`]}
|
||||
initialIndex={initialIndex}
|
||||
>
|
||||
{children}
|
||||
</MemoryRouter>
|
||||
);
|
||||
|
||||
describe('useIsMatchingLocation', () => {
|
||||
it('should return true for a matching location', () => {
|
||||
const { result } = renderHook(
|
||||
() => {
|
||||
const checkMatchingLocation = useIsMatchingLocation();
|
||||
return checkMatchingLocation('/two');
|
||||
},
|
||||
{ wrapper: Wrapper },
|
||||
);
|
||||
it('returns true when paths match with no basePath', () => {
|
||||
const { result } = renderHook(() => useIsMatchingLocation(), {
|
||||
wrapper: Wrapper(),
|
||||
});
|
||||
|
||||
expect(result.current).toBe(true);
|
||||
expect(result.current.isMatchingLocation('/example')).toBe(true);
|
||||
});
|
||||
|
||||
it('should return false for a non-matching location', () => {
|
||||
const { result } = renderHook(
|
||||
() => {
|
||||
const checkMatchingLocation = useIsMatchingLocation();
|
||||
return checkMatchingLocation('/four');
|
||||
},
|
||||
{ wrapper: Wrapper },
|
||||
);
|
||||
it('returns false when paths do not match with no basePath', () => {
|
||||
const { result } = renderHook(() => useIsMatchingLocation(), {
|
||||
wrapper: Wrapper(),
|
||||
});
|
||||
|
||||
expect(result.current).toBe(false);
|
||||
expect(result.current.isMatchingLocation('/non-match')).toBe(false);
|
||||
});
|
||||
|
||||
it('returns true when paths match with basePath', () => {
|
||||
const { result } = renderHook(() => useIsMatchingLocation(), {
|
||||
wrapper: Wrapper(2),
|
||||
});
|
||||
|
||||
expect(
|
||||
result.current.isMatchingLocation('example', AppBasePath.Settings),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it('returns false when paths do not match with basePath', () => {
|
||||
const { result } = renderHook(() => useIsMatchingLocation(), {
|
||||
wrapper: Wrapper(),
|
||||
});
|
||||
|
||||
expect(
|
||||
result.current.isMatchingLocation('non-match', AppBasePath.Settings),
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
it('handles trailing slashes in basePath correctly', () => {
|
||||
const { result } = renderHook(() => useIsMatchingLocation(), {
|
||||
wrapper: Wrapper(2),
|
||||
});
|
||||
|
||||
expect(
|
||||
result.current.isMatchingLocation(
|
||||
'example',
|
||||
(AppBasePath.Settings + '/') as AppBasePath,
|
||||
),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it('handles without basePath correctly', () => {
|
||||
const { result } = renderHook(() => useIsMatchingLocation(), {
|
||||
wrapper: Wrapper(),
|
||||
});
|
||||
|
||||
expect(result.current.isMatchingLocation('example')).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
@ -30,9 +30,9 @@ jest.mock('~/hooks/useIsMatchingLocation');
|
||||
const mockUseIsMatchingLocation = jest.mocked(useIsMatchingLocation);
|
||||
|
||||
const setupMockIsMatchingLocation = (pathname: string) => {
|
||||
mockUseIsMatchingLocation.mockReturnValueOnce(
|
||||
(path: string) => path === pathname,
|
||||
);
|
||||
mockUseIsMatchingLocation.mockReturnValueOnce({
|
||||
isMatchingLocation: (path: string) => path === pathname,
|
||||
});
|
||||
};
|
||||
|
||||
jest.mock('@/auth/hooks/useIsLogged');
|
||||
|
||||
Reference in New Issue
Block a user