-
Notifications
You must be signed in to change notification settings - Fork 278
/
Copy pathRichText.test.tsx
524 lines (401 loc) · 14.1 KB
/
RichText.test.tsx
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import React, { ReactNode } from 'react';
import { use, expect } from 'chai';
import { RichText as ReactRichText } from '@sitecore-jss/sitecore-jss-react';
import { NextRouter } from 'next/router';
import { mount } from 'enzyme';
import { RouterContext } from 'next/dist/shared/lib/router-context.shared-runtime';
import { RichText } from './RichText';
import { SinonSpy, spy } from 'sinon';
import sinonChai from 'sinon-chai';
use(sinonChai);
const Router = (): { push: SinonSpy } & Omit<NextRouter, 'push'> => ({
pathname: '/',
route: '/',
query: {},
asPath: '/',
basePath: '',
isLocaleDomain: false,
isFallback: false,
forward: spy(),
isPreview: false,
isReady: false,
events: { emit: spy(), off: spy(), on: spy() },
push: spy(() => Promise.resolve(true)),
replace: spy(() => Promise.resolve(true)),
reload: spy(),
back: spy(),
prefetch: spy(() => Promise.resolve()),
beforePopState: spy(),
});
// Should provide RouterContext in case if we render Link from next/link
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const Page = ({ children, value }: { children: ReactNode; value?: any }) => (
<RouterContext.Provider value={value || Router()}>{children}</RouterContext.Provider>
);
describe('RichText', () => {
it('should initialize links', () => {
const app = document.createElement('main');
document.body.appendChild(app);
const router = Router();
const props = {
field: {
value: `
<div id="test">
<h1>Hello!</h1>
<a href="/t10">1</a>
<a href="/t10">2</a>
<a href="/contains-children"><span id="child">Title</span></a>
</div>`,
},
};
const c = mount(
<Page value={router}>
<RichText {...props} />
</Page>,
{ attachTo: app }
);
expect(c.html()).contains('<div id="test">');
expect(c.html()).contains('<h1>Hello!</h1>');
expect(c.html()).contains('<a href="/t10">1</a>');
expect(c.html()).contains('<a href="/t10">2</a>');
expect(c.html()).contains('<a href="/contains-children"><span id="child">Title</span></a>');
expect(router.prefetch).callCount(2);
const main = document.querySelector('main');
const links = main && main.querySelectorAll('a');
const link1 = links && links[0];
const link2 = links && links[1];
const link3 = links && links[2];
const innerMarkup = document.querySelector('#child') as HTMLSpanElement;
expect(link1!.pathname).to.equal('/t10');
expect(link2!.pathname).to.equal('/t10');
link1?.click();
expect(router.push).callCount(1);
link2?.click();
expect(router.push).callCount(2);
link3?.click();
expect(router.push).callCount(3);
// Check that when we click on link with children "router push" is called with expected pathname
innerMarkup?.click();
expect(router.push).callCount(4);
expect(router.push.getCall(3).calledWith('https://example.com/contains-children')).to.equal(
true
);
expect(c.find(ReactRichText).length).to.equal(1);
document.body.removeChild(app);
});
it('should re-initialize links when the re-mounting with different content', () => {
const app = document.createElement('main');
document.body.appendChild(app);
const router = Router();
const props = {
field: {
value: '<div id="test"><h1>Hello!</h1><a href="/t100">1</a><a href="/t100">2</a></div>',
},
};
const props2 = {
field: {
value: '<div id="test"><h1>Hello!</h1><a href="/t20">1</a><a href="/t20">2</a></div>',
},
};
const initialMountedComponent = mount(
<Page value={router}>
<RichText {...props} />
</Page>,
{ attachTo: app }
);
expect(initialMountedComponent.html()).contains('<div id="test">');
expect(initialMountedComponent.html()).contains('<h1>Hello!</h1>');
expect(initialMountedComponent.html()).contains('<a href="/t100">1</a>');
expect(initialMountedComponent.html()).contains('<a href="/t100">2</a>');
expect(router.prefetch).callCount(1);
const main = document.querySelector('main');
const links = main && main.querySelectorAll('a');
const link1 = links && links[0];
const link2 = links && links[1];
expect(link1!.pathname).to.equal('/t100');
expect(link2!.pathname).to.equal('/t100');
link1 && link1.click();
expect(router.push).callCount(1);
link2 && link2.click();
expect(router.push).callCount(2);
expect(initialMountedComponent.find(ReactRichText).length).to.equal(1);
initialMountedComponent.unmount();
const remountedComponent = mount(
<Page value={router}>
<RichText {...props2} />
</Page>,
{ attachTo: app }
);
expect(remountedComponent.html()).contains('<div id="test">');
expect(remountedComponent.html()).contains('<h1>Hello!</h1>');
expect(remountedComponent.html()).contains('<a href="/t20">1</a>');
expect(remountedComponent.html()).contains('<a href="/t20">2</a>');
expect(router.prefetch).callCount(2);
const links2 = main && main.querySelectorAll('a');
const link3 = links2 && links2[0];
const link4 = links2 && links2[1];
expect(link3!.pathname).to.equal('/t20');
expect(link4!.pathname).to.equal('/t20');
link3 && link3.click();
expect(router.push).callCount(3);
link4 && link4.click();
expect(router.push).callCount(4);
expect(remountedComponent.find(ReactRichText).length).to.equal(1);
document.body.removeChild(app);
});
it('should initialize links using internalLinksSelector', () => {
const app = document.createElement('main');
document.body.appendChild(app);
const router = Router();
const props = {
field: {
value:
'<div id="test"><h1>Hello!</h1><a href="/testpath/t1?test=sample1">t1</a><a href="/t2">t2</a></div>',
},
internalLinksSelector: 'a[href^="/testpath"]',
};
const c = mount(
<Page value={router}>
<RichText {...props} />
</Page>,
{ attachTo: app }
);
expect(c.html()).contains('<div id="test">');
expect(c.html()).contains('<h1>Hello!</h1>');
expect(router.prefetch).callCount(1);
const main = document.querySelector('main');
const links = main && main.querySelectorAll('a');
const link1 = links && links[0];
const link2 = links && links[1];
expect(link1!.href).to.endsWith('/testpath/t1?test=sample1');
expect(link2!.pathname).to.equal('/t2');
link1 && link1.click();
expect(router.push).callCount(1);
link2 && link2.click();
// Check that push not invoked, because second link don't have event listener
expect(router.push).callCount(1);
expect(c.find(ReactRichText).length).to.equal(1);
document.body.removeChild(app);
});
it('should not initialize links when does not have value', () => {
const router = Router();
const props = {
field: {},
};
const c = mount(
<Page value={router}>
<RichText {...props} />
</Page>
);
expect(router.prefetch).callCount(0);
expect(c.find(ReactRichText).length).to.equal(1);
});
it('should not initialize links if no links in markup', () => {
const router = Router();
const props = {
field: {
value: '<div id="test"><h1>Hello!</h1></div>',
},
};
const c = mount(
<Page value={router}>
<RichText {...props} />
</Page>
);
expect(c.html()).contains('<div id="test">');
expect(c.html()).contains('<h1>Hello!</h1>');
expect(router.prefetch).callCount(0);
expect(c.find(ReactRichText).length).to.equal(1);
});
it('should not initialize links when editable', () => {
const router = Router();
const props = {
field: {
editable: '<div id="test"><h1>Hello!</h1><a href="/t1">t1</a><a href="/t2">t2</a></div>',
},
};
const c = mount(
<Page value={router}>
<RichText {...props} />
</Page>
);
expect(c.html()).contains('<div id="test">');
expect(c.html()).contains('<h1>Hello!</h1>');
expect(c.html()).contains('<a href="/t1">t1</a>');
expect(c.html()).contains('<a href="/t2">t2</a>');
expect(router.prefetch).callCount(0);
expect(c.find(ReactRichText).length).to.equal(1);
});
it('should not initialize links when target set to "_blank"', () => {
const app = document.createElement('main');
document.body.appendChild(app);
const router = Router();
const props = {
field: {
value: '<div id="test"><h1>Hello!</h1><a href="/t1" target="_blank">t1</a></div>',
},
};
const c = mount(
<Page value={router}>
<RichText {...props} />
</Page>,
{ attachTo: app }
);
expect(c.html()).contains('<div id="test">');
expect(c.html()).contains('<h1>Hello!</h1>');
expect(c.html()).contains('<a href="/t1" target="_blank">t1</a>');
const main = document.querySelector('main');
const links = main && main.querySelectorAll('a');
const link = links && links[0];
expect(router.prefetch).callCount(0);
link && link.click();
expect(router.push).callCount(0);
expect(c.find(ReactRichText).length).to.equal(1);
});
it('Should not call prefetch when prefetchLinks is set to false', () => {
const app = document.createElement('main');
document.body.appendChild(app);
const router = Router();
const props = {
field: {
value:
'<div id="test"><h1>Prefetch test!</h1><a href="/notprefetched1">1</a><a href="/notprefetched2">2</a></div>',
},
};
const c = mount(
<Page value={router}>
<RichText {...props} prefetchLinks={false} />
</Page>,
{ attachTo: app }
);
expect(c.html()).contains('<div id="test">');
expect(c.html()).contains('<h1>Prefetch test!</h1>');
expect(c.html()).contains('<a href="/notprefetched1">1</a>');
expect(c.html()).contains('<a href="/notprefetched2">2</a>');
expect(router.prefetch).callCount(0);
});
describe('editMode metadata', () => {
const testMetadata = {
contextItem: {
id: '{09A07660-6834-476C-B93B-584248D3003B}',
language: 'en',
revision: 'a0b36ce0a7db49418edf90eb9621e145',
version: 1,
},
fieldId: '{414061F4-FBB1-4591-BC37-BFFA67F745EB}',
fieldType: 'image',
rawValue: 'Test1',
};
it('should render field metadata component when metadata property is present', () => {
const app = document.createElement('main');
document.body.appendChild(app);
const router = Router();
const props = {
field: {
value: `
<div id="test">
<h1>Hello!</h1>
<a href="/t10">1</a>
<a href="/t10">2</a>
<a href="/contains-children"><span id="child">Title</span></a>
</div>`,
metadata: testMetadata,
},
};
const rendered = mount(
<Page value={router}>
<RichText {...props} prefetchLinks={false} />
</Page>,
{ attachTo: app }
);
expect(rendered.html()).to.equal(
[
`<code type="text/sitecore" chrometype="field" class="scpm" kind="open">${JSON.stringify(
testMetadata
)}</code><div>
`,
`<div id="test">
<h1>Hello!</h1>
<a href="/t10">1</a>
<a href="/t10">2</a>
<a href="/contains-children"><span id="child">Title</span></a>
</div></div><code type="text/sitecore" chrometype="field" class="scpm" kind="close"></code>`,
].join('')
);
});
it('should render default empty field placeholder when field value is empty in edit mode metadata', () => {
const app = document.createElement('main');
document.body.appendChild(app);
const router = Router();
const props = {
field: {
value: '',
metadata: testMetadata,
},
};
const rendered = mount(
<Page value={router}>
<RichText {...props} />
</Page>,
{ attachTo: app }
);
expect(rendered.html()).to.equal(
[
`<code type="text/sitecore" chrometype="field" class="scpm" kind="open">${JSON.stringify(
testMetadata
)}</code>`,
'<span>[No text in field]</span>',
'<code type="text/sitecore" chrometype="field" class="scpm" kind="close"></code>',
].join('')
);
});
it('should render custom empty field placeholder when provided, when field value is empty in edit mode metadata', () => {
const app = document.createElement('main');
document.body.appendChild(app);
const router = Router();
const props = {
field: {
value: '',
metadata: testMetadata,
},
};
const EmptyFieldEditingComponent: React.FC = () => (
<span className="empty-field-value-placeholder">Custom Empty field value</span>
);
const rendered = mount(
<Page value={router}>
<RichText {...props} emptyFieldEditingComponent={EmptyFieldEditingComponent} />
</Page>,
{ attachTo: app }
);
expect(rendered.html()).to.equal(
[
`<code type="text/sitecore" chrometype="field" class="scpm" kind="open">${JSON.stringify(
testMetadata
)}</code>`,
'<span class="empty-field-value-placeholder">Custom Empty field value</span>',
'<code type="text/sitecore" chrometype="field" class="scpm" kind="close"></code>',
].join('')
);
});
it('should render nothing when field value is empty, when editing is explicitly disabled in edit mode metadata ', () => {
const app = document.createElement('main');
document.body.appendChild(app);
const router = Router();
const props = {
field: {
value: '',
metadata: testMetadata,
},
};
const rendered = mount(
<Page value={router}>
<RichText {...props} editable={false} />
</Page>,
{ attachTo: app }
);
expect(rendered.html()).to.equal('');
});
});
});