Newer
Older
import { FIRST_ARRAY_ELEMENT } from '@/constants/common';
import { bioEntityResponseFixture } from '@/models/fixtures/bioEntityContentsFixture';
import { modelsFixture } from '@/models/fixtures/modelsFixture';
import { apiPath } from '@/redux/apiPath';
import { DEFAULT_POSITION } from '@/redux/map/map.constants';
import { INITIAL_STORE_STATE_MOCK } from '@/redux/root/root.fixtures';
import { AppDispatch, RootState } from '@/redux/store';
import { MapModel, PublicationElement } from '@/types/models';
import { mockNetworkNewAPIResponse } from '@/utils/mockNetworkResponse';
import {
InitialStoreState,
getReduxStoreWithActionsListener,
} from '@/utils/testing/getReduxStoreActionsListener';
import { render, screen, waitFor } from '@testing-library/react';
import { HttpStatusCode } from 'axios';
import { MockStoreEnhanced } from 'redux-mock-store';
import { isReactionElement } from '@/redux/reactions/isReactionElement';
import { ElementLink } from './ElementLink.component';
const mockedAxiosNewClient = mockNetworkNewAPIResponse();
const TARGET_ELEMENT: PublicationElement = {
...bioEntityResponseFixture.content[FIRST_ARRAY_ELEMENT].bioEntity,
model: 52,
idReaction: 're1234',
};
const MODEL: MapModel = {
...modelsFixture[FIRST_ARRAY_ELEMENT],
id: TARGET_ELEMENT.model,
};
const OTHER_MODEL: MapModel = {
...modelsFixture[FIRST_ARRAY_ELEMENT],
target: PublicationElement;
const getElementText = (element: PublicationElement): string => {
const isReaction = isReactionElement(element);
const prefix = isReaction ? 'Reaction: ' : 'Element: ';
return prefix + element.elementId;
const getSearchQuery = (element: PublicationElement): string => {
const isReaction = isReactionElement(element);
return (isReaction ? 'reaction:' : 'element:') + element.id;
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
const renderComponent = (
props: Props,
initialStoreState: InitialStoreState = {},
): { store: MockStoreEnhanced<Partial<RootState>, AppDispatch> } => {
const { Wrapper, store } = getReduxStoreWithActionsListener(initialStoreState);
return (
render(
<Wrapper>
<ElementLink target={props.target} />
</Wrapper>,
),
{
store,
}
);
};
describe('ElementLink - component', () => {
describe('when loaded', () => {
mockedAxiosNewClient
.onGet(
apiPath.getBioEntityContentsStringWithQuery({
searchQuery: TARGET_ELEMENT.id.toString(),
isPerfectMatch: true,
}),
)
.reply(HttpStatusCode.Ok, bioEntityResponseFixture);
beforeEach(() => {
renderComponent({ target: TARGET_ELEMENT }, INITIAL_STORE_STATE_MOCK);
});
it('should should show element id', async () => {
const element = TARGET_ELEMENT;
expect(screen.getByText(getElementText(element))).toBeInTheDocument();
});
});
});
describe('when clicked (currentModel different than target model)', () => {
mockedAxiosNewClient
.onGet(
apiPath.getBioEntityContentsStringWithQuery({
searchQuery: TARGET_ELEMENT.id.toString(),
isPerfectMatch: true,
}),
)
.reply(HttpStatusCode.Ok, bioEntityResponseFixture);
it('should close modal, search for element, open drawer and open submap on link click', async () => {
const { store } = renderComponent(
{ target: TARGET_ELEMENT },
{
...INITIAL_STORE_STATE_MOCK,
models: {
...INITIAL_STORE_STATE_MOCK.models,
const element = TARGET_ELEMENT;
const link = screen.getByText(getElementText(element));
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
link.click();
const actions = store.getActions();
// close modal
expect(actions).toEqual(
expect.arrayContaining([
expect.objectContaining({
payload: undefined,
type: 'modal/closeModal',
}),
]),
);
// search for element
expect(actions).toEqual(
expect.arrayContaining([
expect.objectContaining({
payload: undefined,
type: 'project/getSearchData/pending',
}),
]),
);
// open drawer
expect(actions).toEqual(
expect.arrayContaining([
expect.objectContaining({
payload: getSearchQuery(element),
type: 'drawer/openSearchDrawerWithSelectedTab',
}),
]),
);
// open submap
expect(actions).toEqual(
expect.arrayContaining([
expect.objectContaining({
payload: {
modelId: TARGET_ELEMENT.model,
modelName: MODEL.name,
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
},
type: 'map/openMapAndSetActive',
}),
]),
);
});
});
});
describe('when clicked (currentModel the same as target model)', () => {
mockedAxiosNewClient
.onGet(
apiPath.getBioEntityContentsStringWithQuery({
searchQuery: TARGET_ELEMENT.id.toString(),
isPerfectMatch: true,
}),
)
.reply(HttpStatusCode.Ok, bioEntityResponseFixture);
it('should close modal, search for element, open drawer and set submap on link click', async () => {
const { store } = renderComponent(
{ target: TARGET_ELEMENT },
{
...INITIAL_STORE_STATE_MOCK,
models: {
...INITIAL_STORE_STATE_MOCK.models,
},
map: {
...INITIAL_STORE_STATE_MOCK.map,
data: {
...INITIAL_STORE_STATE_MOCK.map.data,
modelId: TARGET_ELEMENT.model,
modelName: MODEL.name,
lastPosition: DEFAULT_POSITION,
},
],
},
},
);
const element = TARGET_ELEMENT;
const link = screen.getByText(getElementText(element));
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
link.click();
const actions = store.getActions();
// close modal
expect(actions).toEqual(
expect.arrayContaining([
expect.objectContaining({
payload: undefined,
type: 'modal/closeModal',
}),
]),
);
// search for element
expect(actions).toEqual(
expect.arrayContaining([
expect.objectContaining({
payload: undefined,
type: 'project/getSearchData/pending',
}),
]),
);
// open drawer
expect(actions).toEqual(
expect.arrayContaining([
expect.objectContaining({
payload: getSearchQuery(element),
type: 'drawer/openSearchDrawerWithSelectedTab',
}),
]),
);
// set submap
expect(actions).toEqual(
expect.arrayContaining([
expect.objectContaining({
payload: {