【问题标题】:Jest/Enzyme can't find getState function in configured storeJest/Enzyme 在配置的存储中找不到 getState 函数
【发布时间】:2019-12-19 05:49:11
【问题描述】:

我刚刚开始为我们的应用程序编写测试并且被 redux 难住了。这就是现在的结构。

测试

import configureStore from 'redux-mock-store';
const initialState = require("./testUtils/testUtils")

const mockStore = configureStore(applyMiddleware(thunk), initialState);
describe('<Link/>', () => {
    test("Renders a link tag", () => {
        const wrapper = shallow(
             <Provider store={mockStore}> <Link href="" /> </Provider>
        ).dive({context: {mockStore}})

        expect(wrapper).toMatchSnapshot()
    })
})

组件

const Link = props => {
    const { href, ...rest } = props
    const absoluteLink = useAbsoluteLink(href)
    return <MaterialLink href={absoluteLink} {...rest} />
}

使用AbsoluteLink

export default link => {
    const hostName = useSelector(state => state.pageData.hostName)
    if (typeof link != 'undefined'
        && hostName
        && link.charAt(0) != '#'
        && !/^https?:\/\//i.test(link)
        && !/^\/\//.test(link)
    ) {
        link = hostName + link
    }

    return link
}

错误

● <Link/> › Renders a link tag

    TypeError: Cannot read property 'getState' of undefined

      24 | 
      25 |     test("Renders a link tag", () => {
    > 26 |         const wrapper = shallow(
         |                         ^
      27 |              <Provider strore={mockStore}> <Link href="" /> </Provider>
      28 |         ).dive({context: {mockStore}})
      29 | 

我已经尝试了我在研究过程中看到的所有内容,但没有任何解决方法。

【问题讨论】:

    标签: reactjs redux jestjs react-hooks enzyme


    【解决方案1】:

    您似乎错过了mockStore 调用。从doc 开始,从configureStore 你会得到函数nockStore,你应该再次调用它,将fake store 传递给它。

    // Initialize mockstore with empty state
    const initialState = { pageData: { hostName: 'some-host-name' } }
    const store = mockStore(initialState)
    

    产生的store 你应该传递给Provider

    【讨论】:

      猜你喜欢
      • 2019-09-26
      • 2017-06-25
      • 2019-10-16
      • 2020-12-17
      • 1970-01-01
      • 1970-01-01
      • 2019-01-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多