firebase - 错误:reactNativeFirebase2.default.auth 不是函数
问题描述
我有一个 SignUp.js 组件,用于将用户注册到我的应用程序。它受本教程启发/复制,基于react-native-firebase starter kit。
// SignUp.js
import React from 'react';
import { StyleSheet, Text, TextInput, View, Image, TouchableOpacity } from 'react-native';
import firebase from 'react-native-firebase';
export default class SignUp extends React.Component {
state = {
fullName: '',
email: '',
password: '',
errorMessage: null
}
handleSignUp = () => {
const { email, password, fullName } = this.state,
firebase
.auth()
.createUserWithEmailAndPassword(this.state.email, this.state.password)
.then(
(user) => {
if (user) {
user.updateProfile({
displayName: this.state.fullName,
photoURL: "../../assets/img/user.png"
});
}
}).then(() => this.props.navigation.navigate('Main'))
.catch(error => this.setState({ errorMessage: error.message }))
}
...
<View style={styles.buttons}>
<TouchableOpacity onPress={this.handleSignUp}>
<Text style={styles.buttonText}>Sign Up</Text>
</TouchableOpacity>
</View>
该方法在用户提交表单时handleSignUp()
通过事件调用。我正在尝试测试此方法以在用户提交带有错误电子邮件的表单时出现错误。这是我的测试:onPress
// SignUp.test.js
import { shallow } from 'enzyme';
import React from 'react';
import { TouchableOpacity } from 'react-native';
import SignUp from '../../components/SignUp';
import renderer from 'react-test-renderer';
it('should render error if user submits a bad email', () => {
const wrapper = shallow(<SignUp />);
wrapper.instance().handleFullNameText('John Smith');
wrapper.instance().handleEmailText('john.smith@'); // <- notice bad email
wrapper.instance().handlePasswordText('123456');
wrapper.find(TouchableOpacity).first().props().onPress();
})
我的测试失败,我有错误
TypeError:_reactNativeFirebase2.default.auth 不是函数
您能否指导我找到一个解决方案,因为我对本机和单元测试有反应?
解决方案
在你的SignUp.test.js
将其包含在文件的顶部
import '@firebase/firestore'
所以看起来像这样
import { shallow } from 'enzyme';
import React from 'react';
import { TouchableOpacity } from 'react-native';
import SignUp from '../../components/SignUp';
import renderer from 'react-test-renderer';
import '@firebase/firestore'
推荐阅读
- python - 嗨,当使用 python 和 selenium 时,我在通过 cloudflare 使用 DDos 保护访问站点时遇到问题
- python - 在 pyspark 中创建列数组
- ios - UITableView 像队列与单元格对齐到底部?
- testing - 如何用柏树点击
- javascript - 如何仅访问具有相同类的特定元素?jQuery
- jupyter-notebook - 如何解决 juypter notebook 中的内核重启错误?
- navigation - 水平滚动导航 - onclick 正确的项目不活动
- python - 尝试导入 matplotlib 时没有名为“kiwisolver”的模块
- cassandra - Cassandra错误无法连接到任何服务器',{'*':错误(111,“尝试连接到[('*',9042)]。最后一个错误:连接被拒绝
- php - 无法删除某些PC的某些浏览器上的cookie