Skip to content

Commit 708009b

Browse files
authored
Compat with rn-svg 7-12 (#62)
1 parent af95f15 commit 708009b

File tree

1 file changed

+40
-11
lines changed

1 file changed

+40
-11
lines changed

src/components/__tests__/FontAwesomeIcon.test.js

+40-11
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,44 @@ const faAcorn = {
4343
]
4444
}
4545

46+
const blueHex = '0000ff'
47+
const redHex = 'ff0000'
4648

47-
// react-native-svg>9.x changed the way it uses the `fill` attribute. In earlier versions,
48-
// it used an array ([0, DECIMAL_COLOR]), whereas newer versions dropped the array
49-
// in favor of a scalar value %DECIMAL_COLOR%
49+
function rgbToHex(r, g, b) {
50+
return [r, g, b]
51+
.map((c) => {
52+
const hex = c.toString(16);
53+
return hex.length == 1 ? `0${hex}` : hex;
54+
})
55+
.join("");
56+
}
57+
58+
function decimalToHex(decimal) {
59+
return decimal.toString(16).substr(2, 6);
60+
}
61+
62+
// react-native-svg changed the way it uses the `fill` attribute across versions. Older versions
63+
// return [_, r, g, b, _] (where 0 <= {r,g,b} <= 1), while other versions return arrays, and even
64+
// scalar values... We, much like the Borg, will adapt.
5065
function getActualFillColorHex(element) {
51-
const fillProp = element.props.fill
52-
const decimalColor = Array.isArray(fillProp) ? fillProp[1] : fillProp;
53-
// convert to hex
54-
return decimalColor.toString(16);
66+
const fillProp = element.props.fill;
67+
if (!Array.isArray(fillProp)) {
68+
// rn-svg v11 use a simple sclar value, representing the decimal value of the color
69+
// @link https://github.com/react-native-community/react-native-svg/blob/v11.0.1/__tests__/__snapshots__/css.test.tsx.snap
70+
// https://github.com/react-native-community/react-native-svg/blob/v12.1.0/__tests__/__snapshots__/css.test.tsx.snap#L158
71+
return decimalToHex(fillProp);
72+
} else if (fillProp.length === 5) {
73+
// rn-svg <= v8 return an array [_, r, g, b, _], where {rgb} are in the range of {0,1}
74+
// @note no links provided because rn-svg didn't include any tests in those versions
75+
return rgbToHex(fillProp[1] * 255, fillProp[2] * 255, fillProp[3] * 255);
76+
} else if (fillProp.length === 2) {
77+
// rn-svg v9, and v10 return an array with shape [_, DECIMAL_COLOR]
78+
// @link https://github.com/react-native-community/react-native-svg/blob/v9.14.0/__tests__/__snapshots__/css.test.tsx.snap#L159
79+
// @link https://github.com/react-native-community/react-native-svg/blob/v10.1.0/__tests__/__snapshots__/css.test.tsx.snap#L159
80+
return decimalToHex(fillProp[1]);
81+
}
82+
83+
return null;
5584
}
5685

5786
fontawesome.library.add(faCoffee, faCircle)
@@ -247,7 +276,7 @@ describe('duotone support', () => {
247276
const tree = renderer.create(<FontAwesomeIcon icon={ faAcorn } style={ styles.icon }/>).toJSON()
248277
const primaryLayer = tree.children[0].children[0].children[1]
249278
const secondaryLayer = tree.children[0].children[0].children[0]
250-
expect(getActualFillColorHex(primaryLayer)).toEqual('ff0000ff')
279+
expect(getActualFillColorHex(primaryLayer)).toEqual(blueHex)
251280
expect(secondaryLayer.props.fillOpacity).toEqual(0.4)
252281
})
253282
})
@@ -261,7 +290,7 @@ describe('duotone support', () => {
261290
const tree = renderer.create(<FontAwesomeIcon icon={ faAcorn } style={ styles.icon } secondaryOpacity={ 0.123 } />).toJSON()
262291
const primaryLayer = tree.children[0].children[0].children[1]
263292
const secondaryLayer = tree.children[0].children[0].children[0]
264-
expect(getActualFillColorHex(primaryLayer)).toEqual('ff0000ff')
293+
expect(getActualFillColorHex(primaryLayer)).toEqual(blueHex)
265294
expect(secondaryLayer.props.fillOpacity).toEqual(0.123)
266295
})
267296
})
@@ -274,7 +303,7 @@ describe('duotone support', () => {
274303
})
275304
const tree = renderer.create(<FontAwesomeIcon icon={ faAcorn } style={ styles.icon } secondaryColor={ "red" } />).toJSON()
276305
const secondaryLayer = tree.children[0].children[0].children[0]
277-
expect(getActualFillColorHex(secondaryLayer)).toEqual('ffff0000')
306+
expect(getActualFillColorHex(secondaryLayer)).toEqual(redHex)
278307
expect(secondaryLayer.props.fillOpacity).toEqual(1)
279308
})
280309
})
@@ -287,7 +316,7 @@ describe('duotone support', () => {
287316
})
288317
const tree = renderer.create(<FontAwesomeIcon icon={ faAcorn } style={ styles.icon } secondaryColor={ "red" } secondaryOpacity={ 0.123 } />).toJSON()
289318
const secondaryLayer = tree.children[0].children[0].children[0]
290-
expect(getActualFillColorHex(secondaryLayer)).toEqual('ffff0000')
319+
expect(getActualFillColorHex(secondaryLayer)).toEqual(redHex)
291320
expect(secondaryLayer.props.fillOpacity).toEqual(0.123)
292321
})
293322
})

0 commit comments

Comments
 (0)