|
1 | | -import { Text } from "react-native"; |
| 1 | +import { AccessibilityInfo, Modal, Platform, Text } from "react-native"; |
2 | 2 | import { fireEvent, render, act, RenderAPI } from "@testing-library/react-native"; |
3 | 3 | import { NativeIcon, NativeImage } from "mendix"; |
4 | 4 | import { Style } from "@mendix/piw-native-utils-internal"; |
@@ -29,6 +29,17 @@ async function fireLayout(image: RenderAPI, name: string, suffix: string): Promi |
29 | 29 | fireEvent(await findByImageTestId(image, name, suffix), "layout", onLayoutEventData); |
30 | 30 | } |
31 | 31 |
|
| 32 | +const mockFindNodeHandle = jest.fn(); |
| 33 | +jest.mock("react-native/Libraries/ReactNative/RendererProxy", () => { |
| 34 | + const actual = jest.requireActual("react-native/Libraries/ReactNative/RendererProxy"); |
| 35 | + return { |
| 36 | + ...actual, |
| 37 | + // Only intercepted while a test installs an implementation, so other tests keep the real behaviour |
| 38 | + findNodeHandle: (node: unknown) => |
| 39 | + mockFindNodeHandle.getMockImplementation() ? mockFindNodeHandle(node) : actual.findNodeHandle(node) |
| 40 | + }; |
| 41 | +}); |
| 42 | + |
32 | 43 | jest.mock("../../utils/imageUtils", () => { |
33 | 44 | const actual = jest.requireActual("../../utils/imageUtils") as typeof import("../../utils/imageUtils"); |
34 | 45 |
|
@@ -667,4 +678,146 @@ describe("Widget", () => { |
667 | 678 | }); |
668 | 679 | }); |
669 | 680 | }); |
| 681 | + |
| 682 | + describe("Enlarged overlay", () => { |
| 683 | + const originalOS = Platform.OS; |
| 684 | + let setFocus: jest.SpyInstance; |
| 685 | + let nodeOf: (tag: number) => { props: { testID?: string } }; |
| 686 | + |
| 687 | + const openEnlarged = async (props: Partial<ImageProps<Style>> = {}): Promise<RenderAPI> => { |
| 688 | + const image = render(<Image {...imageProps} onClickType={"enlarge"} {...props} />); |
| 689 | + await flushMicrotasksQueue(); |
| 690 | + fireEvent.press(await findByImageTestId(image, imageProps.name, "ImageSmallPressable")); |
| 691 | + await findByImageTestId(image, imageProps.name, "ImageEnlargedPressable"); |
| 692 | + return image; |
| 693 | + }; |
| 694 | + |
| 695 | + // Closing plays the exit animation, so fake timers are switched on right before closing to run it to the end |
| 696 | + const closeWithFakeTimers = (close: () => void): void => { |
| 697 | + jest.useFakeTimers(); |
| 698 | + act(() => close()); |
| 699 | + act(() => { |
| 700 | + jest.advanceTimersByTime(500); |
| 701 | + }); |
| 702 | + }; |
| 703 | + |
| 704 | + beforeEach(() => { |
| 705 | + setFocus = jest.spyOn(AccessibilityInfo, "setAccessibilityFocus").mockImplementation(() => undefined); |
| 706 | + // Numeric tags (as on a device) that can be mapped back, to see which element got focus |
| 707 | + const tags = new Map<unknown, number>(); |
| 708 | + const nodes = new Map<number, { props: { testID?: string } }>(); |
| 709 | + mockFindNodeHandle.mockReset().mockImplementation((node: { props: { testID?: string } } | null) => { |
| 710 | + if (!node) { |
| 711 | + return null; |
| 712 | + } |
| 713 | + if (!tags.has(node)) { |
| 714 | + tags.set(node, tags.size + 1); |
| 715 | + nodes.set(tags.size, node); |
| 716 | + } |
| 717 | + return tags.get(node); |
| 718 | + }); |
| 719 | + nodeOf = tag => nodes.get(tag)!; |
| 720 | + }); |
| 721 | + |
| 722 | + afterEach(() => { |
| 723 | + jest.useRealTimers(); |
| 724 | + (Platform as { OS: string }).OS = originalOS; |
| 725 | + mockFindNodeHandle.mockReset(); |
| 726 | + jest.restoreAllMocks(); |
| 727 | + }); |
| 728 | + |
| 729 | + it("is not shown before the image is pressed", async () => { |
| 730 | + const image = render(<Image {...imageProps} onClickType={"enlarge"} />); |
| 731 | + await flushMicrotasksQueue(); |
| 732 | + |
| 733 | + expect(image.UNSAFE_queryByType(Modal)).toBeNull(); |
| 734 | + }); |
| 735 | + |
| 736 | + it("shows a transparent modal that keeps every orientation", async () => { |
| 737 | + const image = await openEnlarged(); |
| 738 | + const { props } = image.UNSAFE_getByType(Modal); |
| 739 | + |
| 740 | + expect(props.transparent).toBe(true); |
| 741 | + expect(props.statusBarTranslucent).toBe(false); |
| 742 | + expect(props.supportedOrientations).toEqual([ |
| 743 | + "portrait", |
| 744 | + "portrait-upside-down", |
| 745 | + "landscape", |
| 746 | + "landscape-left", |
| 747 | + "landscape-right" |
| 748 | + ]); |
| 749 | + }); |
| 750 | + |
| 751 | + it("closes when the enlarged image is pressed", async () => { |
| 752 | + const image = await openEnlarged(); |
| 753 | + const enlarged = await findByImageTestId(image, imageProps.name, "ImageEnlargedPressable"); |
| 754 | + |
| 755 | + closeWithFakeTimers(() => fireEvent.press(enlarged)); |
| 756 | + |
| 757 | + expect(image.UNSAFE_queryByType(Modal)).toBeNull(); |
| 758 | + }); |
| 759 | + |
| 760 | + it("closes on the Android back button", async () => { |
| 761 | + const image = await openEnlarged(); |
| 762 | + |
| 763 | + closeWithFakeTimers(() => image.UNSAFE_getByType(Modal).props.onRequestClose()); |
| 764 | + |
| 765 | + expect(image.UNSAFE_queryByType(Modal)).toBeNull(); |
| 766 | + }); |
| 767 | + |
| 768 | + it("exposes the screen reader caption, hint and role on the enlarged image", async () => { |
| 769 | + const image = await openEnlarged({ |
| 770 | + screenReaderCaption: dynamicValue("A caption"), |
| 771 | + screenReaderHint: dynamicValue("A hint") |
| 772 | + }); |
| 773 | + const { props } = await findByImageTestId(image, imageProps.name, "ImageEnlargedPressable"); |
| 774 | + |
| 775 | + expect(props.accessibilityLabel).toBe("A caption"); |
| 776 | + expect(props.accessibilityHint).toBe("A hint"); |
| 777 | + expect(props.accessibilityRole).toBe("image"); |
| 778 | + }); |
| 779 | + |
| 780 | + it("marks the overlay as modal on iOS", async () => { |
| 781 | + (Platform as { OS: string }).OS = "ios"; |
| 782 | + const image = await openEnlarged(); |
| 783 | + const { props } = await findByImageTestId(image, imageProps.name, "ImageEnlargedOverlay-content"); |
| 784 | + |
| 785 | + expect(props.accessibilityViewIsModal).toBe(true); |
| 786 | + expect(props.importantForAccessibility).toBeUndefined(); |
| 787 | + }); |
| 788 | + |
| 789 | + it("marks the overlay as important for accessibility on Android", async () => { |
| 790 | + (Platform as { OS: string }).OS = "android"; |
| 791 | + const image = await openEnlarged(); |
| 792 | + const { props } = await findByImageTestId(image, imageProps.name, "ImageEnlargedOverlay-content"); |
| 793 | + |
| 794 | + expect(props.importantForAccessibility).toBe("yes"); |
| 795 | + expect(props.accessibilityViewIsModal).toBeUndefined(); |
| 796 | + }); |
| 797 | + |
| 798 | + it("moves accessibility focus to the enlarged image when it opens", async () => { |
| 799 | + const image = await openEnlarged(); |
| 800 | + |
| 801 | + fireEvent( |
| 802 | + await findByImageTestId(image, imageProps.name, "ImageEnlargedOverlay-content"), |
| 803 | + "layout", |
| 804 | + onLayoutEventData |
| 805 | + ); |
| 806 | + |
| 807 | + expect(setFocus).toHaveBeenCalledTimes(1); |
| 808 | + expect(nodeOf(setFocus.mock.calls[0][0]).props.testID).toBe(`${imageProps.name}$ImageEnlargedPressable`); |
| 809 | + }); |
| 810 | + |
| 811 | + it("returns accessibility focus to the small image once it has closed", async () => { |
| 812 | + const image = await openEnlarged(); |
| 813 | + const enlarged = await findByImageTestId(image, imageProps.name, "ImageEnlargedPressable"); |
| 814 | + setFocus.mockClear(); |
| 815 | + |
| 816 | + closeWithFakeTimers(() => fireEvent.press(enlarged)); |
| 817 | + |
| 818 | + expect(image.UNSAFE_queryByType(Modal)).toBeNull(); |
| 819 | + expect(setFocus).toHaveBeenCalledTimes(1); |
| 820 | + expect(nodeOf(setFocus.mock.calls[0][0]).props.testID).toBe(`${imageProps.name}$ImageSmallPressable`); |
| 821 | + }); |
| 822 | + }); |
670 | 823 | }); |
0 commit comments