Skip to content

Commit d25fef9

Browse files
committed
fix impl
1 parent e55f0f0 commit d25fef9

File tree

4 files changed

+84
-5
lines changed

4 files changed

+84
-5
lines changed

src/__tests__/render.test.tsx

Lines changed: 83 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">
@@ -33,6 +49,15 @@ test('does not warn when no options are passed', async () => {
3349
expect(_console.warn).not.toHaveBeenCalled();
3450
});
3551

52+
test('supports null options', async () => {
53+
const TestComponent = () => <Text testID="test">Test</Text>;
54+
55+
await render(<TestComponent />, null as any);
56+
57+
expect(screen.getByTestId('test')).toBeOnTheScreen();
58+
expect(_console.warn).not.toHaveBeenCalled();
59+
});
60+
3661
test('does not warn when only valid options are passed', async () => {
3762
const TestComponent = () => <Text testID="test">Test</Text>;
3863
const Wrapper = ({ children }: { children: React.ReactNode }) => <View>{children}</View>;
@@ -68,6 +93,64 @@ describe('render options', () => {
6893
});
6994
});
7095

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+
71154
describe('component rendering', () => {
72155
test('render accepts RCTText component', async () => {
73156
await render(React.createElement('RCTText', { testID: 'text' }, 'Hello'));

src/__tests__/suspense-fake-timers.test.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ afterEach(() => {
1414
});
1515

1616
function Suspending({ promise, testID }: { promise: Promise<unknown>; testID: string }) {
17-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
18-
// @ts-ignore React 18 does not have `use` hook
1917
React.use(promise);
2018
return <View testID={testID} />;
2119
}

src/__tests__/suspense.test.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ afterEach(() => {
1212
});
1313

1414
function Suspending({ promise, testID }: { promise: Promise<unknown>; testID: string }) {
15-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
16-
// @ts-ignore React 18 does not have `use` hook
1715
React.use(promise);
1816
return <View testID={testID} />;
1917
}

src/render.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ type StyleLike = Record<string, unknown>;
127127

128128
function withHiddenStyle(style: StyleProp<StyleLike>): StyleProp<StyleLike> {
129129
if (style == null) {
130-
return null;
130+
return { display: 'none' };
131131
}
132132

133133
return [style, { display: 'none' }];

0 commit comments

Comments
 (0)