File tree Expand file tree Collapse file tree 4 files changed +51
-1
lines changed
Expand file tree Collapse file tree 4 files changed +51
-1
lines changed Original file line number Diff line number Diff 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
282321See the [ contributing guide] ( CONTRIBUTING.md ) to learn how to contribute to the repository and the development workflow.
Original file line number Diff line number Diff 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 >
Original file line number Diff line number Diff line change @@ -8,8 +8,13 @@ import type { NativeMenuComponentProps } from './types';
88const 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
1520export default MenuView ;
Original file line number Diff line number Diff 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
159163export type MenuComponentProps =
@@ -175,4 +179,5 @@ export type NativeMenuComponentProps = {
175179 actionsHash : string ;
176180 title ?: string ;
177181 hitSlop ?: MenuComponentProps [ 'hitSlop' ] ;
182+ testID ?: string ;
178183} ;
You can’t perform that action at this time.
0 commit comments