Skip to content

Commit 88120d1

Browse files
committed
test hidden instance props
1 parent 498ef37 commit 88120d1

1 file changed

Lines changed: 74 additions & 0 deletions

File tree

src/__tests__/render.test.tsx

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,22 @@ import { Text, View } from 'react-native';
44
import { render, screen } from '..';
55
import { _console, logger } from '../helpers/logger';
66

7+
function MaybeSuspend({
8+
children,
9+
promise,
10+
suspend,
11+
}: {
12+
children: React.ReactNode;
13+
promise: Promise<unknown>;
14+
suspend: boolean;
15+
}) {
16+
if (suspend) {
17+
React.use(promise);
18+
}
19+
20+
return children;
21+
}
22+
723
test('renders a simple component', async () => {
824
const TestComponent = () => (
925
<View testID="container">
@@ -77,6 +93,64 @@ describe('render options', () => {
7793
});
7894
});
7995

96+
describe('hidden instance props', () => {
97+
test('sets hidden suspended elements with no style to display none', async () => {
98+
const promise = new Promise<unknown>(() => {});
99+
100+
function TestComponent({ suspend }: { suspend: boolean }) {
101+
return (
102+
<React.Suspense fallback={<Text>Loading...</Text>}>
103+
<MaybeSuspend promise={promise} suspend={suspend}>
104+
<View testID="hidden-target">
105+
<Text>Ready</Text>
106+
</View>
107+
</MaybeSuspend>
108+
</React.Suspense>
109+
);
110+
}
111+
112+
await render(<TestComponent suspend={false} />);
113+
114+
expect(screen.getByText('Ready')).toBeOnTheScreen();
115+
116+
await screen.rerender(<TestComponent suspend />);
117+
118+
expect(screen.getByText('Loading...')).toBeOnTheScreen();
119+
expect(
120+
screen.getByTestId('hidden-target', { includeHiddenElements: true }).props.style,
121+
).toEqual({
122+
display: 'none',
123+
});
124+
});
125+
126+
test('appends display none when suspending an element with existing style', async () => {
127+
const promise = new Promise<unknown>(() => {});
128+
129+
function TestComponent({ suspend }: { suspend: boolean }) {
130+
return (
131+
<React.Suspense fallback={<Text>Loading...</Text>}>
132+
<MaybeSuspend promise={promise} suspend={suspend}>
133+
<View style={{ opacity: 0.5 }} testID="hidden-target">
134+
<Text>Ready</Text>
135+
</View>
136+
</MaybeSuspend>
137+
</React.Suspense>
138+
);
139+
}
140+
141+
await render(<TestComponent suspend={false} />);
142+
143+
expect(screen.getByText('Ready')).toBeOnTheScreen();
144+
145+
await screen.rerender(<TestComponent suspend />);
146+
147+
expect(screen.getByText('Loading...')).toBeOnTheScreen();
148+
expect(
149+
screen.getByTestId('hidden-target', { includeHiddenElements: true }).props.style,
150+
).toEqual([{ opacity: 0.5 }, { display: 'none' }]);
151+
});
152+
});
153+
80154
describe('component rendering', () => {
81155
test('render accepts RCTText component', async () => {
82156
await render(React.createElement('RCTText', { testID: 'text' }, 'Hello'));

0 commit comments

Comments
 (0)