【发布时间】:2019-10-23 21:48:00
【问题描述】:
react-native-google-signin 定义如下:
export interface GoogleSigninButtonProps extends ViewProps {
style?: StyleProp<ViewStyle>;
size?: GoogleSigninButton.Size;
color?: GoogleSigninButton.Color;
disabled?: boolean;
onPress?(): void;
}
export class GoogleSigninButton extends React.Component<GoogleSigninButtonProps> {
constructor(props: GoogleSigninButtonProps);
}
export namespace GoogleSigninButton {
enum Size {
Standard,
Wide,
Icon,
}
enum Color {
Light,
Dark,
}
}
在我的代码中我就是这样使用它的:
<GoogleSigninButton
style={{ width: 192, height: 48 }}
size={GoogleSigninButton.Size.Wide}
color={GoogleSigninButton.Color.Dark}
onPress={this.authenticate}
testID={'GoogleAuthenticationButton'}
/>
我已经编写了我的测试来尝试模拟如下:
jest.mock('react-native-google-signin', () => ({
GoogleSigninButton: {
Size: {
Standard: 0,
Wide: 1,
Icon: 2
},
Color: {
Light: 0,
Dark: 1
}
}
}))
但是我得到以下错误:
Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.
我已尝试设置GoogleSigninButton: 'GoogleSigninButton',这将修复预期的返回类型,但会产生其他错误,例如未定义大小和颜色。如果我保持原样,那么我会收到上面的错误。
我怎样才能模拟上面的类,以便它返回一个GoogleSignInButton 的字符串,同时定义 Size 和 Color 属性,这样它就不会抛出“无法读取未定义的属性 Size”?
【问题讨论】:
-
简而言之:您需要模拟一个组件,但您使用普通对象模拟它。这就是它不起作用的原因。
标签: reactjs react-native jestjs