|
1 | 1 | import * as React from 'react'; |
2 | | -import { StyleSheet } from 'react-native'; |
| 2 | +import { StyleSheet, Text, View } from 'react-native'; |
3 | 3 | import { fireEvent, render } from 'react-native-testing-library'; |
4 | 4 | import TextInput from '../TextInput/TextInput'; |
5 | | -import { red500 } from '../../styles/colors'; |
| 5 | +import { areLabelsEqual } from '../TextInput/helpers'; |
| 6 | +import { blue500, red500 } from '../../styles/colors'; |
6 | 7 |
|
7 | 8 | const style = StyleSheet.create({ |
8 | 9 | inputStyle: { |
@@ -148,3 +149,117 @@ it('correctly applies focused state Outline TextInput', () => { |
148 | 149 | expect.arrayContaining([expect.objectContaining({ borderWidth: 2 })]) |
149 | 150 | ); |
150 | 151 | }); |
| 152 | + |
| 153 | +it('correctly applies a component as the text label', () => { |
| 154 | + const { toJSON } = render( |
| 155 | + <TextInput |
| 156 | + label={<Text style={style.inputStyle}>Flat input</Text>} |
| 157 | + placeholder="Type something" |
| 158 | + value={'Some test value'} |
| 159 | + /> |
| 160 | + ); |
| 161 | + |
| 162 | + expect(toJSON()).toMatchSnapshot(); |
| 163 | +}); |
| 164 | + |
| 165 | +it('correctly compares labels when both are string', () => { |
| 166 | + expect(areLabelsEqual('Comments', 'Comments')).toBe(true); |
| 167 | + expect(areLabelsEqual('Comments', 'No Comment')).toBe(false); |
| 168 | +}); |
| 169 | + |
| 170 | +it('correctly compares labels when one is string and one is a component', () => { |
| 171 | + expect(areLabelsEqual(<Text>Comments</Text>, 'Comments')).toBe(false); |
| 172 | +}); |
| 173 | + |
| 174 | +it('correctly compares labels when both labels are falsy', () => { |
| 175 | + // We're treating all falsy values as equivalent |
| 176 | + expect(areLabelsEqual()).toBe(true); |
| 177 | + expect(areLabelsEqual(undefined, undefined)).toBe(true); |
| 178 | + expect(areLabelsEqual(null, null)).toBe(true); |
| 179 | + expect(areLabelsEqual(undefined, '')).toBe(true); |
| 180 | + expect(areLabelsEqual(null, '')).toBe(true); |
| 181 | + expect(areLabelsEqual(undefined, null)).toBe(true); |
| 182 | +}); |
| 183 | + |
| 184 | +it('correctly compares labels when both labels are components', () => { |
| 185 | + // Same component; same props, same children |
| 186 | + const component1 = <Text>Comments</Text>; |
| 187 | + |
| 188 | + let component2 = <Text>Comments</Text>; |
| 189 | + expect(areLabelsEqual(component1, component2)).toBe(true); |
| 190 | + |
| 191 | + // Same component; same props, different children |
| 192 | + component2 = <Text>Another Comment</Text>; |
| 193 | + expect(areLabelsEqual(component1, component2)).toBe(false); |
| 194 | + |
| 195 | + // Different component; same props, same children |
| 196 | + component2 = <View>Comments</View>; |
| 197 | + expect(areLabelsEqual(component1, component2)).toBe(false); |
| 198 | + |
| 199 | + // Same component; different props, same children |
| 200 | + component2 = ( |
| 201 | + <Text multiline style={{ color: red500 }}> |
| 202 | + Comments |
| 203 | + </Text> |
| 204 | + ); |
| 205 | + expect(areLabelsEqual(component1, component2)).toBe(false); |
| 206 | +}); |
| 207 | + |
| 208 | +it('correctly compares labels for nested components', () => { |
| 209 | + // Same component; same props, same children |
| 210 | + const component1 = ( |
| 211 | + <Text> |
| 212 | + <Text style={{ color: red500 }}>*</Text> Comments |
| 213 | + </Text> |
| 214 | + ); |
| 215 | + |
| 216 | + let component2 = ( |
| 217 | + <Text> |
| 218 | + <Text style={{ color: red500 }}>*</Text> Comments |
| 219 | + </Text> |
| 220 | + ); |
| 221 | + expect(areLabelsEqual(component1, component2)).toBe(true); |
| 222 | + |
| 223 | + // Same component; same props, different children |
| 224 | + component2 = ( |
| 225 | + <Text> |
| 226 | + <Text style={{ color: red500 }}>Comments</Text> continues |
| 227 | + </Text> |
| 228 | + ); |
| 229 | + expect(areLabelsEqual(component1, component2)).toBe(false); |
| 230 | + |
| 231 | + // Different component; same props, same children |
| 232 | + component2 = ( |
| 233 | + <View> |
| 234 | + <Text style={{ color: red500 }}>*</Text> Comments |
| 235 | + </View> |
| 236 | + ); |
| 237 | + expect(areLabelsEqual(component1, component2)).toBe(false); |
| 238 | + |
| 239 | + // Same component; different props, same children |
| 240 | + component2 = ( |
| 241 | + <Text multiline> |
| 242 | + <Text style={{ color: red500 }}>*</Text> Comments |
| 243 | + </Text> |
| 244 | + ); |
| 245 | + expect(areLabelsEqual(component1, component2)).toBe(false); |
| 246 | + |
| 247 | + // Same component; same props, different number of children |
| 248 | + component2 = ( |
| 249 | + <Text> |
| 250 | + <Text style={{ color: red500 }}>*</Text> |
| 251 | + </Text> |
| 252 | + ); |
| 253 | + expect(areLabelsEqual(component1, component2)).toBe(false); |
| 254 | + |
| 255 | + // Same component; different props in inner component, same children |
| 256 | + component2 = ( |
| 257 | + <Text> |
| 258 | + <Text multiline style={{ color: blue500 }}> |
| 259 | + * |
| 260 | + </Text>{' '} |
| 261 | + Comments |
| 262 | + </Text> |
| 263 | + ); |
| 264 | + expect(areLabelsEqual(component1, component2)).toBe(false); |
| 265 | +}); |
0 commit comments