New useNavigateApp (#9729)

Todo : 
- replace all instances of useNavigate(
- remove getSettingsPagePath
- add eslint rule to enfore usage of useNavigateApp instead of
useNavigate
This commit is contained in:
Félix Malfait
2025-01-18 13:58:12 +01:00
committed by GitHub
parent 8572471973
commit 152902d1be
115 changed files with 975 additions and 679 deletions

View File

@ -0,0 +1,72 @@
import { renderHook } from '@testing-library/react';
import { MemoryRouter, useNavigate } from 'react-router-dom';
import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular';
import { AppPath } from '@/types/AppPath';
import { useNavigateApp } from '~/hooks/useNavigateApp';
jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useNavigate: jest.fn(),
}));
const Wrapper = ({ children }: { children: React.ReactNode }) => (
<MemoryRouter>{children}</MemoryRouter>
);
describe('useNavigateApp', () => {
const mockNavigate = jest.fn();
beforeEach(() => {
jest.clearAllMocks();
(useNavigate as jest.Mock).mockReturnValue(mockNavigate);
});
it('should navigate to the correct path without params', () => {
const { result } = renderHook(() => useNavigateApp(), {
wrapper: Wrapper,
});
result.current(AppPath.Index);
expect(mockNavigate).toHaveBeenCalledWith('/', undefined);
});
it('should navigate to the correct path with params', () => {
const { result } = renderHook(() => useNavigateApp(), {
wrapper: Wrapper,
});
result.current(AppPath.RecordShowPage, {
objectNameSingular: CoreObjectNameSingular.Company,
objectRecordId: '123',
});
expect(mockNavigate).toHaveBeenCalledWith('/object/company/123', undefined);
});
it('should navigate with query params', () => {
const { result } = renderHook(() => useNavigateApp(), {
wrapper: Wrapper,
});
const queryParams = { viewId: '123', filter: 'test' };
result.current(AppPath.Index, undefined, queryParams);
expect(mockNavigate).toHaveBeenCalledWith(
'/?viewId=123&filter=test',
undefined,
);
});
it('should navigate with options', () => {
const { result } = renderHook(() => useNavigateApp(), {
wrapper: Wrapper,
});
const options = { replace: true, state: { test: true } };
result.current(AppPath.Index, undefined, undefined, options);
expect(mockNavigate).toHaveBeenCalledWith('/', options);
});
});

View File

@ -0,0 +1,74 @@
import { renderHook } from '@testing-library/react';
import { MemoryRouter, useNavigate } from 'react-router-dom';
import { SettingsPath } from '@/types/SettingsPath';
import { useNavigateSettings } from '~/hooks/useNavigateSettings';
jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useNavigate: jest.fn(),
}));
const Wrapper = ({ children }: { children: React.ReactNode }) => (
<MemoryRouter>{children}</MemoryRouter>
);
describe('useNavigateSettings', () => {
const mockNavigate = jest.fn();
beforeEach(() => {
jest.clearAllMocks();
(useNavigate as jest.Mock).mockReturnValue(mockNavigate);
});
it('should navigate to the correct settings path without params', () => {
const { result } = renderHook(() => useNavigateSettings(), {
wrapper: Wrapper,
});
result.current(SettingsPath.Accounts);
expect(mockNavigate).toHaveBeenCalledWith('/settings/accounts', undefined);
});
it('should navigate to the correct settings path with params', () => {
const { result } = renderHook(() => useNavigateSettings(), {
wrapper: Wrapper,
});
result.current(SettingsPath.ObjectFieldEdit, {
objectNamePlural: 'companies',
fieldName: 'name',
});
expect(mockNavigate).toHaveBeenCalledWith(
'/settings/objects/companies/name',
undefined,
);
});
it('should navigate with query params', () => {
const { result } = renderHook(() => useNavigateSettings(), {
wrapper: Wrapper,
});
const queryParams = { viewId: '123', filter: 'test' };
result.current(SettingsPath.Accounts, undefined, queryParams);
expect(mockNavigate).toHaveBeenCalledWith(
'/settings/accounts?viewId=123&filter=test',
undefined,
);
});
it('should navigate with options', () => {
const { result } = renderHook(() => useNavigateSettings(), {
wrapper: Wrapper,
});
const options = { replace: true, state: { test: true } };
result.current(SettingsPath.Accounts, undefined, undefined, options);
expect(mockNavigate).toHaveBeenCalledWith('/settings/accounts', options);
});
});