Open yanlele opened 5 years ago
不错
@yanlele 关于dva做的模块管理如何解决store注入: models, globalErrorHandler是自定义的吗? 刚接触react, dva没多久,不太清楚全局的models怎么写。我现在项目里的model都是单个的。 谢谢🙏
@paul-3 models 是dva 定义的一个存储模块单元, 符合redux ducks 模块规范定义的一个状态管理单元而已。 建议把 state、reducer/action、effect等集中管理在一个文件里面。 globalErrorHandler 是自定义的。
因为我用的是dva做状态管理, 我了解的dva解决注入store 有三种解决方式: 1、伪造一个虚假的store ;2、有设计到store 的都直接略过; 3、就是从直接从绑定了models 的 dva 示例中 拿到 _store;
上面文章中有这三种方式的一个描述。欢迎多交流。
@yanlele 好的,大概了解了。谢谢!
想用这个终极解决办法真的是困难重重,最后在它app.start('#root'),报错[app.start] container null not found 的时候放弃了、、还是老实的用前面的方法
@yanlele 请问一下我使用你dva处理方案dva.setup,执行到app.start时报[app.start] extraReducers is conflict with other reducers ,reducers list :router , @@dva,请问这个应该怎么解决呢?
Guys, please stop mention me =)
@dva sorry ,But the error message is displayed like this, : (
@YearLiu 请问你是还用了其他的 状态管理 模块?
@yanlele 我们目前框架来说状态管理采用的是dva的那一套,用@connect注入到页面上,方便的话可以看一下你 import models from '../models'; 这个文件是怎么写的吗?
@yanlele 我们目前框架来说状态管理采用的是dva的那一套,用@connect注入到页面上,方便的话可以看一下你 import models from '../models'; 这个文件是怎么写的吗?
@YearLiu 非常尴尬,已经找不到源码了。 但是这个 models 就是正常的你注册到 dva app model 的那个 models额。
@yanlele 那请问你对我遇到的这个问题有什么猜想或者头绪吗?我目前还没有成功解决这个问题
@yanlele 你好,我想问下,如果我的models不是单个手动注入而是通过webpack提供require.context批量导入后注入,这种情况下该怎么写测试呢?
ts测试jest
常见做法
第一步安装包:
yarn add -D typescript jest ts-jest @types/jest
创建jest.config.js:
base.spec.ts:
关于sessionStorage和localStorage的测试
在jest的最新版本中, 其实已经通过jsdom模拟了 sessionStorage和localStorage 的实现
然后测试的时候, 需要做异步存储, 要不然是不能获取到 存入的值。
例如有这样一个方法:
我们写测试用例的时候, 可以这样写:
组件测试测试中忽略样式加载和文件加载
在配置jest.config.js中加上这两句
然后对应文件分别为: cssTransform.js:
fileTransform.js:
测试中关于antd组件报undefined的问题
这个情况只会出现在 生成覆盖文件时候会产生
这个问题不是个例, antd issue 里面有很多这种情况的讨论。
解决办法:
1、在.babelrc 文件里面 删除import-plugin 配置
2、关闭webpack react生产模式核心压缩功能即不会报错
remove these:
3、获取组件不要用解构 ReferenceError: Layout is not defined
While:All fine...
Could not find "store" in either the context or props of "Connect(XXX)"
const mockStore = configureMockStore(); const store = mockStore({});
describe("Testpage Component", () => { it("should render without throwing an error", () => { expect( shallow(
});
这种 方式存在的问题是 实际上是伪造了一个context.store, 这个store的功能是不足以让程序去获取真正redux 里面的任何数据, 也无法存储任何数据。
所以不建议用这种方法
Testing with Jest and Webpack aliases
配置方式如下, 直接在jest.config.js 中添加如下配置就可以了:
如何略过redux
下面是来自于stack overflow 的解决方案
This is happening because you're trying to test the component without a
What I normally do in this situation is test my component without the Redux connect binding. To do this, you can export the App component itself:
and then import that in the test file using the deconstructed assignment syntax:
You can assume (hopefully... ;) ) that Redux and the React bindings have been properly tested by their creators, and spend your time on testing your own code instead.
关于一个测试报错的问题
直接上码说话
组件如下 JestAppTest.jsx
测试如下 index.test.js
执行命令, 总是会报错
但是我这里明显就有这个p 标签, 然后却说找不到p标签
原因: 这个地方最重大的原因是因为 connect 本身也是一个高阶组件, 改变了上下文, 渲染方式 shallow和mount 的区别
shallow只渲染当前组件,只能能对当前组件做断言; mount会渲染当前组件以及所有子组件,对所有子组件也可以做上述操作。 一般交互测试都会关心到子组件,我使用的都是mount。但是mount耗时更长,内存啥的也都占用的更多, 如果没必要操作和断言子组件,可以使用shallow。
所以上诉情况下, 直接把 shallow 渲染改为 mount 渲染即可。
dva单元测试
如果项目是用的dva做的模块管理如何解决store注入
存在的问题:我把dva作为独立模块加入到自己的项目里面做状态管理。 用enzyme+jest给react 做测试的时候, 如何处理 Invariant Violation: Could not find "store" in either the context or props of "Connect...." 的问题?
error console.log
test code:
这个问题的终极解决办法:
dva.setup.js
测试代码:
处理生命周期中的请求
很多请求接口是放置在componentDidMount等生命周期的, 然后通过接口请求, 异步渲染了组件。这个时候就涉及到我们需要模拟这个过程。 否则是拿不到异步渲染的结构的, 通过enzyme也是无法渲染出来。
异步action
异步action测试 可以稍微了解一下这个模块 redux-mock-store
参考文章