forked from i18next/react-i18next
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrans.nodeToString.spec.js
128 lines (120 loc) · 4.17 KB
/
trans.nodeToString.spec.js
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
import React from 'react';
import { nodesToString } from '../src/Trans';
describe('trans nodeToString', () => {
describe('treat like other components (legacy)', () => {
it('should handle voidElements eg. br', () => {
const fragment = (
<>
lorem <br /> ipsum
</>
);
const expected = 'lorem <1></1> ipsum';
const transKeepBasicHtmlNodesFor = [];
const actual = nodesToString(fragment.props.children, { transKeepBasicHtmlNodesFor });
expect(actual).toEqual(expected);
});
it('should handle non voidElements eg. strong not having attributes and only one child typeof string', () => {
const fragment = (
<>
lorem <strong>bold</strong> ipsum
</>
);
const expected = 'lorem <1>bold</1> ipsum';
const transKeepBasicHtmlNodesFor = [];
const actual = nodesToString(fragment.props.children, { transKeepBasicHtmlNodesFor });
expect(actual).toEqual(expected);
});
});
describe('treat simplified if simple components', () => {
it('should handle voidElements eg. br', () => {
const fragment = (
<>
lorem <br /> ipsum
</>
);
const expected = 'lorem <br/> ipsum';
const transKeepBasicHtmlNodesFor = ['br', 'strong', 'i'];
const actual = nodesToString(fragment.props.children, {
transSupportBasicHtmlNodes: true,
transKeepBasicHtmlNodesFor,
});
expect(actual).toEqual(expected);
});
it('should handle non voidElements eg. strong not having attributes and only one child typeof string', () => {
const fragment = (
<>
lorem <strong>bold</strong> ipsum
</>
);
const expected = 'lorem <strong>bold</strong> ipsum';
const transKeepBasicHtmlNodesFor = ['br', 'strong', 'i'];
const actual = nodesToString(fragment.props.children, {
transSupportBasicHtmlNodes: true,
transKeepBasicHtmlNodesFor,
});
expect(actual).toEqual(expected);
});
});
describe('treat like other components if not simple (legacy)', () => {
it('should handle voidElements eg. br', () => {
const fragment = (
<>
lorem <i className="icon-gear" /> ipsum
</>
);
const expected = 'lorem <1></1> ipsum';
const transKeepBasicHtmlNodesFor = ['br', 'strong', 'i'];
const actual = nodesToString(fragment.props.children, {
transSupportBasicHtmlNodes: true,
transKeepBasicHtmlNodesFor,
});
expect(actual).toEqual(expected);
});
it('should handle non voidElements eg. strong not having attributes and only one child typeof string', () => {
const fragment = (
<>
lorem <strong className="special">bold</strong> ipsum
</>
);
const expected = 'lorem <1>bold</1> ipsum';
const transKeepBasicHtmlNodesFor = ['br', 'strong', 'i'];
const actual = nodesToString(fragment.props.children, {
transSupportBasicHtmlNodes: true,
transKeepBasicHtmlNodesFor,
});
expect(actual).toEqual(expected);
});
});
describe('having dynamic list maps', () => {
it('should create normal inner children if not set to ignore them', () => {
const fragment = (
<>
{'lorem ' /* eslint-disable-line react/jsx-curly-brace-presence */}
<ul>
<li>a</li>
<li>b</li>
</ul>
{' ipsum' /* eslint-disable-line react/jsx-curly-brace-presence */}
</>
);
const expected = 'lorem <1><0>a</0><1>b</1></1> ipsum';
const actual = nodesToString(fragment.props.children, {});
expect(actual).toEqual(expected);
});
it('should omit inner children if set', () => {
const fragment = (
<>
{'lorem ' /* eslint-disable-line react/jsx-curly-brace-presence */}
<ul i18nIsDynamicList>
<li>a</li>
<li>b</li>
</ul>
{' ipsum' /* eslint-disable-line react/jsx-curly-brace-presence */}
</>
);
const expected = 'lorem <1></1> ipsum';
const actual = nodesToString(fragment.props.children, {});
expect(actual).toEqual(expected);
});
});
});