-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathComparison.test.tsx
More file actions
170 lines (144 loc) · 4.85 KB
/
Comparison.test.tsx
File metadata and controls
170 lines (144 loc) · 4.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
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 setSelectedInstances = jest.fn();
const setPreviewContent = jest.fn();
const baseMockState = [
{
store: useInputsStore,
state: { previewContent: [{ id: 'mockId', title: 'mockTitle' }], setPreviewContent },
},
];
const renderWithState = (stateArgs?: StoreWithState[]) => {
if (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 } = renderWithState([
{
store: useInputsStore,
state: { previewContent: [{ id: 'mockId', title: 'mockTitle' }] },
},
{
store: useSearchStore,
state: { setSelectedInstances },
},
{
store: useUIStore,
state: { resetFullDisplayComponentType },
},
]);
fireEvent.click(getByTestId('remove-comparison-entry-mockId'));
expect(setSelectedInstances).toHaveBeenCalled();
expect(resetFullDisplayComponentType).toHaveBeenCalled();
});
test('updates current page when removing last item on last page', () => {
const setPreviewContent = jest.fn();
const { getByTestId } = renderWithState([
{
store: useInputsStore,
state: {
previewContent: [
{ id: 'mockId_1', title: 'mockTitle 1' },
{ id: 'mockId_2', title: 'mockTitle 2' },
{ id: 'mockId_3', title: 'mockTitle 3' },
],
setPreviewContent,
},
},
{
store: useSearchStore,
state: { setSelectedInstances },
},
]);
fireEvent.click(getByTestId('forward-button'));
fireEvent.click(getByTestId('remove-comparison-entry-mockId_3'));
expect(setPreviewContent).toHaveBeenCalled();
expect(setSelectedInstances).toHaveBeenCalled();
expect(getByTestId('backward-button')).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_1', title: 'mockTitle 1' },
{ id: 'mockId_2', title: 'mockTitle 2' },
{ id: 'mockId_3', title: 'mockTitle 3' },
],
},
},
]);
fireEvent.click(getByTestId('forward-button'));
expect(getAllByText(2)[0]).toBeInTheDocument();
fireEvent.click(getByTestId('backward-button'));
expect(getAllByText(1)[0]).toBeInTheDocument();
});
});