我正在将https://github.com/expo/react-native-action-sheet中的ActionSheetProvider
用于我的本机项目。
这就是我在代码中使用它的方式:
import { connectActionSheet } from '@expo/react-native-action-sheet';
export default
@connectActionSheet
class MyComponent extends React.Component {
...
<MyCustomIcon onPress={() => this.showMenu(item)}/>
...
showMenu = targetItem => {
this.props.showActionSheetWithOptions(
{
options: ['cancel','rename','hide','delete'],
cancelButtonIndex: 0,
destructiveButtonIndex: 3,
},
index => {
if (index === 3) { //delete item }
}
}
);
};
这是我到目前为止所做的,但是我不知道如何在测试中触发索引=== 3:
const showActionSheetWithOptions = jest.fn();
const renderComponent = overrides => {
props = {
showActionSheetWithOptions,
...overrides,
};
return shallow(<MyComponent.wrappedComponent {...props} />);
};
it('should check if delete option is selected', () => {
const wrapper = renderComponent();
const instance = wrapper.instance();
const myIcon = wrapper.find({ testID: 'my-icon' });
const spyShowMenu = jest.spyOn(instance, 'showMenu');
myIcon.props.onPress();
expect(spyShowMenu).toHaveBeenCalled(); // this test passes
// Now that the action sheet has been displayed, how to select delete option?
});
});