enzyme

Enzyme integration testing testing with react router - testing components update with links

 ̄綄美尐妖づ 提交于 2019-12-11 15:20:00
问题 I would like to test that clicking on a link updates the components in my app. Here's my app, when you click on about it renders the About component import React, { Component } from 'react'; import './App.css'; import { MemoryRouter as Router, Route, Link } from 'react-router-dom' const Home = () => <h1>home</h1> const About = () => <h1>about</h1> class App extends Component { render() { return ( <Router> <ul> <li><Link to="/about">About</Link></li> <li><Link to="/">Home</Link></li> </ul>

Should I mount using beforeEach() and set props for each test or mount a specific component at the start of each test? (using Enzyme, Chai and Mocha)

情到浓时终转凉″ 提交于 2019-12-11 15:14:10
问题 I've recently starting programming. I'm on a team who is programming in React and is using Enzyme, Mocha and Chai for unit testing. See package versions below. When testing a component where there are several use cases that require different prop values, should I use beforeEach() and then setProps() in each test, or should I do an explicit mount() (or shallow()) at the start of each test? Does it matter? For example, I could use beforeEach() to mount without any props and then use setProps()

Jest not able to verify presence for a proeprty using toHaveProperty

时光毁灭记忆、已成空白 提交于 2019-12-11 15:07:47
问题 I am using Jest and enzyme, I have a react component, below its structure when performing .debug() . console.log src\shared\navigation\Navigations.test.js:20 <NavLink to="/" exact={true} activeStyle={{...}} activeClassName="active"> Weather </NavLink> I am using the following code to try to test if NavLink has a property to with value / . The property is not found (I believe when the component is mounted using shallow the object is decorated with other properties [code below]). I need to know

why actual and expected length is not equal in react js

老子叫甜甜 提交于 2019-12-11 14:56:14
问题 could you please tell me why actual and expected length is not equal in react js here is my code https://codesandbox.io/s/oq7kwzrnj5 mockAxios.get.mockImplementation(() => Promise.resolve({ data: { data: ['a','b'] } })); I am sending 2 elements on array but getting 0 element it("axios call check or not", async () => { const wrapper = shallow(<List />); expect(mockAxios.get).toHaveBeenCalledTimes(1); expect(wrapper.find("li").length).toBe(2); }); 回答1: You are making async call in

SyntaxError: Use of reserved word 'import' running enzyme with karma

Deadly 提交于 2019-12-11 14:08:57
问题 Tried unit testing my React application with Enzyme v3, did not work. PFB the details: modules installed: "enzyme": "^3.1.1", "enzyme-adapter-react-15.4": "^1.0.5", Created a file enzyme.config.js : import Enzyme from 'enzyme'; import Adapter from 'enzyme-adapter-react-15.4'; Enzyme.configure({ adapter: new Adapter() }); Included the above file in my karma runner conf: files: [ // test setup ........... 'test/unit/testutils/enzyme.config.js', .......... ] Got following error: 14 11 2017 16:21

Enzyme test: TypeError: expect(…).find is not a function

半世苍凉 提交于 2019-12-11 11:54:29
问题 Why is .find not a function in the code context below? import React from 'react'; import { shallow } from 'enzyme'; import toJson from 'enzyme-to-json'; import { AuthorizedRoutesJest } from './AuthorizedRoutes'; // Components import { Main } from '../../components'; const wrapper = shallow(<AuthorizedRoutesJest />); describe('<AuthorizedRoutes /> component', () => { it('should render', () => { const tree = toJson(wrapper); expect(tree).toMatchSnapshot(); expect(wrapper).toHaveLength(1); });

How can I test an internal method inside a component in React?

浪子不回头ぞ 提交于 2019-12-11 11:54:20
问题 I am trying to test spy a method used inside a React component but I am failing. The component is the below : export class SiderMenu extends PureComponent { formatter(data, parentPath = '', parentAuthority) { return data.map((item) => { const result = { ...item, path: `${parentPath}${item.path}`, }; return result; }); } constructor(props) { super(props); this.menus = this.formatter(props.menuData); } render(....) } I am using enzyme and I want to test that the formatter method has been called

Why does my test finish before my (enzyme simulated event) synchronous event handler?

我怕爱的太早我们不能终老 提交于 2019-12-11 08:12:17
问题 I have a mocha based test which finishes before my onChange handler in a jsdom based enzyme test of my React component, despite that handler being synchronous using babel+ES2017. If I do a setTimeout() of 1ms to put my expect() calls in; the test passes . Just wondering where the break down is? I'm sure there is some simple concept here I'm not considering. I'm thinking jsdom or enzyme does not wait around for the event handler to finish? A problem compounded by the length of time mocking

writing test for service calls in enzyme - react

北战南征 提交于 2019-12-11 08:06:25
问题 how can i write the test cases for the below component using jest. should i mock the import files. // App.js import React, {Component} from "react"; import { BrowserRouter as Router, Route, Switch } from "react-router-dom"; import Layout from "../Layout"; import Home from "../components/pages/Home"; import Users from "../components/pages/Users"; import UserList from "../components/pages/UserList"; import AppContext from "../components/context/App/index"; import Loader from "../components

jest encountered unexpected token

我是研究僧i 提交于 2019-12-11 08:04:32
问题 I am facing this issue. after running my unit test. This same error occurs wherever the static variable present. do you have any solution for this? or what causing this issue similarly, the same error showing for the auto binding too. ex: handle = ()=>{ } 回答1: finally, I found the solution. add this below lines in the babelrc file "presets": ["env", "react"], "plugins": ["transform-class-properties"] And also if someone facing below issue, Kindly check your node version. it should be 8+