问题标签 [reactjs-testutils]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
reactjs - 反应测试:浅渲染 - getRenderOutput 返回 null
我刚刚开始进行反应测试,尝试使用浅层渲染。
我不知道这里发生了什么 - 我相信我的代码遵循我在示例中看到的所有说明(例如http://racingtadpole.com/blog/test-react-with-jest/)
但我越来越空了shallowRenderer.getRenderOutput();
另外我很困惑,因为当我注销 shallowRenderer 时,我没有看到任何 getRenderOutput 方法?
结果输出:
javascript - nextState on componentWillUpdate not correct while testing with Jest (using also react-router wrapper)
I am using Jest 0.4.0. I have a component wrapped into this (from react-router docs):
My component uses componentWillUpdate
:
In my test:
As much as I call setState
, my nextState
in componentWillUpdate
is always something: ""
. However, in the test, if I check the content of myComponentInstance.state
then it is something: "12345"
. So basically, componentWillUpdate
gets called but not having the new state even my instance component has it.
Any ideas on this?
--
EDIT 1
Below suggestions are based on setState being asynchronous function but that didn't solve the problem. I was also trying to simulate a store change (Flux pattern) in this way:
Well that didn't work either, actually was telling me that onChange
is not defined. So my problem is with the react-router wrapper. I found a solution but I am not sure if there are better ones cause this one looks very hacky. It is the following:
In this way, both myComponentInstance.setState
or simulating a myComponentInstance.onChange
mocking the store work and I don't need to use asynchronous functions.
javascript - 无法在简单的 React/Jest 测试中调用未定义的方法“toUpperCase”
被这个给难住了。。
我正在尝试测试组件是否已成功呈现。测试看起来像:
和组件:
运行测试会产生以下输出:
我在需要 React 的测试之前运行了一个脚本,它unmockedModulePathPatterns
在我的package.json
. 见下文:
该组件在真实应用程序中呈现良好。
使用节点调试我已经确定测试可以运行render
,但不是componentDidMount
但除此之外我没有任何进一步的信息或关于尝试什么的想法!
highcharts - HighChart 测试反应错误#13
我正在使用使用 React 创建的 High Chart 组件。我得为它写一个测试,基础设施是Jasmine/Karma/React TestUtils。
我写了以下代码:
我一直收到 Error#13 http://www.highcharts.com/errors/13(在“EmailActivityBreakdown”对象中实例化图表组件时)。
组件通常可以正常工作(即在非测试用例模式下)。如何在 ReactTest 框架内创建带有“renderTo”的图表元素才能工作?
谢谢。
reactjs - 点击时使用 Boostrap Modal 测试 React
我从反应引导网站得到了这个简单的代码......
这是我的测试...简单我想单击启动按钮,验证模态打开,然后单击取消按钮以验证模态不再打开。
请问如何找到取消按钮?我尝试了各种各样的东西,似乎没有什么对我有用。
unit-testing - 模拟 JEST 单元测试的文档上的 keydown
用于对文档附加了 keydown 侦听器JEST
的组件进行单元测试。
我该如何测试JEST
呢?如何模拟文档上的 keydown 事件?我需要将事件侦听器放在文档上,因为它应该响应键盘操作而与焦点元素无关。
编辑:这里的问题是关于模拟文档或 document.body 上的事件。所有示例都是关于一个实际的 DOM 节点,它可以正常工作,但文档不能。
目前正在尝试这样做:
reactjs - 如何测试子组件从父级的 getChildContext() 获取正确的上下文?
我有一个具有 getChildContext() 方法的组件和接收从父级道具派生的上下文的子级。
我如何测试他们是否收到了它?
这是组件的代码......一些选项卡组件。
reactjs - Sinon.stub 在测试中被调用,但期望被调用语句失败
我有一个无状态的反应组件,它有一个我正在尝试测试的 onClick 方法被调用。
我这样做的方法是设置一个带有默认值的道具,指向将在生产中调用的方法,这样我就可以通过 sinon.stub() 在我的测试中测试点击事件。
所以这一切都很好。存根被调用。我知道这一点,因为我添加.returns(console.log("called"));
到存根并在控制台中看到它打印出来。
但是当涉及到 时expect(onClickStub).to.have.been.called
,我的测试失败了,说它没有被调用。
我的代码涉及一个无状态组件,所以我用一个快速的 TestWrapper 类包装它以进行测试。我不确定上下文是如何工作的,但我认为这对于测试并不是完全必要的。
一切都在这里:
reactjs - 开玩笑的问题 - ReactElement.js:无法获取模拟元数据
我有一个非常简单的测试
只要我不需要TestUtils,测试就可以正常工作。添加任何 node_module 引用的那一刻,我开始看到以下错误
我敢肯定很多人都在开玩笑,而我错过了一些愚蠢的东西..appreaciate任何想法来解决这个问题?
html - ReactTestUtils.renderIntoDocument 一个带有子组件的组件 - 子组件似乎“不在 DOM 中呈现”
我在我的测试中渲染一个组件,如下所示:
这是来自SlidingContainer
的渲染方法
如您所见,$bodyElement
是一个包含子元素的 DOM 元素。它有一个固定的宽度,空白:nowrap,溢出:隐藏
这是updateScrollState
`SlidingContainer' 的方法
此代码在我在浏览器中测试时有效,但在运行 karma/jasmine 单元测试时,bodyElement.scrollWidth
两者bodyElement.offsetWidth
都等于 0。我四处搜索,似乎这是一个元素仅在内存中呈现且没有真实 DOM 的问题,即使renderIntoDocument
应该将元素渲染为dom。