在测试本机模块功能时遇到了一些麻烦。我设法模拟了商店,道具和状态,并调用了模块功能,这是我的测试:
import React from 'react';
import { shallow, configure } from 'enzyme';
import { expect } from 'chai';
import Adapter from 'enzyme-adapter-react-16';
import {Login} from '../src/containers/authentification/login'
configure({ adapter: new Adapter() })
describe('test login module', () => {
test('Login fails', () => {
const store = {
getState: jest.fn(() => ({credentials:{token: 'token'}})),
dispatch: jest.fn(),
subscribe: jest.fn(),
}
const navigation = { navigate: jest.fn() };
const props = {
navigation: jest.fn(),
store,
}
props.navigation.state = {params: {}}
const wrapper = shallow(<Login {...props}/>);
wrapper.update();
wrapper.setState({driver: 'Kuku', password: 'kuku'})
const instance = wrapper.instance();
instance.state.driver = 'dfgdf'
wrapper.update()
console.log(wrapper.state(), instance.state)
expect(instance.loginTry()).toBeFalsy();
});
});
如我所见,我也在包装器和实例上设置了状态,并且console.log()
照原样打印。但是通过运行npm测试,我得到了:
console.log __tests__/APlication.js:39
{ driver: 'dfgdf',
password: 'kuku',
error: null,
driverInput: '#bdc3c7',
passwordInput: '#bdc3c7',
errorText: null } { driver: 'dfgdf',
password: 'kuku',
error: null,
driverInput: '#bdc3c7',
passwordInput: '#bdc3c7',
errorText: null }
● Login fails
ReferenceError: code is not defined
89 |
90 | loginTry() {
> 91 | code = this.state.driver;
| ^
92 | password = this.state.password;
93 |
94 | LoginRequest(code, password).then((response) => {
在这里我可以忽略什么?应该不应该像以前使用setState那样设置该函数中的状态?