Skip to content

Commit

Permalink
feat(props): adding test id and library mock example (#931)
Browse files Browse the repository at this point in the history
* feat(props): adding test id

* docs(readme): adding mock example
  • Loading branch information
mateoguzmana authored Oct 22, 2024
1 parent a33379d commit 3231bed
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,45 @@ It will contain id of the given action.
|-------------------------|----------|
| ({nativeEvent}) => void | No |

## Testing with Jest

In some cases, you might want to mock the package to test your components. You can do this by using the `jest.mock` function.

```ts
import type { MenuComponentProps } from '@react-native-menu/menu';

jest.mock('@react-native-menu/menu', () => ({
MenuView: jest.fn((props: MenuComponentProps) => {
const React = require('react');

class MockMenuView extends React.Component {
render() {
return React.createElement(
'View',
{ testID: props.testID },
// Dynamically mock each action
props.actions.map(action =>
React.createElement('Button', {
key: action.id,
title: action.title,
onPress: () => {
if (action.id && props?.onPressAction) {
props.onPressAction({ nativeEvent: { event: action.id } });
}
},
testID: action.id
})
),
this.props.children
);
}
}

return React.createElement(MockMenuView, props);
})
}));
```

## Contributing

See the [contributing guide](CONTRIBUTING.md) to learn how to contribute to the repository and the development workflow.
Expand Down
1 change: 1 addition & 0 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ export const App = () => {
]}
shouldOpenOnLongPress={true}
themeVariant={themeVariant}
testID="menuView"
>
<View style={styles.button}>
<Text style={styles.buttonText}>Test</Text>
Expand Down
7 changes: 6 additions & 1 deletion src/UIMenuView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@ import type { NativeMenuComponentProps } from './types';
const MenuView: React.FC<React.PropsWithChildren<NativeMenuComponentProps>> = ({
style,
children,
testID,
}) => {
return <View style={style}>{children}</View>;
return (
<View style={style} testID={testID}>
{children}
</View>
);
};

export default MenuView;
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ type MenuComponentPropsBase = {
left: number;
right: number;
};
/**
* Test ID for testing purposes
*/
testID?: string;
};

export type MenuComponentProps =
Expand All @@ -175,4 +179,5 @@ export type NativeMenuComponentProps = {
actionsHash: string;
title?: string;
hitSlop?: MenuComponentProps['hitSlop'];
testID?: string;
};

0 comments on commit 3231bed

Please sign in to comment.