|
1 | 1 | import * as React from 'react'; |
2 | | -import { TextInput, View } from 'react-native'; |
| 2 | +import { I18nManager, StyleSheet, TextInput, View } from 'react-native'; |
3 | 3 |
|
4 | 4 | import { fireEvent, render } from '@testing-library/react-native'; |
5 | 5 |
|
6 | 6 | import TextField, { |
7 | 7 | type TextFieldAccessoryProps, |
8 | 8 | } from '../TextField/TextField'; |
9 | 9 |
|
| 10 | +const defaultI18nIsRTL = I18nManager.isRTL; |
| 11 | + |
| 12 | +afterEach(() => { |
| 13 | + I18nManager.isRTL = defaultI18nIsRTL; |
| 14 | +}); |
| 15 | + |
| 16 | +function firstIndexOfTestIdInTree(tree: unknown, testID: string): number { |
| 17 | + const serialized = JSON.stringify(tree); |
| 18 | + const match = new RegExp(`"testID":\\s*"${testID}"`).exec(serialized); |
| 19 | + return match ? match.index : -1; |
| 20 | +} |
| 21 | + |
10 | 22 | it('renders filled TextField with label and value', () => { |
11 | 23 | const tree = render( |
12 | 24 | <TextField label="Email" value="a@b.co" onChangeText={() => {}} /> |
@@ -277,3 +289,162 @@ it('applies helperProps to the helper Text', () => { |
277 | 289 |
|
278 | 290 | expect(getByTestId('helper-text').props.children).toBe('Hint'); |
279 | 291 | }); |
| 292 | + |
| 293 | +it('applies RTL text alignment and writing direction to the TextInput (filled)', () => { |
| 294 | + I18nManager.isRTL = true; |
| 295 | + |
| 296 | + const { getByTestId } = render( |
| 297 | + <TextField |
| 298 | + label="Email" |
| 299 | + value="x" |
| 300 | + onChangeText={() => {}} |
| 301 | + testID="tf-input-rtl" |
| 302 | + /> |
| 303 | + ); |
| 304 | + |
| 305 | + expect(StyleSheet.flatten(getByTestId('tf-input-rtl').props.style)).toEqual( |
| 306 | + expect.objectContaining({ |
| 307 | + textAlign: 'right', |
| 308 | + writingDirection: 'rtl', |
| 309 | + }) |
| 310 | + ); |
| 311 | +}); |
| 312 | + |
| 313 | +it('applies RTL text alignment and writing direction to the TextInput (outlined)', () => { |
| 314 | + I18nManager.isRTL = true; |
| 315 | + |
| 316 | + const { getByTestId } = render( |
| 317 | + <TextField |
| 318 | + variant="outlined" |
| 319 | + label="Email" |
| 320 | + value="x" |
| 321 | + onChangeText={() => {}} |
| 322 | + testID="tf-input-rtl-outlined" |
| 323 | + /> |
| 324 | + ); |
| 325 | + |
| 326 | + expect( |
| 327 | + StyleSheet.flatten(getByTestId('tf-input-rtl-outlined').props.style) |
| 328 | + ).toEqual( |
| 329 | + expect.objectContaining({ |
| 330 | + textAlign: 'right', |
| 331 | + writingDirection: 'rtl', |
| 332 | + }) |
| 333 | + ); |
| 334 | +}); |
| 335 | + |
| 336 | +it('applies RTL writing direction to helper text', () => { |
| 337 | + I18nManager.isRTL = true; |
| 338 | + |
| 339 | + const { getByTestId } = render( |
| 340 | + <TextField |
| 341 | + label="Email" |
| 342 | + value="" |
| 343 | + onChangeText={() => {}} |
| 344 | + helper="Hint" |
| 345 | + helperProps={{ testID: 'helper-rtl' }} |
| 346 | + /> |
| 347 | + ); |
| 348 | + |
| 349 | + expect(StyleSheet.flatten(getByTestId('helper-rtl').props.style)).toEqual( |
| 350 | + expect.objectContaining({ |
| 351 | + writingDirection: 'rtl', |
| 352 | + }) |
| 353 | + ); |
| 354 | +}); |
| 355 | + |
| 356 | +it('places RightAccessory before LeftAccessory in the tree when RTL', () => { |
| 357 | + I18nManager.isRTL = true; |
| 358 | + |
| 359 | + function LeftAccessory() { |
| 360 | + return <View testID="rtl-acc-from-left-prop" />; |
| 361 | + } |
| 362 | + |
| 363 | + function RightAccessory() { |
| 364 | + return <View testID="rtl-acc-from-right-prop" />; |
| 365 | + } |
| 366 | + |
| 367 | + const { toJSON } = render( |
| 368 | + <TextField |
| 369 | + label="Email" |
| 370 | + value="" |
| 371 | + onChangeText={() => {}} |
| 372 | + LeftAccessory={LeftAccessory} |
| 373 | + RightAccessory={RightAccessory} |
| 374 | + testID="tf-input-rtl-order" |
| 375 | + /> |
| 376 | + ); |
| 377 | + |
| 378 | + const tree = toJSON(); |
| 379 | + expect( |
| 380 | + firstIndexOfTestIdInTree(tree, 'rtl-acc-from-right-prop') |
| 381 | + ).toBeLessThan(firstIndexOfTestIdInTree(tree, 'rtl-acc-from-left-prop')); |
| 382 | +}); |
| 383 | + |
| 384 | +it('places LeftAccessory before RightAccessory in the tree when LTR', () => { |
| 385 | + I18nManager.isRTL = false; |
| 386 | + |
| 387 | + function LeftAccessory() { |
| 388 | + return <View testID="ltr-acc-from-left-prop" />; |
| 389 | + } |
| 390 | + |
| 391 | + function RightAccessory() { |
| 392 | + return <View testID="ltr-acc-from-right-prop" />; |
| 393 | + } |
| 394 | + |
| 395 | + const { toJSON } = render( |
| 396 | + <TextField |
| 397 | + label="Email" |
| 398 | + value="" |
| 399 | + onChangeText={() => {}} |
| 400 | + LeftAccessory={LeftAccessory} |
| 401 | + RightAccessory={RightAccessory} |
| 402 | + testID="tf-input-ltr-order" |
| 403 | + /> |
| 404 | + ); |
| 405 | + |
| 406 | + const tree = toJSON(); |
| 407 | + expect(firstIndexOfTestIdInTree(tree, 'ltr-acc-from-left-prop')).toBeLessThan( |
| 408 | + firstIndexOfTestIdInTree(tree, 'ltr-acc-from-right-prop') |
| 409 | + ); |
| 410 | +}); |
| 411 | + |
| 412 | +it('maps a lone LeftAccessory to leading in LTR and trailing in RTL (tree order)', () => { |
| 413 | + function LoneLeftAccessory() { |
| 414 | + return <View testID="lone-left-acc" />; |
| 415 | + } |
| 416 | + |
| 417 | + I18nManager.isRTL = false; |
| 418 | + |
| 419 | + const { toJSON: toJsonLtr } = render( |
| 420 | + <TextField |
| 421 | + label="Email" |
| 422 | + value="" |
| 423 | + onChangeText={() => {}} |
| 424 | + LeftAccessory={LoneLeftAccessory} |
| 425 | + testID="tf-lone-ltr" |
| 426 | + /> |
| 427 | + ); |
| 428 | + |
| 429 | + I18nManager.isRTL = true; |
| 430 | + |
| 431 | + const { toJSON: toJsonRtl } = render( |
| 432 | + <TextField |
| 433 | + label="Email" |
| 434 | + value="" |
| 435 | + onChangeText={() => {}} |
| 436 | + LeftAccessory={LoneLeftAccessory} |
| 437 | + testID="tf-lone-rtl" |
| 438 | + /> |
| 439 | + ); |
| 440 | + |
| 441 | + const ltrTree = toJsonLtr(); |
| 442 | + expect(firstIndexOfTestIdInTree(ltrTree, 'lone-left-acc')).toBeLessThan( |
| 443 | + firstIndexOfTestIdInTree(ltrTree, 'tf-lone-ltr') |
| 444 | + ); |
| 445 | + |
| 446 | + const rtlTree = toJsonRtl(); |
| 447 | + expect(firstIndexOfTestIdInTree(rtlTree, 'tf-lone-rtl')).toBeLessThan( |
| 448 | + firstIndexOfTestIdInTree(rtlTree, 'lone-left-acc') |
| 449 | + ); |
| 450 | +}); |
0 commit comments