enzyme

How to mock a React component lifecycle method with Jest and Enzyme?

别说谁变了你拦得住时间么 提交于 2019-12-03 03:22:01
The Enzyme docs for Full DOM Rendering here contains the following example of spying on a lifecycle method with Sinon: describe('<Foo />', () => { it('calls componentDidMount', () => { sinon.spy(Foo.prototype, 'componentDidMount'); const wrapper = mount(<Foo />); expect(Foo.prototype.componentDidMount.calledOnce).to.equal(true); }); }); What is the equivalent to this using mock functions from Jest? I'm using Create-React-App, and would rather not include Sinon if the same can be achieved with Jest. Here's what I'd expect the test to look like: describe('<App />', () => { it('calls

Access nested State in Enzyme

泪湿孤枕 提交于 2019-12-02 22:33:46
问题 My state looking like this this.state = { potato: { chips: 'yum', fries: 'even better', } } Then I want to access fries . wrapper.state('potato') get me to the first level, how to go deeper? It is not wrapper.state('potato').state('fries') wrapper.state('potato', 'fries') wrapper.state(['potato', 'fries']) wrapper.state('potato').fries wrapper.state('potato')['fries'] When I do const potato = wrapper.state('potato'); and then console.log(potato); I get { chips: 'yum', fries: 'even better', }

React Native with Typescript and Jest is broken after 0.57 Update: Couldn't find preset “module:metro-react-native-babel-preset” relative to directory

こ雲淡風輕ζ 提交于 2019-12-02 20:45:16
If you integrate test with Jest and Enzyme in the new React Version 0.57 and TypeScript, they won't work. Here are the steps to reproduce: Create a new React Native project: react-native init MyApp -package "com.my.app" --template typescript && node MyApp/setup.js Install all Jest and Enzyne related packages: npm install --save-dev react-dom enzyme enzyme-react-adapter-16 jest-fetch-mock ts-jest Add the jest configuration: "jest": { "preset": "react-native", "moduleFileExtensions": [ "ts", "tsx", "js" ], "transform": { "^.+\\.(js)$": "<rootDir>/node_modules/babel-jest", "\\.(ts|tsx)$": "

How to mock react-router context

陌路散爱 提交于 2019-12-02 20:17:44
I've got fairly simple react component (Link wrapper which adds 'active' class if route is active): import React, { PropTypes } from 'react'; import { Link } from 'react-router'; const NavLink = (props, context) => { const isActive = context.router.isActive(props.to, true); const activeClass = isActive ? 'active' : ''; return ( <li className={activeClass}> <Link {...props}>{props.children}</Link> </li> ); } NavLink.contextTypes = { router: PropTypes.object, }; NavLink.propTypes = { children: PropTypes.node, to: PropTypes.string, }; export default NavLink; How am I supposed to test it? My only

Nested components testing with Enzyme inside of React & Redux

谁说胖子不能爱 提交于 2019-12-02 19:06:10
I have a component SampleComponent that mounts another "connected component" (i.e. container ). When I try to test SampleComponent by mount ing (since I need the componentDidMount ), I get the error: Invariant Violation: Could not find "store" in either the context or props of "Connect(ContainerComponent)". Either wrap the root component in a , or explicitly pass "store" as a prop to "Connect(ContainerComponent)". What's the best way of testing this? Enzyme's mount takes optional parameters. The two that are necessary for what you need are options.context: (Object [optional]): Context to be

eslint should be listed in the project's dependencies, not devDependencies

纵饮孤独 提交于 2019-12-02 19:02:29
Either I don't understand dependencies vs. devDependencies in node 100% yet or eslint is just wrong here (not capable of analyzing this correctly): 3:1 error 'chai' should be listed in the project's dependencies, not devDependencies import/no-extraneous-dependencies 4:1 error 'chai-enzyme' should be listed in the project's dependencies, not devDependencies import/no-extraneous-dependencies 5:1 error 'enzyme' should be listed in the project's dependencies, not devDependencies import/no-extraneous-dependencies 7:1 error 'sinon' should be listed in the project's dependencies, not devDependencies

can we emulate print mode (media query print) for unit testing?

橙三吉。 提交于 2019-12-02 11:31:18
问题 can we emulate print mode (media query print) for unit testing? (Ract, enzyme, jest) i try: window.matchMedia('print') but it does not work for me, style media print not applied; 回答1: I use iframe for printing and added some param to url for that. We can test printing using thit param: window.location.hash = '?printMode=true'; // my variant after that run component and lets try to write tests. 来源: https://stackoverflow.com/questions/47219643/can-we-emulate-print-mode-media-query-print-for

Enzyme onclick spy toHaveBeenCalled test does not work when testing on arrow function

隐身守侯 提交于 2019-12-02 10:13:02
问题 how can i test the child component onclick. Please see the below snippet. // App.js import React, {Component, Fragment} from 'react' import Child from './child' class App extends Component{ state = { data: null, enable: false } componentDidMount(){ this.getData() } getData = async () => { const response = await fetch('http://www.example.com'); const data = await response.json(); this.setState({ data }) } _handleChildClick = () => { this.setState({ enable: true }) } render(){ const {data,

When spying on component method, called from componentDidMount, the spy is never called

雨燕双飞 提交于 2019-12-02 09:46:53
In a React component I have export default class MyComp extends Component { ... componentDidMount() { this.customFunc(); } customFunc = () => { // .. } ... } And when I try to test this method with Jest and Enzyme like this: it('Should call customFunc on mount', () => { const MyCompInstance = mount(<MyComp {...props} >).instance(); const spy = jest.spyOn(MyCompInstance, 'customFunc'); expect(spy).toHaveBeenCalled(); }); it fails with Expected mock function to have been called, but it was not called. The funny thing is that if I put console.log() in componentDidMount and in custsomFunc - they

Creating HTML elements for javascript testing

泄露秘密 提交于 2019-12-02 09:45:59
问题 So i have a particular problem, inside my react component i used commands like these: document.getElementById('modalsContainer').appendChild(recognitionProfileZoom); document.getElementById('modalsContainer').appendChild(categoryZoom); and: document.getElementById('cddoccategoryzoom').value; But these elements that were specified by ID don't exist in my component. How would one go about creating these objects on the tests? The code below would use these elements but it fails because they don