在Jest + Enzyme中使用useSelector测试HOC

时间:2020-05-20 11:35:54

标签: javascript reactjs redux jestjs enzyme

我是测试新手 有一个鹰,它有一个组件和可以访问它的组作为输入。从商店获取用户可用的组列表,并返回一个组件或null

import { useSelector } from 'react-redux'
import accessCheck from './accessCheck'

const RequireAccess = ({ Component, accesses }) => {
    const userGroups = useSelector((state) => state.currentUser.userData.groups)
    if (accessCheck(accesses, userGroups)) {
        return Component
    }
    return null
}

export default RequireAccess

我该如何测试? 我尝试了类似的方法,但显然是胡说八道

const initialState = {
        currentUser: {
            userData: {
                groups: ['cmgui', 'cmgui-settings'],
            },
        },
    }

    const ReduxProvider = ({ children, reduxStore }) => (
        <Provider store={reduxStore}>{children}</Provider>
    )

    const mockStore = configureStore()
    let store

    beforeEach(() => {
        store = mockStore(initialState)
    })

    test('should render component', () => {
        const SomeComponent = jest.fn()
        const accesses = ['cm1gui']
        const wrapper = shallow(
            <ReduxProvider reduxStore={store}>
                <RequireAccess
                    Component={SomeComponent}
                    accesses={accesses}
                />
            </ReduxProvider>
        )

        console.log(wrapper.debug())
    })

0 个答案:

没有答案