【发布时间】:2017-06-12 19:58:47
【问题描述】:
所以我对 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 的第一篇文章,如果我需要澄清任何事情,请告诉我!
【问题讨论】:
标签: javascript unit-testing jestjs redux-store