|
| 1 | +import * as React from 'react'; |
| 2 | +import { View, Text, Pressable } from 'react-native'; |
| 3 | +import { render, fireEvent } from '..'; |
| 4 | + |
| 5 | +// eslint-disable-next-line no-console |
| 6 | +const originalConsoleError = console.error; |
| 7 | + |
| 8 | +const VALIDATION_ERROR = |
| 9 | + 'Invariant Violation: Text strings must be rendered within a <Text> component'; |
| 10 | +const PROFILER_ERROR = 'The above error occurred in the <Profiler> component'; |
| 11 | + |
| 12 | +beforeEach(() => { |
| 13 | + // eslint-disable-next-line no-console |
| 14 | + console.error = (errorMessage: string) => { |
| 15 | + if (!errorMessage.includes(PROFILER_ERROR)) { |
| 16 | + originalConsoleError(errorMessage); |
| 17 | + } |
| 18 | + }; |
| 19 | +}); |
| 20 | + |
| 21 | +afterEach(() => { |
| 22 | + // eslint-disable-next-line no-console |
| 23 | + console.error = originalConsoleError; |
| 24 | +}); |
| 25 | + |
| 26 | +test('should throw when rendering a string outside a text component', () => { |
| 27 | + expect(() => |
| 28 | + render(<View>hello</View>, { |
| 29 | + unstable_validateStringsRenderedWithinText: true, |
| 30 | + }) |
| 31 | + ).toThrow( |
| 32 | + `${VALIDATION_ERROR}. Detected attempt to render "hello" string within a <View> component.` |
| 33 | + ); |
| 34 | +}); |
| 35 | + |
| 36 | +test('should throw an error when rerendering with text outside of Text component', () => { |
| 37 | + const { rerender } = render(<View />, { |
| 38 | + unstable_validateStringsRenderedWithinText: true, |
| 39 | + }); |
| 40 | + |
| 41 | + expect(() => rerender(<View>hello</View>)).toThrow( |
| 42 | + `${VALIDATION_ERROR}. Detected attempt to render "hello" string within a <View> component.` |
| 43 | + ); |
| 44 | +}); |
| 45 | + |
| 46 | +const InvalidTextAfterPress = () => { |
| 47 | + const [showText, setShowText] = React.useState(false); |
| 48 | + |
| 49 | + if (!showText) { |
| 50 | + return ( |
| 51 | + <Pressable onPress={() => setShowText(true)}> |
| 52 | + <Text>Show text</Text> |
| 53 | + </Pressable> |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + return <View>text rendered outside text component</View>; |
| 58 | +}; |
| 59 | + |
| 60 | +test('should throw an error when strings are rendered outside Text', () => { |
| 61 | + const { getByText } = render(<InvalidTextAfterPress />, { |
| 62 | + unstable_validateStringsRenderedWithinText: true, |
| 63 | + }); |
| 64 | + |
| 65 | + expect(() => fireEvent.press(getByText('Show text'))).toThrow( |
| 66 | + `${VALIDATION_ERROR}. Detected attempt to render "text rendered outside text component" string within a <View> component.` |
| 67 | + ); |
| 68 | +}); |
| 69 | + |
| 70 | +test('should not throw for texts nested in fragments', () => { |
| 71 | + expect(() => |
| 72 | + render( |
| 73 | + <Text> |
| 74 | + <>hello</> |
| 75 | + </Text>, |
| 76 | + { unstable_validateStringsRenderedWithinText: true } |
| 77 | + ) |
| 78 | + ).not.toThrow(); |
| 79 | +}); |
| 80 | + |
| 81 | +test('should not throw if option validateRenderedString is false', () => { |
| 82 | + expect(() => render(<View>hello</View>)).not.toThrow(); |
| 83 | +}); |
| 84 | + |
| 85 | +test(`should throw when one of the children is a text and the parent is not a Text component`, () => { |
| 86 | + expect(() => |
| 87 | + render( |
| 88 | + <View> |
| 89 | + <Text>hello</Text> |
| 90 | + hello |
| 91 | + </View>, |
| 92 | + { unstable_validateStringsRenderedWithinText: true } |
| 93 | + ) |
| 94 | + ).toThrow( |
| 95 | + `${VALIDATION_ERROR}. Detected attempt to render "hello" string within a <View> component.` |
| 96 | + ); |
| 97 | +}); |
| 98 | + |
| 99 | +test(`should throw when a string is rendered within a fragment rendered outside a Text`, () => { |
| 100 | + expect(() => |
| 101 | + render( |
| 102 | + <View> |
| 103 | + <>hello</> |
| 104 | + </View>, |
| 105 | + { unstable_validateStringsRenderedWithinText: true } |
| 106 | + ) |
| 107 | + ).toThrow( |
| 108 | + `${VALIDATION_ERROR}. Detected attempt to render "hello" string within a <View> component.` |
| 109 | + ); |
| 110 | +}); |
| 111 | + |
| 112 | +test('should throw if a number is rendered outside a text', () => { |
| 113 | + expect(() => |
| 114 | + render(<View>0</View>, { unstable_validateStringsRenderedWithinText: true }) |
| 115 | + ).toThrow( |
| 116 | + `${VALIDATION_ERROR}. Detected attempt to render "0" string within a <View> component.` |
| 117 | + ); |
| 118 | +}); |
| 119 | + |
| 120 | +const Trans = ({ i18nKey }: { i18nKey: string }) => <>{i18nKey}</>; |
| 121 | + |
| 122 | +test('should throw with components returning string value not rendered in Text', () => { |
| 123 | + expect(() => |
| 124 | + render( |
| 125 | + <View> |
| 126 | + <Trans i18nKey="hello" /> |
| 127 | + </View>, |
| 128 | + { unstable_validateStringsRenderedWithinText: true } |
| 129 | + ) |
| 130 | + ).toThrow( |
| 131 | + `${VALIDATION_ERROR}. Detected attempt to render "hello" string within a <View> component.` |
| 132 | + ); |
| 133 | +}); |
| 134 | + |
| 135 | +test('should not throw with components returning string value rendered in Text', () => { |
| 136 | + expect(() => |
| 137 | + render( |
| 138 | + <Text> |
| 139 | + <Trans i18nKey="hello" /> |
| 140 | + </Text>, |
| 141 | + { unstable_validateStringsRenderedWithinText: true } |
| 142 | + ) |
| 143 | + ).not.toThrow(); |
| 144 | +}); |
| 145 | + |
| 146 | +test('should throw when rendering string in a View in a Text', () => { |
| 147 | + expect(() => |
| 148 | + render( |
| 149 | + <Text> |
| 150 | + <View>hello</View> |
| 151 | + </Text>, |
| 152 | + { unstable_validateStringsRenderedWithinText: true } |
| 153 | + ) |
| 154 | + ).toThrow( |
| 155 | + `${VALIDATION_ERROR}. Detected attempt to render "hello" string within a <View> component.` |
| 156 | + ); |
| 157 | +}); |
0 commit comments