-
Notifications
You must be signed in to change notification settings - Fork 279
Expand file tree
/
Copy pathindex.test.tsx
More file actions
384 lines (319 loc) · 11.6 KB
/
Copy pathindex.test.tsx
File metadata and controls
384 lines (319 loc) · 11.6 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
import {
render,
screen,
fireEvent,
} from '#app/components/react-testing-library-with-providers';
import { matchers } from '@emotion/jest';
import * as viewTracking from '#app/hooks/useViewTracker';
import * as clickTracking from '#app/hooks/useClickTrackerHandler';
import { ServiceContext } from '#app/contexts/ServiceContext';
import { ServiceConfig } from '#app/models/types/serviceConfig';
import { service as portugueseConfig } from '#app/lib/config/services/portuguese';
import { service as turkceConfig } from '#app/lib/config/services/turkce';
import { service as arabicConfig } from '#app/lib/config/services/arabic';
import {
ARTICLE_PAGE,
LIVE_TV_PAGE,
MEDIA_ARTICLE_PAGE,
TV_PAGE,
} from '../../routes/utils/pageTypes';
import {
topicTagsFixture,
multipleTopicsFixture,
arabicTopicTagsFixture,
arabicMultipleTopicsFixture,
} from './fixtures';
import useFetchTopicPromos from './useFetchTopicPromos';
import TopicDiscovery from '.';
jest.mock('./useFetchTopicPromos');
expect.extend(matchers);
describe('TopicDiscovery', () => {
const mockUseFetchTopicPromos = useFetchTopicPromos as jest.MockedFunction<
typeof useFetchTopicPromos
>;
beforeEach(() => {
mockUseFetchTopicPromos.mockReturnValue({
topicPromos:
multipleTopicsFixture[topicTagsFixture[0].topicId].data.items,
isLoading: false,
isError: false,
});
});
afterEach(() => {
jest.restoreAllMocks();
});
it('should render the heading', () => {
render(<TopicDiscovery topics={topicTagsFixture} />, {
service: 'portuguese',
});
expect(
screen.getByRole('heading', { name: 'Descubra mais' }),
).toBeInTheDocument();
});
it('should render a section with the topic-discovery test id', () => {
render(<TopicDiscovery topics={topicTagsFixture} />, {
service: 'portuguese',
});
expect(screen.getByTestId('topic-discovery')).toBeInTheDocument();
});
it('should render tabs for each valid topic', () => {
render(<TopicDiscovery topics={topicTagsFixture} />, {
service: 'portuguese',
});
expect(
screen.getByRole('tab', { name: topicTagsFixture[0].topicName }),
).toBeInTheDocument();
expect(
screen.getByRole('tab', { name: topicTagsFixture[1].topicName }),
).toBeInTheDocument();
expect(
screen.getByRole('tab', { name: topicTagsFixture[2].topicName }),
).toBeInTheDocument();
expect(
screen.queryByRole('tab', { name: topicTagsFixture[3].topicName }),
).toBeInTheDocument();
});
it('should render the first topic as the active tab by default', () => {
render(<TopicDiscovery topics={topicTagsFixture} />, {
service: 'portuguese',
});
expect(
screen.getByRole('tab', { name: topicTagsFixture[0].topicName }),
).toHaveAttribute('aria-selected', 'true');
});
it('should render a tabpanel', () => {
render(<TopicDiscovery topics={topicTagsFixture} />, {
service: 'portuguese',
});
expect(screen.getByRole('tabpanel')).toBeInTheDocument();
});
it('should render promos for the active topic', () => {
render(<TopicDiscovery topics={topicTagsFixture} />, {
service: 'portuguese',
});
const firstTopicTitle = topicTagsFixture[0].topicName;
expect(screen.getByText(firstTopicTitle)).toBeInTheDocument();
});
it('should switch active topic when a different tab is clicked', () => {
render(<TopicDiscovery topics={topicTagsFixture} />, {
service: 'portuguese',
});
fireEvent.click(
screen.getByRole('tab', { name: topicTagsFixture[1].topicName }),
);
expect(
screen.getByRole('tab', { name: topicTagsFixture[1].topicName }),
).toHaveAttribute('aria-selected', 'true');
const secondTopicTitle = topicTagsFixture[1].topicName;
expect(screen.getByText(secondTopicTitle)).toBeInTheDocument();
});
it('renders the "more about" section with topic title last if {topic} is last in the config', async () => {
const config: ServiceConfig = { ...portugueseConfig.default };
render(
<ServiceContext.Provider value={config}>
<TopicDiscovery topics={topicTagsFixture} />
</ServiceContext.Provider>,
);
// Wait for loading to finish and the link to appear
const moreAbout = await screen.findByTestId('topic-discovery-more-about');
expect(moreAbout).toHaveTextContent(
`Mais sobre ${topicTagsFixture[0].topicName}`,
);
});
it('renders the "more about" section with topic title first if {topic} is first in the config', async () => {
const config: ServiceConfig = { ...turkceConfig.default };
render(
<ServiceContext.Provider value={config}>
<TopicDiscovery topics={topicTagsFixture} />
</ServiceContext.Provider>,
);
// Wait for loading to finish and the link to appear
const moreAbout = await screen.findByTestId('topic-discovery-more-about');
expect(moreAbout).toHaveTextContent(
`${topicTagsFixture[0].topicName} hakkında daha fazla`,
);
});
it('renders the "more about" section with fallback if moreAbout is missing', async () => {
const portugueseTranslations = {
...portugueseConfig.default.translations,
topicDiscovery: { heading: 'Discover more' },
};
const config = {
...portugueseConfig.default,
translations: portugueseTranslations,
} as ServiceConfig;
render(
<ServiceContext.Provider value={config}>
<TopicDiscovery topics={topicTagsFixture} />
</ServiceContext.Provider>,
);
await screen.findByText(`More about ${topicTagsFixture[0].topicName}`);
});
it('should not render when there are no valid topics', () => {
const { container } = render(<TopicDiscovery topics={[]} />, {
service: 'portuguese',
});
expect(container).toBeEmptyDOMElement();
});
it('renders fetch error message when promos request fails', async () => {
mockUseFetchTopicPromos.mockReturnValue({
topicPromos: [],
isLoading: false,
isError: true,
});
render(<TopicDiscovery topics={topicTagsFixture} />, {
service: 'portuguese',
});
expect(
await screen.findByText('Falha ao carregar. Tente novamente'),
).toBeInTheDocument();
expect(
screen.queryByTestId('topic-discovery-more-about'),
).not.toBeInTheDocument();
});
it.each([MEDIA_ARTICLE_PAGE, TV_PAGE, LIVE_TV_PAGE])(
'should render promo links in dark ui colours for %s pages',
pageType => {
render(<TopicDiscovery topics={topicTagsFixture} />, {
pageType,
service: 'portuguese',
});
const promoLink = screen.getByRole('link', {
name: /Derrota em dose dupla/,
});
expect(window.getComputedStyle(promoLink).color).toBe(
'rgb(246, 246, 246)',
);
expect(screen.getByRole('tabpanel')).toHaveStyleRule('color', '#B0B2B4', {
target: 'li .promo-text a:visited',
});
},
);
it('should render promo links in light ui colours for article pages', () => {
render(<TopicDiscovery topics={topicTagsFixture} />, {
pageType: ARTICLE_PAGE,
service: 'portuguese',
});
const promoLink = screen.getByRole('link', {
name: /Derrota em dose dupla/,
});
expect(window.getComputedStyle(promoLink).color).toBe('rgb(20, 20, 20)');
expect(screen.getByRole('tabpanel')).toHaveStyleRule('color', '#545658', {
target: 'li .promo-text a:visited',
});
});
describe('analytics', () => {
const groupTracker = {
itemCount: 4,
name: 'Descubra mais',
type: 'topic-discovery',
};
it('should call useViewTracker with topic-discovery component name', () => {
const viewTrackerSpy = jest.spyOn(viewTracking, 'default');
render(<TopicDiscovery topics={topicTagsFixture} />, {
service: 'portuguese',
});
expect(viewTrackerSpy).toHaveBeenCalledWith({
componentName: 'topic-discovery',
groupTracker,
});
viewTrackerSpy.mockRestore();
});
it('should call useViewTracker with topic-discovery-fetch-error-message component name when there is a fetch error', async () => {
const viewTrackerSpy = jest.spyOn(viewTracking, 'default');
mockUseFetchTopicPromos.mockReturnValue({
topicPromos: [],
isLoading: false,
isError: true,
});
render(<TopicDiscovery topics={topicTagsFixture} />, {
service: 'portuguese',
});
expect(viewTrackerSpy).toHaveBeenCalledWith({
componentName: 'topic-discovery-fetch-error-message',
});
viewTrackerSpy.mockRestore();
});
it('should call useClickTrackerHandler with topic-discovery-tab-<id> component name and preventNavigation', () => {
const clickTrackerSpy = jest
.spyOn(clickTracking, 'default')
.mockImplementation(() => ({ onClick: jest.fn() }));
render(<TopicDiscovery topics={topicTagsFixture} />, {
service: 'portuguese',
});
const expectedCalls = topicTagsFixture.map(topic => ({
componentName: 'topic-discovery-tab',
groupTracker,
itemTracker: {
type: 'topic-discovery-tab',
text: topic.topicName,
position: topicTagsFixture.indexOf(topic) + 1,
resourceId: topic.topicId,
},
preventNavigation: true,
}));
expectedCalls.forEach(expectedCall => {
expect(clickTrackerSpy).toHaveBeenCalledWith(expectedCall);
});
clickTrackerSpy.mockRestore();
});
it('should call useClickTrackerHandler when "more about" link is clicked', async () => {
const mockClickHandler = jest.fn();
jest
.spyOn(clickTracking, 'default')
.mockReturnValue({ onClick: mockClickHandler });
render(<TopicDiscovery topics={topicTagsFixture} />, {
service: 'portuguese',
});
const moreAboutLink = await screen.findByTestId(
'topic-discovery-more-about',
);
fireEvent.click(moreAboutLink);
expect(mockClickHandler).toHaveBeenCalledTimes(1);
expect(clickTracking.default).toHaveBeenCalledWith({
componentName: 'topic-discovery-more-about-link',
groupTracker,
itemTracker: {
type: 'topic-discovery-more-about-link',
text: `Mais sobre ${topicTagsFixture[0].topicName}`,
resourceId: topicTagsFixture[0].topicId,
},
});
});
});
describe('RTL rendering for Arabic', () => {
beforeEach(() => {
mockUseFetchTopicPromos.mockReturnValue({
topicPromos:
arabicMultipleTopicsFixture[arabicTopicTagsFixture[0].topicId].data
.items,
isLoading: false,
isError: false,
});
});
it('should set dir attribute to rtl on section element when service is arabic', () => {
const config: ServiceConfig = { ...arabicConfig.default };
render(
<ServiceContext.Provider value={config}>
<TopicDiscovery topics={arabicTopicTagsFixture} />
</ServiceContext.Provider>,
);
const section = screen.getByTestId('topic-discovery');
expect(section).toHaveAttribute('dir', 'rtl');
});
it('should switch active arabic topic when a different tab is clicked', () => {
const config: ServiceConfig = { ...arabicConfig.default };
render(
<ServiceContext.Provider value={config}>
<TopicDiscovery topics={arabicTopicTagsFixture} />
</ServiceContext.Provider>,
);
fireEvent.click(
screen.getByRole('tab', { name: arabicTopicTagsFixture[1].topicName }),
);
expect(
screen.getByRole('tab', { name: arabicTopicTagsFixture[1].topicName }),
).toHaveAttribute('aria-selected', 'true');
});
});
});