Skip to content

Commit e61a42a

Browse files
committed
refactor: detect Image host component name
1 parent f69d1d5 commit e61a42a

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

src/__tests__/host-component-names.test.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ describe('getHostComponentNames', () => {
1414
hostComponentNames: {
1515
text: 'banana',
1616
textInput: 'banana',
17+
image: 'banana',
1718
switch: 'banana',
1819
scrollView: 'banana',
1920
modal: 'banana',
@@ -23,6 +24,7 @@ describe('getHostComponentNames', () => {
2324
expect(getHostComponentNames()).toEqual({
2425
text: 'banana',
2526
textInput: 'banana',
27+
image: 'banana',
2628
switch: 'banana',
2729
scrollView: 'banana',
2830
modal: 'banana',
@@ -37,6 +39,7 @@ describe('getHostComponentNames', () => {
3739
expect(hostComponentNames).toEqual({
3840
text: 'Text',
3941
textInput: 'TextInput',
42+
image: 'Image',
4043
switch: 'RCTSwitch',
4144
scrollView: 'RCTScrollView',
4245
modal: 'Modal',
@@ -67,6 +70,7 @@ describe('configureHostComponentNamesIfNeeded', () => {
6770
expect(getConfig().hostComponentNames).toEqual({
6871
text: 'Text',
6972
textInput: 'TextInput',
73+
image: 'Image',
7074
switch: 'RCTSwitch',
7175
scrollView: 'RCTScrollView',
7276
modal: 'Modal',
@@ -78,6 +82,7 @@ describe('configureHostComponentNamesIfNeeded', () => {
7882
hostComponentNames: {
7983
text: 'banana',
8084
textInput: 'banana',
85+
image: 'banana',
8186
switch: 'banana',
8287
scrollView: 'banana',
8388
modal: 'banana',
@@ -89,13 +94,14 @@ describe('configureHostComponentNamesIfNeeded', () => {
8994
expect(getConfig().hostComponentNames).toEqual({
9095
text: 'banana',
9196
textInput: 'banana',
97+
image: 'banana',
9298
switch: 'banana',
9399
scrollView: 'banana',
94100
modal: 'banana',
95101
});
96102
});
97103

98-
test('throw an error when autodetection fails', () => {
104+
test('throw an error when auto-detection fails', () => {
99105
const mockCreate = jest.spyOn(TestRenderer, 'create') as jest.Mock;
100106
const renderer = TestRenderer.create(<View />);
101107

src/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export type ConfigAliasOptions = {
2323
export type HostComponentNames = {
2424
text: string;
2525
textInput: string;
26+
image: string;
2627
switch: string;
2728
scrollView: string;
2829
modal: string;

src/helpers/host-component-names.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as React from 'react';
22
import { ReactTestInstance } from 'react-test-renderer';
3-
import { Modal, ScrollView, Switch, Text, TextInput, View } from 'react-native';
3+
import { Image, Modal, ScrollView, Switch, Text, TextInput, View } from 'react-native';
44
import { configureInternal, getConfig, HostComponentNames } from '../config';
55
import { renderWithAct } from '../render-act';
66
import { HostTestInstance } from './component-tree';
@@ -34,6 +34,7 @@ function detectHostComponentNames(): HostComponentNames {
3434
<View>
3535
<Text testID="text">Hello</Text>
3636
<TextInput testID="textInput" />
37+
<Image testID="image" />
3738
<Switch testID="switch" />
3839
<ScrollView testID="scrollView" />
3940
<Modal testID="modal" />
@@ -43,6 +44,7 @@ function detectHostComponentNames(): HostComponentNames {
4344
return {
4445
text: getByTestId(renderer.root, 'text').type as string,
4546
textInput: getByTestId(renderer.root, 'textInput').type as string,
47+
image: getByTestId(renderer.root, 'image').type as string,
4648
switch: getByTestId(renderer.root, 'switch').type as string,
4749
scrollView: getByTestId(renderer.root, 'scrollView').type as string,
4850
modal: getByTestId(renderer.root, 'modal').type as string,
@@ -85,6 +87,14 @@ export function isHostTextInput(element?: ReactTestInstance | null): element is
8587
return element?.type === getHostComponentNames().textInput;
8688
}
8789

90+
/**
91+
* Checks if the given element is a host Image element.
92+
* @param element The element to check.
93+
*/
94+
export function isHostImage(element?: ReactTestInstance | null): element is HostTestInstance {
95+
return element?.type === getHostComponentNames().image;
96+
}
97+
8898
/**
8999
* Checks if the given element is a host Switch element.
90100
* @param element The element to check.

0 commit comments

Comments
 (0)