Skip to content

Commit 3231bed

Browse files
authored
feat(props): adding test id and library mock example (#931)
* feat(props): adding test id * docs(readme): adding mock example
1 parent a33379d commit 3231bed

File tree

4 files changed

+51
-1
lines changed

4 files changed

+51
-1
lines changed

README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,45 @@ It will contain id of the given action.
277277
|-------------------------|----------|
278278
| ({nativeEvent}) => void | No |
279279

280+
## Testing with Jest
281+
282+
In some cases, you might want to mock the package to test your components. You can do this by using the `jest.mock` function.
283+
284+
```ts
285+
import type { MenuComponentProps } from '@react-native-menu/menu';
286+
287+
jest.mock('@react-native-menu/menu', () => ({
288+
MenuView: jest.fn((props: MenuComponentProps) => {
289+
const React = require('react');
290+
291+
class MockMenuView extends React.Component {
292+
render() {
293+
return React.createElement(
294+
'View',
295+
{ testID: props.testID },
296+
// Dynamically mock each action
297+
props.actions.map(action =>
298+
React.createElement('Button', {
299+
key: action.id,
300+
title: action.title,
301+
onPress: () => {
302+
if (action.id && props?.onPressAction) {
303+
props.onPressAction({ nativeEvent: { event: action.id } });
304+
}
305+
},
306+
testID: action.id
307+
})
308+
),
309+
this.props.children
310+
);
311+
}
312+
}
313+
314+
return React.createElement(MockMenuView, props);
315+
})
316+
}));
317+
```
318+
280319
## Contributing
281320

282321
See the [contributing guide](CONTRIBUTING.md) to learn how to contribute to the repository and the development workflow.

example/src/App.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ export const App = () => {
163163
]}
164164
shouldOpenOnLongPress={true}
165165
themeVariant={themeVariant}
166+
testID="menuView"
166167
>
167168
<View style={styles.button}>
168169
<Text style={styles.buttonText}>Test</Text>

src/UIMenuView.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@ import type { NativeMenuComponentProps } from './types';
88
const MenuView: React.FC<React.PropsWithChildren<NativeMenuComponentProps>> = ({
99
style,
1010
children,
11+
testID,
1112
}) => {
12-
return <View style={style}>{children}</View>;
13+
return (
14+
<View style={style} testID={testID}>
15+
{children}
16+
</View>
17+
);
1318
};
1419

1520
export default MenuView;

src/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@ type MenuComponentPropsBase = {
154154
left: number;
155155
right: number;
156156
};
157+
/**
158+
* Test ID for testing purposes
159+
*/
160+
testID?: string;
157161
};
158162

159163
export type MenuComponentProps =
@@ -175,4 +179,5 @@ export type NativeMenuComponentProps = {
175179
actionsHash: string;
176180
title?: string;
177181
hitSlop?: MenuComponentProps['hitSlop'];
182+
testID?: string;
178183
};

0 commit comments

Comments
 (0)