@times-components/ts-components 1.140.5 → 1.141.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,367 @@
1
+ import React from 'react';
2
+ import { render, waitFor } from '@testing-library/react';
3
+ import '@testing-library/jest-dom';
4
+ import {
5
+ AlgoliaSearchProvider,
6
+ useAlgoliaSearch
7
+ } from '../AlgoliaSearchProvider';
8
+
9
+ jest.mock('algoliasearch', () => {
10
+ return jest.fn(() => ({
11
+ initIndex: jest.fn()
12
+ }));
13
+ });
14
+
15
+ jest.mock('../algoliaRelatedArticles', () => ({
16
+ searchRelatedArticles: jest.fn()
17
+ }));
18
+
19
+ import algoliasearch from 'algoliasearch';
20
+ import { searchRelatedArticles } from '../algoliaRelatedArticles';
21
+
22
+ const mockAlgoliasearch = algoliasearch as jest.MockedFunction<
23
+ typeof algoliasearch
24
+ >;
25
+ const mockSearchRelatedArticles = searchRelatedArticles as jest.MockedFunction<
26
+ typeof searchRelatedArticles
27
+ >;
28
+
29
+ const TestComponent: React.FC = () => {
30
+ const { getRelatedArticles } = useAlgoliaSearch();
31
+
32
+ React.useEffect(
33
+ () => {
34
+ if (getRelatedArticles) {
35
+ getRelatedArticles();
36
+ }
37
+ },
38
+ [getRelatedArticles]
39
+ );
40
+
41
+ return <div data-testid="test-component">Test Component</div>;
42
+ };
43
+
44
+ const mockAnalyticsStream = jest.fn();
45
+
46
+ const mockAlgoliaSearchKeys = {
47
+ applicationId: 'test-app-id',
48
+ apiKey: 'test-api-key',
49
+ indexName: 'test-index'
50
+ };
51
+
52
+ const mockArticle = {
53
+ id: 'test-article-123',
54
+ label: 'Test Article Label',
55
+ headline: 'Test Article Headline',
56
+ section: 'news',
57
+ topics: [{ name: 'Technology' }],
58
+ bylines: []
59
+ };
60
+
61
+ describe('AlgoliaSearchProvider', () => {
62
+ let mockIndex: any;
63
+
64
+ beforeEach(() => {
65
+ jest.clearAllMocks();
66
+ mockIndex = {
67
+ search: jest.fn()
68
+ };
69
+ mockAlgoliasearch.mockReturnValue({
70
+ initIndex: jest.fn().mockReturnValue(mockIndex)
71
+ } as any);
72
+ });
73
+
74
+ describe('Provider functionality', () => {
75
+ it('should render children correctly', () => {
76
+ const { getByTestId } = render(
77
+ <AlgoliaSearchProvider
78
+ algoliaSearchKeys={mockAlgoliaSearchKeys}
79
+ article={mockArticle}
80
+ analyticsStream={mockAnalyticsStream}
81
+ >
82
+ <div data-testid="child-component">Child Content</div>
83
+ </AlgoliaSearchProvider>
84
+ );
85
+
86
+ expect(getByTestId('child-component')).toBeInTheDocument();
87
+ });
88
+
89
+ it('should create algolia index with correct parameters', () => {
90
+ render(
91
+ <AlgoliaSearchProvider
92
+ algoliaSearchKeys={mockAlgoliaSearchKeys}
93
+ article={mockArticle}
94
+ analyticsStream={mockAnalyticsStream}
95
+ >
96
+ <div>Content</div>
97
+ </AlgoliaSearchProvider>
98
+ );
99
+
100
+ expect(mockAlgoliasearch).toHaveBeenCalledWith(
101
+ 'test-app-id',
102
+ 'test-api-key'
103
+ );
104
+ expect(
105
+ mockAlgoliasearch('test-app-id', 'test-api-key').initIndex
106
+ ).toHaveBeenCalledWith('test-index');
107
+ });
108
+
109
+ it('should handle null algoliaSearchKeys', () => {
110
+ render(
111
+ <AlgoliaSearchProvider
112
+ algoliaSearchKeys={null as any}
113
+ article={mockArticle}
114
+ analyticsStream={mockAnalyticsStream}
115
+ >
116
+ <div>Content</div>
117
+ </AlgoliaSearchProvider>
118
+ );
119
+
120
+ expect(mockAlgoliasearch).not.toHaveBeenCalled();
121
+ });
122
+
123
+ it('should recreate index when algoliaSearchKeys change', () => {
124
+ const { rerender } = render(
125
+ <AlgoliaSearchProvider
126
+ algoliaSearchKeys={mockAlgoliaSearchKeys}
127
+ article={mockArticle}
128
+ analyticsStream={mockAnalyticsStream}
129
+ >
130
+ <div>Content</div>
131
+ </AlgoliaSearchProvider>
132
+ );
133
+
134
+ expect(mockAlgoliasearch).toHaveBeenCalledTimes(1);
135
+
136
+ const newKeys = {
137
+ ...mockAlgoliaSearchKeys,
138
+ applicationId: 'new-app-id'
139
+ };
140
+
141
+ rerender(
142
+ <AlgoliaSearchProvider
143
+ algoliaSearchKeys={newKeys}
144
+ article={mockArticle}
145
+ analyticsStream={mockAnalyticsStream}
146
+ >
147
+ <div>Content</div>
148
+ </AlgoliaSearchProvider>
149
+ );
150
+
151
+ expect(mockAlgoliasearch).toHaveBeenCalledTimes(2);
152
+ expect(mockAlgoliasearch).toHaveBeenLastCalledWith(
153
+ 'new-app-id',
154
+ 'test-api-key'
155
+ );
156
+ });
157
+ });
158
+
159
+ describe('getRelatedArticles functionality', () => {
160
+ it('should call searchRelatedArticles with correct parameters when index and article exist', async () => {
161
+ const mockResults = {
162
+ query: 'test query',
163
+ sliceName: 'test slice',
164
+ items: [],
165
+ count: 5
166
+ };
167
+ mockSearchRelatedArticles.mockResolvedValue(mockResults);
168
+
169
+ render(
170
+ <AlgoliaSearchProvider
171
+ algoliaSearchKeys={mockAlgoliaSearchKeys}
172
+ article={mockArticle}
173
+ analyticsStream={mockAnalyticsStream}
174
+ >
175
+ <TestComponent />
176
+ </AlgoliaSearchProvider>
177
+ );
178
+
179
+ await waitFor(() => {
180
+ expect(mockSearchRelatedArticles).toHaveBeenCalledWith(
181
+ mockIndex,
182
+ mockArticle,
183
+ mockAnalyticsStream
184
+ );
185
+ });
186
+ });
187
+
188
+ it('should return null when no index is available', async () => {
189
+ let resultFromHook: any;
190
+
191
+ const TestComponentWithResult: React.FC = () => {
192
+ const { getRelatedArticles } = useAlgoliaSearch();
193
+
194
+ React.useEffect(
195
+ () => {
196
+ const fetchResults = async () => {
197
+ if (getRelatedArticles) {
198
+ resultFromHook = await getRelatedArticles();
199
+ }
200
+ };
201
+ fetchResults();
202
+ },
203
+ [getRelatedArticles]
204
+ );
205
+
206
+ return <div>Test</div>;
207
+ };
208
+
209
+ render(
210
+ <AlgoliaSearchProvider
211
+ algoliaSearchKeys={null as any}
212
+ article={mockArticle}
213
+ analyticsStream={mockAnalyticsStream}
214
+ >
215
+ <TestComponentWithResult />
216
+ </AlgoliaSearchProvider>
217
+ );
218
+
219
+ await waitFor(() => {
220
+ expect(resultFromHook).toBeNull();
221
+ });
222
+ expect(mockSearchRelatedArticles).not.toHaveBeenCalled();
223
+ });
224
+
225
+ it('should return null when no article is provided', async () => {
226
+ let resultFromHook: any;
227
+
228
+ const TestComponentWithResult: React.FC = () => {
229
+ const { getRelatedArticles } = useAlgoliaSearch();
230
+
231
+ React.useEffect(
232
+ () => {
233
+ const fetchResults = async () => {
234
+ if (getRelatedArticles) {
235
+ resultFromHook = await getRelatedArticles();
236
+ }
237
+ };
238
+ fetchResults();
239
+ },
240
+ [getRelatedArticles]
241
+ );
242
+
243
+ return <div>Test</div>;
244
+ };
245
+
246
+ render(
247
+ <AlgoliaSearchProvider
248
+ algoliaSearchKeys={mockAlgoliaSearchKeys}
249
+ article={null}
250
+ analyticsStream={mockAnalyticsStream}
251
+ >
252
+ <TestComponentWithResult />
253
+ </AlgoliaSearchProvider>
254
+ );
255
+
256
+ await waitFor(() => {
257
+ expect(resultFromHook).toBeNull();
258
+ });
259
+ expect(mockSearchRelatedArticles).not.toHaveBeenCalled();
260
+ });
261
+
262
+ it('should return results from searchRelatedArticles', async () => {
263
+ const mockResults = {
264
+ query: 'test query',
265
+ sliceName: 'test slice',
266
+ items: [{ id: 1 }, { id: 2 }, { id: 3 }],
267
+ count: 3
268
+ };
269
+ mockSearchRelatedArticles.mockResolvedValue(mockResults);
270
+
271
+ let resultFromHook: any;
272
+
273
+ const TestComponentWithResult: React.FC = () => {
274
+ const { getRelatedArticles } = useAlgoliaSearch();
275
+
276
+ React.useEffect(
277
+ () => {
278
+ const fetchResults = async () => {
279
+ if (getRelatedArticles) {
280
+ resultFromHook = await getRelatedArticles();
281
+ }
282
+ };
283
+ fetchResults();
284
+ },
285
+ [getRelatedArticles]
286
+ );
287
+
288
+ return <div>Test</div>;
289
+ };
290
+
291
+ render(
292
+ <AlgoliaSearchProvider
293
+ algoliaSearchKeys={mockAlgoliaSearchKeys}
294
+ article={mockArticle}
295
+ analyticsStream={mockAnalyticsStream}
296
+ >
297
+ <TestComponentWithResult />
298
+ </AlgoliaSearchProvider>
299
+ );
300
+
301
+ await waitFor(() => {
302
+ expect(resultFromHook).toEqual(mockResults);
303
+ });
304
+ });
305
+
306
+ it('should memoize getRelatedArticles callback based on dependencies', () => {
307
+ let firstCallback: any;
308
+ let secondCallback: any;
309
+
310
+ const CallbackCapture: React.FC = () => {
311
+ const { getRelatedArticles } = useAlgoliaSearch();
312
+ if (!firstCallback) {
313
+ firstCallback = getRelatedArticles;
314
+ } else if (!secondCallback) {
315
+ secondCallback = getRelatedArticles;
316
+ }
317
+ return <div>Test</div>;
318
+ };
319
+
320
+ const { rerender } = render(
321
+ <AlgoliaSearchProvider
322
+ algoliaSearchKeys={mockAlgoliaSearchKeys}
323
+ article={mockArticle}
324
+ analyticsStream={mockAnalyticsStream}
325
+ >
326
+ <CallbackCapture />
327
+ </AlgoliaSearchProvider>
328
+ );
329
+
330
+ rerender(
331
+ <AlgoliaSearchProvider
332
+ algoliaSearchKeys={mockAlgoliaSearchKeys}
333
+ article={mockArticle}
334
+ analyticsStream={mockAnalyticsStream}
335
+ >
336
+ <CallbackCapture />
337
+ </AlgoliaSearchProvider>
338
+ );
339
+
340
+ expect(firstCallback).toBe(secondCallback);
341
+ });
342
+ });
343
+
344
+ describe('useAlgoliaSearch hook', () => {
345
+ it('should provide context value when used within provider', () => {
346
+ let contextValue: any;
347
+
348
+ const TestComponentInsideProvider: React.FC = () => {
349
+ contextValue = useAlgoliaSearch();
350
+ return <div>Test</div>;
351
+ };
352
+
353
+ render(
354
+ <AlgoliaSearchProvider
355
+ algoliaSearchKeys={mockAlgoliaSearchKeys}
356
+ article={mockArticle}
357
+ analyticsStream={mockAnalyticsStream}
358
+ >
359
+ <TestComponentInsideProvider />
360
+ </AlgoliaSearchProvider>
361
+ );
362
+
363
+ expect(contextValue).toHaveProperty('getRelatedArticles');
364
+ expect(typeof contextValue.getRelatedArticles).toBe('function');
365
+ });
366
+ });
367
+ });