我有一个简单的身份验证组件。我需要知道什么是测试isLogin
方法的最佳方法。我将用户的详细信息存储在redux-store中,每当我调用此方法时,它都会检查存储并返回true或false。这是组件:
import React from 'react';
import type { Node } from 'react';
import { useSelector } from 'react-redux';
import { Render } from '..';
type componentProps = {
fallback?: any,
children: Array<Node> | Node,
};
/* A method for checking if user is login or not */
const isLogin = () => {
const token = useSelector(state => state.User.token);
return !!token;
};
const Authentication = ({ fallback, children }: componentProps) => {
const token = useSelector(state => state.User.token);
return [
<Render key="item-auth-children" condition={token}>
{children}
</Render>,
<Render key="item-auth-fallback" condition={!token && fallback}>
{fallback}
</Render>,
];
};
Authentication.defaultProps = {
fallback: null,
};
Authentication.isLogin = isLogin;
export default Authentication;
我需要编写一个带有笑话酶的测试来检查isLogin方法。
我做了这样的事情,但这没有任何意义:
import React from 'react';
import { mount } from 'enzyme';
import configureStore from 'redux-mock-store';
import { Provider } from 'react-redux';
import { Authentication } from '..';
const middleWares = [];
const mockStore = configureStore(middleWares);
const initialState = {
User: {
loading: false,
token: '',
userType: 'client',
error: {},
},
};
const store = mockStore(initialState);
describe('isLogin Method test', () => {
it('should checks user login state correctly', function() {
const wrapper = mount(
<Provider store={store}>{Authentication.isLogin()}</Provider>,
);
});
});