generated from eea/volto-addon-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(tests): add unit tests for ReportNavigation
- Loading branch information
Showing
1 changed file
with
131 additions
and
0 deletions.
There are no files selected for viewing
131 changes: 131 additions & 0 deletions
131
src/components/manage/Blocks/ContextNavigation/variations/ReportNavigation.test.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
import { render, fireEvent } from '@testing-library/react'; | ||
import { Provider } from 'react-intl-redux'; | ||
import { Router } from 'react-router-dom'; | ||
import { createMemoryHistory } from 'history'; | ||
import ReportNavigation from './ReportNavigation'; | ||
import configureStore from 'redux-mock-store'; | ||
import '@testing-library/jest-dom/extend-expect'; | ||
|
||
const mockStore = configureStore(); | ||
const store = mockStore({ | ||
intl: { | ||
locale: 'en', | ||
messages: {}, | ||
}, | ||
}); | ||
|
||
jest.mock( | ||
'@plone/volto/components/theme/Navigation/withContentNavigation', | ||
() => ({ | ||
withContentNavigation: (Component) => (props) => ( | ||
<Component {...props} navigation={mockNavigation} /> | ||
), | ||
}), | ||
); | ||
|
||
// Mock navigation data | ||
const mockNavigation = { | ||
items: [ | ||
{ | ||
'@id': '/item1', | ||
title: 'Item 1', | ||
href: '/item1', | ||
type: 'document', | ||
description: 'Item 1 description', | ||
is_current: false, | ||
is_in_path: false, | ||
items: [ | ||
{ | ||
'@id': '/item1/subitem1', | ||
title: 'Subitem 1', | ||
href: '/item1/subitem1', | ||
type: 'document', | ||
is_current: false, | ||
is_in_path: false, | ||
items: [], | ||
}, | ||
], | ||
}, | ||
{ | ||
'@id': '/item2', | ||
title: 'Item 2', | ||
href: '/item2', | ||
type: 'document', | ||
description: 'Item 2 description', | ||
is_current: true, | ||
is_in_path: true, | ||
items: [], | ||
}, | ||
], | ||
has_custom_name: true, | ||
title: 'Custom Navigation', | ||
url: '/custom-navigation', | ||
}; | ||
|
||
describe('ReportNavigation', () => { | ||
it('renders navigation items correctly', () => { | ||
const history = createMemoryHistory(); | ||
const { getByText } = render( | ||
<Provider store={store}> | ||
<Router history={history}> | ||
<ReportNavigation /> | ||
</Router> | ||
</Provider>, | ||
); | ||
|
||
// Check if the navigation header is rendered | ||
expect(getByText('Custom Navigation')).toBeInTheDocument(); | ||
|
||
// Check if the navigation items are rendered | ||
expect(getByText('Item 1')).toBeInTheDocument(); | ||
expect(getByText('Item 2')).toBeInTheDocument(); | ||
expect(getByText('Subitem 1')).toBeInTheDocument(); | ||
}); | ||
|
||
it('toggles details on summary click', () => { | ||
const history = createMemoryHistory(); | ||
const { container } = render( | ||
<Provider store={store}> | ||
<Router history={history}> | ||
<ReportNavigation /> | ||
</Router> | ||
</Provider>, | ||
); | ||
|
||
const detailsElement = container.querySelector('a[href="/item1"]'); | ||
|
||
// Simulate click on summary | ||
fireEvent.click(detailsElement); | ||
}); | ||
|
||
it('renders links with correct href attributes', () => { | ||
const history = createMemoryHistory(); | ||
const { getByText } = render( | ||
<Provider store={store}> | ||
<Router history={history}> | ||
<ReportNavigation /> | ||
</Router> | ||
</Provider>, | ||
); | ||
|
||
expect(getByText('Item 1').closest('a')).toHaveAttribute('href', '/item1'); | ||
expect(getByText('Subitem 1').closest('a')).toHaveAttribute( | ||
'href', | ||
'/item1/subitem1', | ||
); | ||
}); | ||
|
||
it('applies active class to the current navigation item', () => { | ||
const history = createMemoryHistory(); | ||
const { getByText } = render( | ||
<Provider store={store}> | ||
<Router history={history}> | ||
<ReportNavigation /> | ||
</Router> | ||
</Provider>, | ||
); | ||
|
||
const activeItem = getByText('Item 2'); | ||
expect(activeItem).toHaveClass('in_path'); | ||
}); | ||
}); |