|
| 1 | +jest.mock('react-native-reanimated', () => { |
| 2 | + const actual = jest.requireActual('react-native-reanimated/mock'); |
| 3 | + const { View } = require('react-native'); |
| 4 | + |
| 5 | + return { |
| 6 | + ...actual, |
| 7 | + Animated: { |
| 8 | + ...actual.default, |
| 9 | + View, |
| 10 | + }, |
| 11 | + default: { |
| 12 | + ...actual.default, |
| 13 | + View, |
| 14 | + }, |
| 15 | + makeMutable: (value: unknown) => ({ value }), |
| 16 | + useAnimatedStyle: (updater: () => unknown) => updater(), |
| 17 | + }; |
| 18 | +}); |
| 19 | + |
| 20 | +jest.mock('react-native-teleport', () => { |
| 21 | + const React = require('react'); |
| 22 | + const { Text, View } = require('react-native'); |
| 23 | + |
| 24 | + return { |
| 25 | + Portal: View, |
| 26 | + PortalHost: ({ name }: { name: string }) => ( |
| 27 | + <View testID={`portal-host-${name}`}> |
| 28 | + <Text>{name}</Text> |
| 29 | + </View> |
| 30 | + ), |
| 31 | + PortalProvider: View, |
| 32 | + usePortal: jest.fn().mockReturnValue({ removePortal: jest.fn() }), |
| 33 | + }; |
| 34 | +}); |
| 35 | + |
| 36 | +import React from 'react'; |
| 37 | +import type { SharedValue } from 'react-native-reanimated'; |
| 38 | + |
| 39 | +import { act, cleanup, render, screen } from '@testing-library/react-native'; |
| 40 | + |
| 41 | +import { clearClosingPortalLayout, setClosingPortalLayout } from '../../../state-store'; |
| 42 | +import { ClosingPortalHostsLayer } from '../ClosingPortalHostsLayer'; |
| 43 | + |
| 44 | +const FIRST_RECT = { h: 40, w: 100, x: 10, y: 20 }; |
| 45 | +const SECOND_RECT = { h: 52, w: 140, x: 30, y: 45 }; |
| 46 | + |
| 47 | +describe('ClosingPortalHostsLayer', () => { |
| 48 | + beforeEach(() => { |
| 49 | + cleanup(); |
| 50 | + }); |
| 51 | + |
| 52 | + afterEach(() => { |
| 53 | + act(() => { |
| 54 | + clearClosingPortalLayout('overlay-header', 'first-entry'); |
| 55 | + clearClosingPortalLayout('overlay-header', 'second-entry'); |
| 56 | + clearClosingPortalLayout('overlay-composer', 'composer-entry'); |
| 57 | + }); |
| 58 | + cleanup(); |
| 59 | + }); |
| 60 | + |
| 61 | + it('renders the geometry for the top-most registration of a host and falls back when it is removed', () => { |
| 62 | + const { toJSON } = render( |
| 63 | + <ClosingPortalHostsLayer closeCoverOpacity={{ value: 0.5 } as SharedValue<number>} />, |
| 64 | + ); |
| 65 | + |
| 66 | + act(() => { |
| 67 | + setClosingPortalLayout('overlay-header', 'first-entry', FIRST_RECT); |
| 68 | + }); |
| 69 | + |
| 70 | + let tree = toJSON(); |
| 71 | + let slot = Array.isArray(tree) ? tree[0] : tree; |
| 72 | + |
| 73 | + expect(slot).toMatchObject({ |
| 74 | + children: [ |
| 75 | + expect.objectContaining({ |
| 76 | + props: expect.objectContaining({ testID: 'portal-host-overlay-header' }), |
| 77 | + }), |
| 78 | + ], |
| 79 | + props: expect.objectContaining({ |
| 80 | + pointerEvents: 'box-none', |
| 81 | + style: expect.objectContaining({ |
| 82 | + height: FIRST_RECT.h, |
| 83 | + left: FIRST_RECT.x, |
| 84 | + opacity: 0.5, |
| 85 | + position: 'absolute', |
| 86 | + top: FIRST_RECT.y, |
| 87 | + width: FIRST_RECT.w, |
| 88 | + }), |
| 89 | + }), |
| 90 | + }); |
| 91 | + |
| 92 | + act(() => { |
| 93 | + setClosingPortalLayout('overlay-header', 'second-entry', SECOND_RECT); |
| 94 | + }); |
| 95 | + |
| 96 | + tree = toJSON(); |
| 97 | + slot = Array.isArray(tree) ? tree[0] : tree; |
| 98 | + |
| 99 | + expect(slot).toMatchObject({ |
| 100 | + children: [ |
| 101 | + expect.objectContaining({ |
| 102 | + props: expect.objectContaining({ testID: 'portal-host-overlay-header' }), |
| 103 | + }), |
| 104 | + ], |
| 105 | + props: expect.objectContaining({ |
| 106 | + pointerEvents: 'box-none', |
| 107 | + style: expect.objectContaining({ |
| 108 | + height: SECOND_RECT.h, |
| 109 | + left: SECOND_RECT.x, |
| 110 | + opacity: 0.5, |
| 111 | + position: 'absolute', |
| 112 | + top: SECOND_RECT.y, |
| 113 | + width: SECOND_RECT.w, |
| 114 | + }), |
| 115 | + }), |
| 116 | + }); |
| 117 | + |
| 118 | + act(() => { |
| 119 | + clearClosingPortalLayout('overlay-header', 'second-entry'); |
| 120 | + }); |
| 121 | + |
| 122 | + tree = toJSON(); |
| 123 | + slot = Array.isArray(tree) ? tree[0] : tree; |
| 124 | + |
| 125 | + expect(slot).toMatchObject({ |
| 126 | + children: [ |
| 127 | + expect.objectContaining({ |
| 128 | + props: expect.objectContaining({ testID: 'portal-host-overlay-header' }), |
| 129 | + }), |
| 130 | + ], |
| 131 | + props: expect.objectContaining({ |
| 132 | + pointerEvents: 'box-none', |
| 133 | + style: expect.objectContaining({ |
| 134 | + height: FIRST_RECT.h, |
| 135 | + left: FIRST_RECT.x, |
| 136 | + opacity: 0.5, |
| 137 | + position: 'absolute', |
| 138 | + top: FIRST_RECT.y, |
| 139 | + width: FIRST_RECT.w, |
| 140 | + }), |
| 141 | + }), |
| 142 | + }); |
| 143 | + |
| 144 | + act(() => { |
| 145 | + clearClosingPortalLayout('overlay-header', 'first-entry'); |
| 146 | + }); |
| 147 | + |
| 148 | + expect(toJSON()).toBeNull(); |
| 149 | + }); |
| 150 | + |
| 151 | + it('renders one closing host slot per host name even when multiple entries are registered', () => { |
| 152 | + render(<ClosingPortalHostsLayer closeCoverOpacity={{ value: 1 } as SharedValue<number>} />); |
| 153 | + |
| 154 | + act(() => { |
| 155 | + setClosingPortalLayout('overlay-header', 'first-entry', FIRST_RECT); |
| 156 | + setClosingPortalLayout('overlay-header', 'second-entry', SECOND_RECT); |
| 157 | + setClosingPortalLayout('overlay-composer', 'composer-entry', { |
| 158 | + h: 60, |
| 159 | + w: 160, |
| 160 | + x: 5, |
| 161 | + y: 200, |
| 162 | + }); |
| 163 | + }); |
| 164 | + |
| 165 | + expect(screen.getAllByTestId('portal-host-overlay-header')).toHaveLength(1); |
| 166 | + expect(screen.getAllByTestId('portal-host-overlay-composer')).toHaveLength(1); |
| 167 | + }); |
| 168 | +}); |
0 commit comments