Skip to content

Commit

Permalink
chore: useGoToResourceBatchUpdatePage tests HAUKI-672
Browse files Browse the repository at this point in the history
  • Loading branch information
mikkojamG committed Dec 13, 2024
1 parent eca8098 commit 1425514
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions src/hooks/useGoToResourceBatchUpdatePage.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import React from 'react';
import { renderHook } from '@testing-library/react-hooks';
import { MemoryRouter, Route, Routes } from 'react-router-dom';
import * as routerMock from 'react-router-dom';
import useGoToResourceBatchUpdatePage from './useGoToResourceBatchUpdatePage';

const mockNavigate = vi.fn();

vi.mock('react-router-dom', async () => {
const mod = await vi.importActual('react-router-dom');

return {
...mod,
useParams: vi.fn(),
useNavigate: () => mockNavigate,
};
});

describe('useGoToResourceBatchUpdatePage', () => {
it('should navigate to the correct batch update page when parentId and resourceId are provided', () => {
vi.spyOn(routerMock, 'useParams').mockReturnValueOnce({
parentId: 'parent1',
id: 'resource1',
});

const { result } = renderHook(() => useGoToResourceBatchUpdatePage(), {
wrapper: ({ children }) => (
<MemoryRouter initialEntries={['/somepath']}>
<Routes>
<Route path="*" element={children} />
</Routes>
</MemoryRouter>
),
});

result.current();

expect(mockNavigate).toHaveBeenCalledWith(
'/resource/parent1/child/resource1/batch'
);
});

it('should navigate to the correct batch update page when only resourceId is provided', () => {
vi.spyOn(routerMock, 'useParams').mockReturnValueOnce({ id: 'resource1' });

const { result } = renderHook(() => useGoToResourceBatchUpdatePage(), {
wrapper: ({ children }) => (
<MemoryRouter initialEntries={['/somepath']}>
<Routes>
<Route path="*" element={children} />
</Routes>
</MemoryRouter>
),
});

result.current();

expect(mockNavigate).toHaveBeenCalledWith('/resource/resource1/batch');
});

it('should throw an error when neither parentId nor resourceId is provided', () => {
vi.spyOn(routerMock, 'useParams').mockReturnValueOnce({});

const { result } = renderHook(() => useGoToResourceBatchUpdatePage(), {
wrapper: ({ children }) => (
<MemoryRouter initialEntries={['/somepath']}>
<Routes>
<Route path="*" element={children} />
</Routes>
</MemoryRouter>
),
});

expect(result.current).toThrowError(
'Invalid route. No resource id found from the path.'
);
});
});

0 comments on commit 1425514

Please sign in to comment.