我有一个包含以下组件的最小测试React应用程序:importReactfrom'react';import$from'jquery';exportdefaultclassAppextendsReact.Component{componentDidMount(){console.log('componentDidMount',$('#helloDiv').length);}render(){returnHelloReact!;}}在浏览器(Chrome)中加载它时效果很好。componentDidMount()中的console.log()打印出1helloDivelementfoun
可以在自定义事件上使用Enzyme的方法.simulate()。例如://Code//Testconstelement=shallow();element.simulate('foo');这是应该使用Enzyme测试自定义事件的方式,还是使用s.th.的更好方法?喜欢://Testconstelement=shallow();element.props.onFoo() 最佳答案 似乎没有实现自定义事件的.simulate()。有一个issue在github上,讨论了这个主题,其中一位Enzyme维护者建议使用您提供的第二种方法:wr
我正在为我的应用程序寻求单元测试方面的帮助,我正在使用indexedDB。在我实现indexedDB功能之前,测试是正确的。但是现在,对于所有这些,我都看到一个错误:ReferenceError:indexedDBisnotdefined有人可以给我建议如何消除该错误吗?我正在搜索信息,并尝试了不同的方法来模拟window或indexedDB,但没有结果。 最佳答案 这个问题是由于Dexie期望window.indexedDB被定义,当以没有真正的DOM或的headless模式(使用Jest)运行时,情况并非如此窗口范围。在Dexi
所以我尝试学习React中的测试,我有这个:Button.js和Button.test.js问题连同下面的代码一起评论://Button.jsimportReactfrom'react';import{string,bool,func}from'prop-types';import{StyledButton}from'./styled'constButton=({size,text,})=>({text}//butthetextpropsishere.Thatismycurrentpracticeofpassingthepropstothechildren,amImissinganyt
我正在测试一个有条件地呈现包装组件的组件。我正在使用enzyme和jest,根组件是通过shallow()方法呈现的。问题是测试Root组件是否包含包装组件。如何在不使用mount()渲染方法的情况下测试包装组件是否存在?hoc.component.jsxexportfunctionHOC(Component){render(){return}}wrapped.component.jsxclassWrappedComponentextendsReact.Component{...}exportdefaultHOC(WrappedComponent)root.component.jsxc
我查看了各种解决类属性测试问题的建议,但都没有成功,我想知道是否有人可以更清楚地说明我可能出错的地方,这是我尝试过的所有测试错误预期的模拟函数已被调用,但它没有被调用。搜索.jsximportReact,{Component}from'react'import{func}from'prop-types'importInputfrom'./Input'importButtonfrom'./Button'classSearchFormextendsComponent{staticpropTypes={toggleAlert:func.isRequired}constructor(){sup
我想测试一个简单的组件是否呈现(因为我还在研究Jest)。应用程序本身使用webpack加载图像以显示Logo。当我尝试挂载/渲染/浅化无状态组件时,Jest抛出错误。FAILsrc/components/blog/blogList.spec.jsx●Testsuitefailedtorun/home/requinard/Projects/manus-frontend/src/img/manus_logo.png:Unexpectedcharacter'�'(1:0)>1|�PNG|^2|3|4|IHDR��G}pHYs.#.#x�?vtEXtSoftwareAdobeImageRea
假设我的React组件中有一个方法:doSomething(){//methodusesthis.propsandthis.state}我想针对设置的不同Prop和状态测试此方法。那我怎么调用它呢?MyClass.prototype.doSomething将调用该函数,但随后未设置this.props和this.state。 最佳答案 您可以使用enzyme的instance函数来获取渲染组件的实例,并在其上调用方法。constwrapper=shallow()wrapper.instance().doSomething()
我的主要组件的测试方法存在严重问题。经过多次重试后我的实际测试仍然不起作用,看起来像这样:describe(':',()=>{beforeEach(()=>{wrapper=mount();});describe('Interaction:',()=>{it('shouldcallArrowDown()',()=>{constinstance=wrapper.instance();spy=jest.spyOn(instance,'ArrowDown');instance.forceUpdate();wrapper.simulate('keyDown',{key:'Arrowdown'}
在我的单元测试中,我想测试父组件是否成功渲染了它的子组件。这是代码:describe('ParentComponent',()=>{it('rendersChildcomponent',()=>{constwrapper=shallow();expect(wrapper.find(Child).length).toEqual(1);});});家长:constParent=observer(({store})=>{constbookList=toJS(store.planets);return({bookList.map(book=>{return;})});});以上代码摘自here