1
1
import React from 'react' ;
2
2
import { render } from '@testing-library/react-native' ;
3
3
import { View } from 'react-native' ;
4
+ import { ThemeProvider } from '../../../src/core/theme/context' ;
4
5
import WithThemeProvider from '../../support/withThemeProvider' ;
6
+ import extendThemeConfig from '../../../src/core/theme/extendThemeConfig' ;
5
7
6
8
import Icon from '../../../src/components/main/Icon' ;
7
9
@@ -11,6 +13,17 @@ const FakeBaseIcon = props => (
11
13
< View { ...props } />
12
14
) ;
13
15
16
+ const hideConsoleErrors = ( callback ) => {
17
+ /* eslint-disable no-console */
18
+ const originalConsoleError = console . error ;
19
+ console . error = jest . fn ( ) ;
20
+
21
+ callback ( ) ;
22
+
23
+ console . error = originalConsoleError ;
24
+ /* eslint-enable no-console */
25
+ } ;
26
+
14
27
describe ( 'Icon' , ( ) => {
15
28
const renderComponent = props => render (
16
29
< Icon testID = "test-icon" name = "test" as = { FakeBaseIcon } { ...props } /> ,
@@ -35,6 +48,36 @@ describe( 'Icon', () => {
35
48
expect ( getByTestId ( 'test-icon' ) ) . toHaveProp ( 'color' , '#999AB8' ) ;
36
49
} ) ;
37
50
51
+ it ( 'renders normally when the `as` prop is not provided but there is a default prop for it' , ( ) => {
52
+ const theme = extendThemeConfig ( {
53
+ components : {
54
+ Icon : {
55
+ defaultProps : {
56
+ as : FakeBaseIcon
57
+ }
58
+ }
59
+ }
60
+ } ) ;
61
+
62
+ const { getByTestId } = render (
63
+ < ThemeProvider theme = { theme } >
64
+ < Icon name = "test" testID = "test-icon" />
65
+ </ ThemeProvider >
66
+ ) ;
67
+
68
+ expect ( getByTestId ( 'test-icon' ) ) . toHaveProp ( 'name' , 'test' ) ;
69
+ } ) ;
70
+
71
+ it ( 'throws an error when the `as` prop is not provided and there is not default prop for it' , ( ) => hideConsoleErrors (
72
+ ( ) => {
73
+ expect ( ( ) => render ( < Icon name = "test" /> ) )
74
+ . toThrow (
75
+ 'The `as` prop is required for the Icon component. You must supply it in the component '
76
+ + 'props or in the Theme config as the component\'s default props.'
77
+ ) ;
78
+ } )
79
+ ) ;
80
+
38
81
itBehavesLike (
39
82
'aStyledSystemComponent' ,
40
83
{
0 commit comments