Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions packages/rn-tester/js/RNTesterAppShared.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,33 @@ const RNTesterApp = ({
screen,
recentlyUsed,
hadDeepLink,
listFilters,
} = state;

const activeListScreen: 'components' | 'apis' =
screen === Screens.COMPONENTS ? 'components' : 'apis';
const activeListFilter = listFilters[activeListScreen];

const handleFilterTextChange = useCallback(
(filterText: string) => {
dispatch({
type: RNTesterNavigationActionsType.LIST_FILTER_CHANGE,
data: {screen: activeListScreen, filterText},
});
},
[dispatch, activeListScreen],
);

const handleCategoryChange = useCallback(
(category: string) => {
dispatch({
type: RNTesterNavigationActionsType.LIST_FILTER_CHANGE,
data: {screen: activeListScreen, category},
});
},
[dispatch, activeListScreen],
);

const isScreenTiny = useWindowDimensions().height < 600;

const examplesList = useMemo(
Expand Down Expand Up @@ -327,6 +352,10 @@ const RNTesterApp = ({
sections={activeExampleList}
handleModuleCardPress={handleModuleCardPress}
bottomInset={isIOS ? contentInsets.bottom : 0}
filterText={activeListFilter.filterText}
onFilterTextChange={handleFilterTextChange}
category={activeListFilter.category}
onCategoryChange={handleCategoryChange}
/>
)}
</View>
Expand Down
289 changes: 289 additions & 0 deletions packages/rn-tester/js/__tests__/RNTesterAppShared-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,289 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
*/

import type {ReactTestRenderer} from 'react-test-renderer';

import {create, unmount} from '@react-native/jest-preset/jest/renderer';
import * as React from 'react';
import {BackHandler, Platform, Text, TextInput, View} from 'react-native';
import TestRenderer from 'react-test-renderer';

const mockFlatListModule = {
title: 'FlatList',
description: 'Performant interface for rendering simple, flat lists.',
showIndividualExamples: true,
examples: [
{
name: 'simple',
title: 'Simple List',
description: 'Basic flat list.',
render(): React.Node {
return (
<View testID="flatlist-simple-example">
<Text>Simple FlatList</Text>
</View>
);
},
},
{
name: 'horizontal',
title: 'Horizontal List',
description: 'Horizontal flat list.',
render(): React.Node {
return (
<View testID="flatlist-horizontal-example">
<Text>Horizontal FlatList</Text>
</View>
);
},
},
],
};

const mockViewModule = {
title: 'View',
description: 'Basic building block.',
examples: [
{
title: 'Basic',
render(): React.Node {
return (
<View testID="view-example">
<Text>View Example</Text>
</View>
);
},
},
],
};

const mockAlertModule = {
title: 'Alert',
description: 'Launches an alert dialog.',
examples: [
{
title: 'Basic',
render(): React.Node {
return (
<View testID="alert-example">
<Text>Alert Example</Text>
</View>
);
},
},
],
};

const mockShareModule = {
title: 'Share',
description: 'Open share dialog.',
examples: [
{
title: 'Basic',
render(): React.Node {
return (
<View testID="share-example">
<Text>Share Example</Text>
</View>
);
},
},
],
};

jest.mock('../components/RNTesterDocumentationURL', () => () => null);
jest.mock('../utils/RNTesterList', () => ({
Components: [
{
key: 'FlatListExampleIndex',
category: 'ListView',
module: mockFlatListModule,
},
{
key: 'ViewExample',
category: 'Basic',
module: mockViewModule,
},
],
APIs: [
{
key: 'AlertExample',
category: 'UI',
module: mockAlertModule,
},
{
key: 'ShareExample',
category: 'Basic',
module: mockShareModule,
},
],
Modules: {
FlatListExampleIndex: mockFlatListModule,
ViewExample: mockViewModule,
AlertExample: mockAlertModule,
ShareExample: mockShareModule,
},
}));

function pressByTestID(root: ReactTestRenderer['root'], testID: string): void {
const target = root.find(
node =>
node.props.testID === testID && typeof node.props.onPress === 'function',
);
target.props.onPress();
}

describe('RNTesterAppShared', () => {
const RNTesterApp = require('../RNTesterAppShared').default;

it('preserves the search filter after opening an example and navigating back on iOS', async () => {
const renderer = await create(<RNTesterApp />);
try {
await TestRenderer.act(() => {
renderer.root.findByType(TextInput).props.onChangeText('Flat');
});

expect(renderer.root.findByType(TextInput).props.value).toBe('Flat');
expect(renderer.root.findAllByProps({testID: 'View'}).length).toBe(0);

await TestRenderer.act(() => {
pressByTestID(renderer.root, 'FlatList');
});

// Sub-example list also preserves its own search filter across opening an individual example.
await TestRenderer.act(() => {
renderer.root.findByType(TextInput).props.onChangeText('Horiz');
});
expect(renderer.root.findAllByProps({testID: 'Simple List'}).length).toBe(
0,
);

await TestRenderer.act(() => {
pressByTestID(renderer.root, 'Horizontal List');
});
expect(
renderer.root.findByProps({testID: 'flatlist-horizontal-example'}),
).toBeTruthy();

await TestRenderer.act(() => {
renderer.root.findByProps({title: 'Back'}).props.onPress();
});
expect(renderer.root.findByType(TextInput).props.value).toBe('Horiz');
expect(renderer.root.findAllByProps({testID: 'Simple List'}).length).toBe(
0,
);
expect(
renderer.root.findAllByProps({testID: 'Horizontal List'}).length,
).toBeGreaterThan(0);

await TestRenderer.act(() => {
renderer.root.findByProps({title: 'Back'}).props.onPress();
});

expect(renderer.root.findByType(TextInput).props.value).toBe('Flat');
expect(renderer.root.findAllByProps({testID: 'View'}).length).toBe(0);
expect(
renderer.root.findAllByProps({testID: 'FlatList'}).length,
).toBeGreaterThan(0);
expect(
renderer.root.findAllByProps({children: 'Recently Viewed'}).length,
).toBe(0);

// Clearing the search filter restores all items and Recently Viewed.
await TestRenderer.act(() => {
renderer.root.findByType(TextInput).props.onChangeText('');
});
expect(
renderer.root.findAllByProps({testID: 'View'}).length,
).toBeGreaterThan(0);
expect(
renderer.root.findAllByProps({children: 'Recently Viewed'}).length,
).toBeGreaterThan(0);
} finally {
await unmount(renderer);
}
});

it('preserves search filters per tab across APIs and Components navigation on Android', async () => {
const originalPlatform = Object.getOwnPropertyDescriptor(Platform, 'OS');
if (originalPlatform == null) {
throw new Error('The platform mock is missing.');
}
Object.defineProperty(Platform, 'OS', {
value: 'android',
configurable: true,
});

let hardwareBackPressHandler: ?() => boolean = null;
const backListenerSpy = jest
.spyOn(BackHandler, 'addEventListener')
.mockImplementation((eventName, handler) => {
if (eventName === 'hardwareBackPress') {
hardwareBackPressHandler = handler;
}
return {remove: jest.fn()};
});

const renderer = await create(<RNTesterApp />);
try {
await TestRenderer.act(() => {
renderer.root.findByType(TextInput).props.onChangeText('View');
});
expect(renderer.root.findAllByProps({testID: 'FlatList'}).length).toBe(0);

// Switch to APIs tab: should have its own empty filter.
await TestRenderer.act(() => {
pressByTestID(renderer.root, 'apis-tab');
});
expect(renderer.root.findByType(TextInput).props.value).toBe('');
expect(
renderer.root.findAllByProps({testID: 'Share'}).length,
).toBeGreaterThan(0);

// Filter APIs, open Alert, navigate back via Android hardware back press.
await TestRenderer.act(() => {
renderer.root.findByType(TextInput).props.onChangeText('Ale');
});
expect(renderer.root.findAllByProps({testID: 'Share'}).length).toBe(0);

await TestRenderer.act(() => {
pressByTestID(renderer.root, 'Alert');
});
expect(renderer.root.findByProps({testID: 'alert-example'})).toBeTruthy();

await TestRenderer.act(() => {
if (hardwareBackPressHandler == null) {
throw new Error('Expected hardwareBackPress listener');
}
hardwareBackPressHandler();
});

expect(renderer.root.findByType(TextInput).props.value).toBe('Ale');
expect(renderer.root.findAllByProps({testID: 'Share'}).length).toBe(0);
expect(
renderer.root.findAllByProps({testID: 'Alert'}).length,
).toBeGreaterThan(0);

// Switch back to Components tab: its search filter is still preserved.
await TestRenderer.act(() => {
pressByTestID(renderer.root, 'components-tab');
});
expect(renderer.root.findByType(TextInput).props.value).toBe('View');
expect(renderer.root.findAllByProps({testID: 'FlatList'}).length).toBe(0);
expect(
renderer.root.findAllByProps({testID: 'View'}).length,
).toBeGreaterThan(0);
} finally {
backListenerSpy.mockRestore();
Reflect.defineProperty(Platform, 'OS', originalPlatform);
await unmount(renderer);
}
});
});
Loading