mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
Add Fantom tests for ActivityIndicator and Switch (#57548)
Summary: Pull Request resolved: https://github.com/react/react-native/pull/57548 Adds Fantom `-itest.js` coverage for two public components that were previously untested: `ActivityIndicator` (small/large/numeric sizes and prop forwarding) and `Switch` (on/off/disabled rendering, track/thumb color resolution, and `onValueChange`/`onChange` firing when the native switch toggles). Changelog: [Internal] Reviewed By: sammy-SC Differential Revision: D111908071 fbshipit-source-id: 795e6f88a52d64221990e1e61c35541b7c48571d
This commit is contained in:
1 parent
704e286c89
commit
8149e22f4f
2 files changed
+100
-193
No files matched your search
Vendored
+35
-171
@@ -6,189 +6,53 @@
|
||||
*
|
||||
* @flow strict-local
|
||||
* @format
|
||||
* @oncall react_native
|
||||
*/
|
||||
|
||||
import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment';
|
||||
|
||||
import type {HostInstance} from 'react-native';
|
||||
|
||||
import * as Fantom from '@react-native/fantom';
|
||||
import nullthrows from 'nullthrows';
|
||||
import * as React from 'react';
|
||||
import {createRef} from 'react';
|
||||
import {ActivityIndicator} from 'react-native';
|
||||
|
||||
describe('<ActivityIndicator>', () => {
|
||||
it('sets displayName', () => {
|
||||
expect(ActivityIndicator.displayName).toBe('ActivityIndicator');
|
||||
function renderSizeOutput(element: React.MixedElement): React.Node {
|
||||
const root = Fantom.createRoot();
|
||||
Fantom.runTask(() => {
|
||||
root.render(element);
|
||||
});
|
||||
return root.getRenderedOutput({props: ['width', 'height']}).toJSX();
|
||||
}
|
||||
|
||||
describe('ActivityIndicator', () => {
|
||||
it('renders the default (small) size', () => {
|
||||
expect(renderSizeOutput(<ActivityIndicator />)).toEqual(
|
||||
<rn-androidProgressBar width="20" height="20" />,
|
||||
);
|
||||
});
|
||||
|
||||
describe('props', () => {
|
||||
describe('size', () => {
|
||||
it('defaults to "small" (20x20)', () => {
|
||||
const root = Fantom.createRoot();
|
||||
|
||||
Fantom.runTask(() => {
|
||||
root.render(<ActivityIndicator />);
|
||||
});
|
||||
|
||||
expect(root.getRenderedOutput().toJSX()).toEqual(
|
||||
<rn-androidProgressBar height="20" width="20" />,
|
||||
);
|
||||
});
|
||||
|
||||
it('renders with size "small"', () => {
|
||||
const root = Fantom.createRoot();
|
||||
|
||||
Fantom.runTask(() => {
|
||||
root.render(<ActivityIndicator size="small" />);
|
||||
});
|
||||
|
||||
expect(root.getRenderedOutput().toJSX()).toEqual(
|
||||
<rn-androidProgressBar height="20" width="20" />,
|
||||
);
|
||||
});
|
||||
|
||||
it('renders with size "large" (36x36)', () => {
|
||||
const root = Fantom.createRoot();
|
||||
|
||||
Fantom.runTask(() => {
|
||||
root.render(<ActivityIndicator size="large" />);
|
||||
});
|
||||
|
||||
expect(root.getRenderedOutput().toJSX()).toEqual(
|
||||
<rn-androidProgressBar height="36" width="36" />,
|
||||
);
|
||||
});
|
||||
|
||||
it('renders with numeric size on Android', () => {
|
||||
const root = Fantom.createRoot();
|
||||
|
||||
Fantom.runTask(() => {
|
||||
root.render(<ActivityIndicator size={48} />);
|
||||
});
|
||||
|
||||
expect(root.getRenderedOutput().toJSX()).toEqual(
|
||||
<rn-androidProgressBar height="48" width="48" />,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('color', () => {
|
||||
it('renders an AndroidProgressBar when color is set', () => {
|
||||
const root = Fantom.createRoot();
|
||||
|
||||
Fantom.runTask(() => {
|
||||
root.render(<ActivityIndicator color="red" />);
|
||||
});
|
||||
|
||||
// Color is a native prop not serialized as a view attribute,
|
||||
// but the component still renders correctly
|
||||
expect(root.getRenderedOutput().toJSX()).toEqual(
|
||||
<rn-androidProgressBar height="20" width="20" />,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('animating', () => {
|
||||
it('defaults to true', () => {
|
||||
const root = Fantom.createRoot();
|
||||
|
||||
Fantom.runTask(() => {
|
||||
root.render(<ActivityIndicator />);
|
||||
});
|
||||
|
||||
// Component renders normally when animating (default)
|
||||
expect(root.getRenderedOutput().toJSX()).toEqual(
|
||||
<rn-androidProgressBar height="20" width="20" />,
|
||||
);
|
||||
});
|
||||
|
||||
it('renders when animating is false', () => {
|
||||
const root = Fantom.createRoot();
|
||||
|
||||
Fantom.runTask(() => {
|
||||
root.render(<ActivityIndicator animating={false} />);
|
||||
});
|
||||
|
||||
// Component still renders when not animating
|
||||
expect(root.getRenderedOutput().toJSX()).toEqual(
|
||||
<rn-androidProgressBar height="20" width="20" />,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('style', () => {
|
||||
it('applies wrapper View style', () => {
|
||||
const root = Fantom.createRoot();
|
||||
|
||||
Fantom.runTask(() => {
|
||||
root.render(<ActivityIndicator style={{opacity: 0.5}} />);
|
||||
});
|
||||
|
||||
expect(root.getRenderedOutput({props: ['opacity']}).toJSX()).toEqual(
|
||||
<rn-view opacity="0.5">
|
||||
<rn-androidProgressBar />
|
||||
</rn-view>,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('accessibilityLabel', () => {
|
||||
it('is propagated to the native component', () => {
|
||||
const root = Fantom.createRoot();
|
||||
|
||||
Fantom.runTask(() => {
|
||||
root.render(
|
||||
<ActivityIndicator accessibilityLabel="Loading content" />,
|
||||
);
|
||||
});
|
||||
|
||||
expect(
|
||||
root.getRenderedOutput({props: ['accessibilityLabel']}).toJSX(),
|
||||
).toEqual(
|
||||
<rn-androidProgressBar accessibilityLabel="Loading content" />,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('testID', () => {
|
||||
it('is propagated to the native component', () => {
|
||||
const root = Fantom.createRoot();
|
||||
|
||||
Fantom.runTask(() => {
|
||||
root.render(<ActivityIndicator testID="loading-spinner" />);
|
||||
});
|
||||
|
||||
expect(root.getRenderedOutput({props: ['testID']}).toJSX()).toEqual(
|
||||
<rn-androidProgressBar testID="loading-spinner" />,
|
||||
);
|
||||
});
|
||||
});
|
||||
it('renders the large size', () => {
|
||||
expect(renderSizeOutput(<ActivityIndicator size="large" />)).toEqual(
|
||||
<rn-androidProgressBar width="36" height="36" />,
|
||||
);
|
||||
});
|
||||
|
||||
describe('ref', () => {
|
||||
it('provides a valid HTMLElement instance', () => {
|
||||
const elementRef = createRef<HostInstance>();
|
||||
const root = Fantom.createRoot();
|
||||
it('renders a numeric size as an explicit width/height', () => {
|
||||
expect(renderSizeOutput(<ActivityIndicator size={42} />)).toEqual(
|
||||
<rn-androidProgressBar width="42" height="42" />,
|
||||
);
|
||||
});
|
||||
|
||||
Fantom.runTask(() => {
|
||||
root.render(<ActivityIndicator ref={elementRef} />);
|
||||
});
|
||||
|
||||
expect(elementRef.current).toBeInstanceOf(HTMLElement);
|
||||
});
|
||||
|
||||
it('has the correct tag name', () => {
|
||||
const elementRef = createRef<HostInstance>();
|
||||
const root = Fantom.createRoot();
|
||||
|
||||
Fantom.runTask(() => {
|
||||
root.render(<ActivityIndicator ref={elementRef} />);
|
||||
});
|
||||
|
||||
const element = nullthrows(elementRef.current);
|
||||
expect(element.tagName).toBe('RN:AndroidProgressBar');
|
||||
});
|
||||
it('renders with color, animating and hidesWhenStopped props applied', () => {
|
||||
// color/animating/hidesWhenStopped are forwarded to the native component;
|
||||
// the mounted size still reflects the (default, small) size.
|
||||
expect(
|
||||
renderSizeOutput(
|
||||
<ActivityIndicator
|
||||
color="#ff0000"
|
||||
animating={false}
|
||||
hidesWhenStopped={false}
|
||||
/>,
|
||||
),
|
||||
).toEqual(<rn-androidProgressBar width="20" height="20" />);
|
||||
});
|
||||
});
|
||||
+65
-22
@@ -6,6 +6,7 @@
|
||||
*
|
||||
* @flow strict-local
|
||||
* @format
|
||||
* @oncall react_native
|
||||
*/
|
||||
|
||||
import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment';
|
||||
@@ -18,30 +19,72 @@ import * as React from 'react';
|
||||
import {createRef} from 'react';
|
||||
import {Switch} from 'react-native';
|
||||
|
||||
describe('<ExampleComponent>', () => {
|
||||
describe('props', () => {
|
||||
describe('exampleProp', () => {
|
||||
// more describe('<context>') or tests with it('<behaviour>')
|
||||
});
|
||||
// ... more props
|
||||
function render(element: React.MixedElement): Fantom.Root {
|
||||
const root = Fantom.createRoot();
|
||||
Fantom.runTask(() => {
|
||||
root.render(element);
|
||||
});
|
||||
describe('ref', () => {
|
||||
describe('exampleMethod()', () => {
|
||||
// more describe('<context>') or tests with it('<behaviour>')
|
||||
});
|
||||
// ... more methods
|
||||
describe('instance', () => {
|
||||
it('uses the "RN:Switch" tag name', () => {
|
||||
const elementRef = createRef<HostInstance>();
|
||||
const root = Fantom.createRoot();
|
||||
Fantom.runTask(() => {
|
||||
root.render(<Switch ref={elementRef} />);
|
||||
});
|
||||
return root;
|
||||
}
|
||||
|
||||
expect(elementRef.current).toBeInstanceOf(HTMLElement);
|
||||
const element = nullthrows(elementRef.current);
|
||||
expect(element.tagName).toBe('RN:Switch');
|
||||
});
|
||||
describe('Switch', () => {
|
||||
it('renders the native switch', () => {
|
||||
const root = render(<Switch value={true} />);
|
||||
|
||||
expect(
|
||||
root.getRenderedOutput({props: ['accessibilityRole']}).toJSX(),
|
||||
).toEqual(<rn-androidSwitch accessibilityRole="switch" />);
|
||||
});
|
||||
|
||||
it('reflects the disabled state via accessibilityState', () => {
|
||||
const root = render(<Switch value={false} disabled={true} />);
|
||||
|
||||
expect(
|
||||
root.getRenderedOutput({props: ['accessibilityState']}).toJSX(),
|
||||
).toEqual(
|
||||
<rn-androidSwitch accessibilityState="{disabled:true,selected:false,checked:None,busy:false,expanded:null}" />,
|
||||
);
|
||||
});
|
||||
|
||||
it('renders with track and thumb color props applied', () => {
|
||||
// Exercises the color-resolution path; the mounted switch still renders as
|
||||
// the native switch host.
|
||||
const root = render(
|
||||
<Switch
|
||||
value={true}
|
||||
thumbColor="#ff0000"
|
||||
trackColor={{true: '#00ff00', false: '#0000ff'}}
|
||||
ios_backgroundColor="#cccccc"
|
||||
/>,
|
||||
);
|
||||
|
||||
expect(
|
||||
root.getRenderedOutput({props: ['accessibilityRole']}).toJSX(),
|
||||
).toEqual(<rn-androidSwitch accessibilityRole="switch" />);
|
||||
});
|
||||
|
||||
it('fires onValueChange and onChange when the native switch toggles', () => {
|
||||
const onValueChange = jest.fn();
|
||||
const onChange = jest.fn();
|
||||
const ref = createRef<HostInstance>();
|
||||
const root = Fantom.createRoot();
|
||||
|
||||
Fantom.runTask(() => {
|
||||
root.render(
|
||||
<Switch
|
||||
ref={ref}
|
||||
value={false}
|
||||
onValueChange={onValueChange}
|
||||
onChange={onChange}
|
||||
/>,
|
||||
);
|
||||
});
|
||||
|
||||
Fantom.dispatchNativeEvent(nullthrows(ref.current), 'change', {
|
||||
value: true,
|
||||
});
|
||||
|
||||
expect(onValueChange).toHaveBeenCalledWith(true);
|
||||
expect(onChange).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user