-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
128 additions
and
4 deletions.
There are no files selected for viewing
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
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
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
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,124 @@ | ||
import { navigateToEditPage } from '@src/test/__mocks__/common/hooks/useNavigateToEditPage.mock'; | ||
import { fireEvent, render } from '@testing-library/react'; | ||
import { setInitialGlobalState, StoreWithState } from '@src/test/__mocks__/store'; | ||
import { createMemoryRouter, RouterProvider } from 'react-router-dom'; | ||
import { Comparison } from '@components/Comparison'; | ||
import { ReactNode } from 'react'; | ||
import { Fragment } from 'react/jsx-runtime'; | ||
import { useInputsStore, useSearchStore, useUIStore } from '@src/store'; | ||
|
||
jest.mock('react-intl', () => ({ | ||
FormattedMessage: ({ id, values }: any) => { | ||
return ( | ||
<div id={id}> | ||
{values ? Object.entries(values).map(([k, v]) => <Fragment key={k}>{v as ReactNode}</Fragment>) : id} | ||
</div> | ||
); | ||
}, | ||
useIntl: () => ({ | ||
formatMessage: ({ id }: { id: string }) => id, | ||
}), | ||
})); | ||
|
||
describe('Comparison', () => { | ||
const resetPreviewContent = jest.fn(); | ||
const resetFullDisplayComponentType = jest.fn(); | ||
const resetSelectedInstances = jest.fn(); | ||
|
||
const baseMockState = [ | ||
{ | ||
store: useInputsStore, | ||
state: { previewContent: [{ id: 'mockId', title: 'mockTitle' }] }, | ||
}, | ||
]; | ||
|
||
const renderWithState = (stateArgs?: StoreWithState[]) => { | ||
stateArgs && setInitialGlobalState(stateArgs); | ||
|
||
return render( | ||
<RouterProvider | ||
router={createMemoryRouter([ | ||
{ | ||
path: '/', | ||
element: <Comparison />, | ||
}, | ||
])} | ||
/>, | ||
); | ||
}; | ||
|
||
test('renders placeholder if no previewContent present', () => { | ||
const { getByText } = renderWithState(); | ||
|
||
expect(getByText('ld.chooseTwoResourcesCompare')).toBeInTheDocument(); | ||
}); | ||
|
||
test('renders placeholder if previewContent has only one entry', () => { | ||
const { getByText } = renderWithState(baseMockState); | ||
|
||
expect(getByText('ld.chooseOneResourceCompare')).toBeInTheDocument(); | ||
}); | ||
|
||
test('removes an entry', () => { | ||
const { getByTestId, getByText } = renderWithState(baseMockState); | ||
|
||
fireEvent.click(getByTestId('remove-comparison-entry')); | ||
|
||
expect(getByText('ld.chooseTwoResourcesCompare')).toBeInTheDocument(); | ||
}); | ||
|
||
test('closes comparison', async () => { | ||
const { getByTestId } = renderWithState([ | ||
{ | ||
store: useInputsStore, | ||
state: { resetPreviewContent }, | ||
}, | ||
{ | ||
store: useUIStore, | ||
state: { resetFullDisplayComponentType }, | ||
}, | ||
{ | ||
store: useSearchStore, | ||
state: { resetSelectedInstances }, | ||
}, | ||
]); | ||
|
||
fireEvent.click(getByTestId('close-comparison-section')); | ||
|
||
expect(resetPreviewContent).toHaveBeenCalled(); | ||
expect(resetFullDisplayComponentType).toHaveBeenCalled(); | ||
expect(resetSelectedInstances).toHaveBeenCalled(); | ||
}); | ||
|
||
test('handles navigation to own page', async () => { | ||
const { getByTestId, getByText } = renderWithState(baseMockState); | ||
|
||
fireEvent.click(getByTestId('preview-actions-dropdown')); | ||
fireEvent.click(getByText('ld.edit')); | ||
|
||
expect(navigateToEditPage).toHaveBeenCalled(); | ||
}); | ||
|
||
test('navigates page-wise', async () => { | ||
const { getByTestId, getAllByText } = renderWithState([ | ||
{ | ||
store: useInputsStore, | ||
state: { | ||
previewContent: [ | ||
{ id: 'mockId', title: 'mockTitle' }, | ||
{ id: 'mockId', title: 'mockTitle' }, | ||
{ id: 'mockId', title: 'mockTitle' }, | ||
], | ||
}, | ||
}, | ||
]); | ||
|
||
fireEvent.click(getByTestId('forward-button')); | ||
|
||
expect(getAllByText(2)[0]).toBeInTheDocument(); | ||
|
||
fireEvent.click(getByTestId('backward-button')); | ||
|
||
expect(getAllByText(1)[0]).toBeInTheDocument(); | ||
}); | ||
}); |