|
1 | | -import { Animated, BackHandler as RNBackHandler, Text } from 'react-native'; |
| 1 | +import { |
| 2 | + Animated, |
| 3 | + BackHandler as RNBackHandler, |
| 4 | + DeviceEventEmitter, |
| 5 | + Dimensions, |
| 6 | + Platform, |
| 7 | + Text, |
| 8 | +} from 'react-native'; |
2 | 9 | import type { BackHandlerStatic as RNBackHandlerStatic } from 'react-native'; |
3 | 10 |
|
4 | 11 | import { afterAll, beforeAll, describe, expect, it, jest } from '@jest/globals'; |
5 | | -import { act, userEvent } from '@testing-library/react-native'; |
| 12 | +import { act, fireEvent, userEvent } from '@testing-library/react-native'; |
6 | 13 |
|
7 | 14 | import { render, screen } from '../../test-utils'; |
8 | 15 | import { LightTheme } from '../../theme/schemes'; |
@@ -575,6 +582,186 @@ describe('Modal', () => { |
575 | 582 | }); |
576 | 583 | }); |
577 | 584 |
|
| 585 | + // `keyboardWillShow` and `keyboardWillHide` are not emitted on Android |
| 586 | + const keyboardEventsPerPlatform: Array<[typeof Platform.OS, string, string]> = |
| 587 | + [ |
| 588 | + ['ios', 'keyboardWillShow', 'keyboardWillHide'], |
| 589 | + ['android', 'keyboardDidShow', 'keyboardDidHide'], |
| 590 | + ]; |
| 591 | + |
| 592 | + describe.each(keyboardEventsPerPlatform)( |
| 593 | + 'when the on-screen keyboard is shown on %s', |
| 594 | + (platform, showEvent, hideEvent) => { |
| 595 | + const KEYBOARD_HEIGHT = 300; |
| 596 | + const TOP_INSET = 37; |
| 597 | + const BOTTOM_INSET = 44; |
| 598 | + |
| 599 | + const screenHeight = Dimensions.get('screen').height; |
| 600 | + // Space taken by the keyboard below the bottom edge of the wrapper |
| 601 | + const expectedOverlap = KEYBOARD_HEIGHT - BOTTOM_INSET; |
| 602 | + const originalPlatform = Platform.OS; |
| 603 | + |
| 604 | + beforeAll(() => { |
| 605 | + Platform.OS = platform; |
| 606 | + }); |
| 607 | + |
| 608 | + afterAll(() => { |
| 609 | + Platform.OS = originalPlatform; |
| 610 | + }); |
| 611 | + |
| 612 | + const showKeyboard = async () => { |
| 613 | + await act(() => { |
| 614 | + DeviceEventEmitter.emit(showEvent, { |
| 615 | + endCoordinates: { |
| 616 | + screenX: 0, |
| 617 | + screenY: screenHeight - KEYBOARD_HEIGHT, |
| 618 | + width: 750, |
| 619 | + height: KEYBOARD_HEIGHT, |
| 620 | + }, |
| 621 | + }); |
| 622 | + }); |
| 623 | + }; |
| 624 | + |
| 625 | + const hideKeyboard = async () => { |
| 626 | + await act(() => { |
| 627 | + DeviceEventEmitter.emit(hideEvent, { |
| 628 | + endCoordinates: { |
| 629 | + screenX: 0, |
| 630 | + screenY: screenHeight, |
| 631 | + width: 750, |
| 632 | + height: 0, |
| 633 | + }, |
| 634 | + }); |
| 635 | + }); |
| 636 | + }; |
| 637 | + |
| 638 | + const layoutWrapper = async (y: number, height: number) => { |
| 639 | + await act(() => |
| 640 | + fireEvent(screen.getByTestId('modal-wrapper'), 'layout', { |
| 641 | + nativeEvent: { layout: { x: 0, y, width: 750, height } }, |
| 642 | + }) |
| 643 | + ); |
| 644 | + }; |
| 645 | + |
| 646 | + it('should keep the content above the keyboard if the window is not resized', async () => { |
| 647 | + await render( |
| 648 | + <Modal visible={true} testID="modal"> |
| 649 | + {null} |
| 650 | + </Modal> |
| 651 | + ); |
| 652 | + |
| 653 | + expect(screen.getByTestId('modal-wrapper')).toHaveStyle({ |
| 654 | + paddingBottom: 0, |
| 655 | + }); |
| 656 | + |
| 657 | + await layoutWrapper(TOP_INSET, screenHeight - TOP_INSET - BOTTOM_INSET); |
| 658 | + await showKeyboard(); |
| 659 | + |
| 660 | + expect(screen.getByTestId('modal-wrapper')).toHaveStyle({ |
| 661 | + paddingBottom: expectedOverlap, |
| 662 | + }); |
| 663 | + }); |
| 664 | + |
| 665 | + it('should account for a keyboard which is already open when it becomes visible', async () => { |
| 666 | + // `Keyboard.metrics()` is populated from `keyboardDidShow` on every platform |
| 667 | + await act(() => { |
| 668 | + DeviceEventEmitter.emit('keyboardDidShow', { |
| 669 | + endCoordinates: { |
| 670 | + screenX: 0, |
| 671 | + screenY: screenHeight - KEYBOARD_HEIGHT, |
| 672 | + width: 750, |
| 673 | + height: KEYBOARD_HEIGHT, |
| 674 | + }, |
| 675 | + }); |
| 676 | + }); |
| 677 | + |
| 678 | + const { rerender } = await render( |
| 679 | + <Modal visible={false} testID="modal"> |
| 680 | + {null} |
| 681 | + </Modal> |
| 682 | + ); |
| 683 | + |
| 684 | + await rerender( |
| 685 | + <Modal visible={true} testID="modal"> |
| 686 | + {null} |
| 687 | + </Modal> |
| 688 | + ); |
| 689 | + |
| 690 | + await layoutWrapper(TOP_INSET, screenHeight - TOP_INSET - BOTTOM_INSET); |
| 691 | + |
| 692 | + expect(screen.getByTestId('modal-wrapper')).toHaveStyle({ |
| 693 | + paddingBottom: expectedOverlap, |
| 694 | + }); |
| 695 | + |
| 696 | + await act(() => { |
| 697 | + DeviceEventEmitter.emit('keyboardDidHide', { |
| 698 | + endCoordinates: { |
| 699 | + screenX: 0, |
| 700 | + screenY: screenHeight, |
| 701 | + width: 750, |
| 702 | + height: 0, |
| 703 | + }, |
| 704 | + }); |
| 705 | + }); |
| 706 | + }); |
| 707 | + |
| 708 | + it('should not add any padding if the window is resized by the system', async () => { |
| 709 | + await render( |
| 710 | + <Modal visible={true} testID="modal"> |
| 711 | + {null} |
| 712 | + </Modal> |
| 713 | + ); |
| 714 | + |
| 715 | + // The system shrunk the window, so the wrapper is already above the keyboard |
| 716 | + await layoutWrapper( |
| 717 | + TOP_INSET, |
| 718 | + screenHeight - TOP_INSET - BOTTOM_INSET - KEYBOARD_HEIGHT |
| 719 | + ); |
| 720 | + await showKeyboard(); |
| 721 | + |
| 722 | + expect(screen.getByTestId('modal-wrapper')).toHaveStyle({ |
| 723 | + paddingBottom: 0, |
| 724 | + }); |
| 725 | + }); |
| 726 | + |
| 727 | + it('should follow the wrapper if the safe area insets are overridden', async () => { |
| 728 | + await render( |
| 729 | + <Modal visible={true} testID="modal" style={{ marginBottom: 0 }}> |
| 730 | + {null} |
| 731 | + </Modal> |
| 732 | + ); |
| 733 | + |
| 734 | + await layoutWrapper(TOP_INSET, screenHeight - TOP_INSET); |
| 735 | + await showKeyboard(); |
| 736 | + |
| 737 | + expect(screen.getByTestId('modal-wrapper')).toHaveStyle({ |
| 738 | + paddingBottom: KEYBOARD_HEIGHT, |
| 739 | + }); |
| 740 | + }); |
| 741 | + |
| 742 | + it('should restore the original padding once the keyboard is hidden', async () => { |
| 743 | + await render( |
| 744 | + <Modal visible={true} testID="modal"> |
| 745 | + {null} |
| 746 | + </Modal> |
| 747 | + ); |
| 748 | + |
| 749 | + await layoutWrapper(TOP_INSET, screenHeight - TOP_INSET - BOTTOM_INSET); |
| 750 | + await showKeyboard(); |
| 751 | + |
| 752 | + expect(screen.getByTestId('modal-wrapper')).toHaveStyle({ |
| 753 | + paddingBottom: expectedOverlap, |
| 754 | + }); |
| 755 | + |
| 756 | + await hideKeyboard(); |
| 757 | + |
| 758 | + expect(screen.getByTestId('modal-wrapper')).toHaveStyle({ |
| 759 | + paddingBottom: 0, |
| 760 | + }); |
| 761 | + }); |
| 762 | + } |
| 763 | + ); |
| 764 | + |
578 | 765 | it('animated value changes correctly', async () => { |
579 | 766 | const value = new Animated.Value(1); |
580 | 767 | await render( |
|
0 commit comments