所以我对 redux-store 的实现可能不是最好的,而且我对 Jest 的了解也是最少的..
我有三个文件,其中包括...
我的测试文件如下所示:
// file.test.js
jest.mock( 'store', () => jest.fn() )
// external
import React from 'react'
import configureStore from 'redux-mock-store'
// internal
import Component from 'components/Component'
import store from 'store'
describe( 'Test My Component', () => {
const mockStore = configureStore()
it ( 'should equal true', () => {
const initialState = { active: true }
store.mockImplementation(() => mockStore( initialState ))
expect( store.getState().active ).toBe( true )
} )
} );
我嘲笑商店的原因是因为在
<Component />
内部我使用的模块本身导入相同的store
并拥有一些正在使用store.getState()
...的函数
所以返回的是
TypeError: _store2.default.getState is not a function
我确实通过模拟模块调用(由
<Component />
使用)来解决这个问题,但我觉得我“应该”能够做到这一点,因为我有不同的初始状态,我想尝试而不是全部取决于模块。
这是我关于SO的第一篇文章,如果我需要澄清任何事情,请告诉我!
不要导入商店。我们想嘲笑商店:
const store = mockStore(initialState);