-
-
Notifications
You must be signed in to change notification settings - Fork 414
/
index.test.ts
152 lines (127 loc) · 3.95 KB
/
index.test.ts
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
import { transform } from '@babel/core'
import preset, { Options } from '.'
const defaultOptions = {
namedExport: 'ReactComponent',
state: { componentName: 'SvgComponent' },
}
const testPreset = (code: string, options: Partial<Options>) => {
const result = transform(code, {
plugins: ['@babel/plugin-syntax-jsx'],
presets: [[preset, { ...defaultOptions, ...options }]],
configFile: false,
})
return result?.code
}
describe('preset', () => {
it('handles svgProps', () => {
expect(
testPreset('<svg />', {
svgProps: {
foo: 'bar',
x: '{y}',
},
}),
).toMatchInlineSnapshot(`
"import * as React from \\"react\\";
const SvgComponent = () => <svg foo=\\"bar\\" x={y} />;
export default SvgComponent;"
`)
})
it('handles titleProp', () => {
expect(
testPreset('<svg></svg>', {
titleProp: true,
}),
).toMatchInlineSnapshot(`
"import * as React from \\"react\\";
const SvgComponent = ({
title,
titleId
}) => <svg aria-labelledby={titleId}>{title ? <title id={titleId}>{title}</title> : null}</svg>;
export default SvgComponent;"
`)
})
it('handles titleProp and fallback on existing title', () => {
// testing when existing title has string as chilren
expect(
testPreset(`<svg><title>Hello</title></svg>`, {
titleProp: true,
}),
).toMatchInlineSnapshot(`
"import * as React from \\"react\\";
const SvgComponent = ({
title,
titleId
}) => <svg aria-labelledby={titleId}>{title === undefined ? <title id={titleId}>Hello</title> : title ? <title id={titleId}>{title}</title> : null}</svg>;
export default SvgComponent;"
`)
// testing when existing title has JSXExpression as children
expect(
testPreset(`<svg><title>{"Hello"}</title></svg>`, {
titleProp: true,
}),
).toMatchInlineSnapshot(`
"import * as React from \\"react\\";
const SvgComponent = ({
title,
titleId
}) => <svg aria-labelledby={titleId}>{title === undefined ? <title id={titleId}>{\\"Hello\\"}</title> : title ? <title id={titleId}>{title}</title> : null}</svg>;
export default SvgComponent;"
`)
})
it('handles replaceAttrValues', () => {
expect(
testPreset('<svg a="#000" b="#fff" />', {
replaceAttrValues: {
'#000': 'black',
'#fff': '{props.white}',
},
}),
).toMatchInlineSnapshot(`
"import * as React from \\"react\\";
const SvgComponent = () => <svg a=\\"black\\" b={props.white} />;
export default SvgComponent;"
`)
})
it('handles expandProps & icon & dimensions', () => {
expect(
testPreset('<svg a="#000" b="#fff" />', {
expandProps: 'end',
icon: true,
dimensions: true,
}),
).toMatchInlineSnapshot(`
"import * as React from \\"react\\";
const SvgComponent = props => <svg a=\\"#000\\" b=\\"#fff\\" width=\\"1em\\" height=\\"1em\\" {...props} />;
export default SvgComponent;"
`)
})
it('handles custom icon size', () => {
expect(
testPreset('<svg a="#000" b="#fff" />', {
expandProps: 'end',
icon: 24,
dimensions: true,
}),
).toMatchInlineSnapshot(`
"import * as React from \\"react\\";
const SvgComponent = props => <svg a=\\"#000\\" b=\\"#fff\\" width={24} height={24} {...props} />;
export default SvgComponent;"
`)
})
it('defaults to 24 on native', () => {
expect(
testPreset('<svg a="#000" b="#fff" />', {
expandProps: 'end',
icon: true,
native: true,
dimensions: true,
}),
).toMatchInlineSnapshot(`
"import * as React from \\"react\\";
import Svg from \\"react-native-svg\\";
const SvgComponent = props => <Svg a=\\"#000\\" b=\\"#fff\\" width={24} height={24} {...props} />;
export default SvgComponent;"
`)
})
})