enzyme

Redux How to update the store in unit tests?

拜拜、爱过 提交于 2019-12-06 19:19:38
问题 Using enzyme, mocha and expect asserts. The aim of my unit test is to check that dispatch gets called with the correct arguments when paused and not paused in mergeProps. I need to dynamically change the state of my store to do: paused: true . At the moment I try and update the paused value by dispatching but I don't think this is correct because it's just a mock and never actually runs through the reducer. I am using the package redux-mock-store. How do I do this? describe('Play Container',

Test whether React component has rendered

匆匆过客 提交于 2019-12-06 13:46:18
I have a React component that I am trying to test using Enzyme/Jest. I am trying to figure out what the most appropriate test would be to ensure the component has rendered. My component has a prop shouldRender that, if false, will cause the component to not render. My component looks like this: import React from 'react'; const propTypes = { shouldRender: React.PropTypes.bool, }; class MyComponent extends React.Component { constructor(props) { super(props); this.state = { foo: 'bar', }; } render() { if (!this.props.shouldRender) { return null; } return ( <div> <span>My component</span> </div> )

TypeError: dispatch is not a function when testing with react-create-app jest and enzyme

天涯浪子 提交于 2019-12-06 11:43:33
I'm trying to setup testing on a new project created with react-create-app. Which now seems to be using React 16 and Jest 3 (which supposedly had some breaking changes, or maybe that was enzime). I'm getting an error similar to this post TypeError: dispatch is not a function when I try to test a method using JEST TypeError: dispatch is not a function at App.componentDidMount (src/components/App.js:21:68) import React from 'react'; import { Provider } from 'react-redux'; import { mount } from 'enzyme'; import { App } from '../components/App'; import configureStore from '../state/store

Testing JSX inside .map function using Enzyme

自作多情 提交于 2019-12-06 07:50:02
So say i have this table: <table> <thead> <tr> <th>cat name</th> </tr> </thead> <tbody> {cats.map(cat => { return ( <tr key={cat.id}> <td styleName="table-item">{ cat.name }</td> </tr> ); })} </tbody> </table> How do i go about testing the jsx inside the map function with enzyme? I am able to test the other parts of it fine, like so: describe('<CatsTable />', () => { const wrapper = mount( <CatsTable cats={cats} /> ); it('renders correctly', () => { expect(wrapper.find('table')).to.have.length(1); expect(wrapper.find('tbody')).to.have.length(1); }); }); But if i try the same thing with what's

Enzyme async await mock function not being called

吃可爱长大的小学妹 提交于 2019-12-06 05:44:27
I'm trying to test an async await function, however im getting an error. ● Should handle getGIF event › should handle getGIF event expect(jest.fn()).toHaveBeenCalledTimes(1) Expected mock function to have been called one time, but it was called zero times. I'm unsure how to test async await functions, so i was using this blog as an example https://medium.com/@rishabhsrao/mocking-and-testing-fetch-with-jest-c4d670e2e167 App.js import React, {Component} from 'react'; import logo from './logo.svg'; import './App.css'; import Card from './Card'; import PropTypes from "prop-types"; const Styles = {

Jest Expected mock function to have been called, but it was not called

旧时模样 提交于 2019-12-06 01:59:34
问题 I've looked at various suggestions to solve testing a class property with no success and was wondering if anyone could possibly cast a little more light on where I may be going wrong, here are the tests I've tried all with the error Expected mock function to have been called, but it was not called. Search.jsx import React, { Component } from 'react' import { func } from 'prop-types' import Input from './Input' import Button from './Button' class SearchForm extends Component { static propTypes

How to unit test localStorage using sinon

好久不见. 提交于 2019-12-06 01:57:39
I am trying to test localStorage using sinon. Basically I am very new to unit testing so this might be very basic. Update I managed to come up with this but now its giving me a new error Should wrap property of object Test describe('Initial State', () => { it('should set the initial state for the component', () => { const props = { currentUser: {} }; sinon.stub(window.localStorage, 'setItem'); window.localStorage.setItem('none', 'nothing'); }); }); Umair Sarfraz I managed to resolve it. Thanks to @anoop because his answer was of help but I had to manage it with a major workaround. I am using

Jest + Enzyme: How to test an image src?

孤人 提交于 2019-12-05 18:44:17
问题 I have a Logo component: import React from "react"; import logo from "assets/images/logo.png"; const Logo = () => { return <img style={{ width: 50 }} src={logo} alt="logo" />; }; export default Logo; Test file: import React from "react"; import Logo from "components/shared/Logo"; describe("<Logo />", () => { it("renders an image", () => { const logo = shallow(<Logo />); expect(logo.find("img").prop("src")).toEqual("blahh"); }); }); But when I run the test, there is some weird error: $ NODE

enzyme: TypeError: Adapter is not a constructor

混江龙づ霸主 提交于 2019-12-05 18:10:16
问题 Hi I was trying to test the react application With enzyme, But it throws an error TypeError: Adapter is not a constructor , Any Idea This is my test file import ProductRow from '../product_row'; import React from 'react'; // import { mount } from 'enzyme'; import * as enzyme from 'enzyme'; import * as Adapter from 'enzyme-adapter-react-16'; enzyme.configure({ adapter: new Adapter() }); test('TodoComponent renders the text inside it', () => { const wrapper = enzyme.mount( <ProductRow item={{}}

How to write jest test cases for navigation in react

此生再无相见时 提交于 2019-12-05 13:49:54
I am new to Jest test cases writing, Trying to write the test case for checking navigation in an react application. Currently I have written test case which works like below: I have Login page which is having register link on it, which redirects user to the Register page. So what I am checking is 1. Loading the login page 2. Triggered Register link click event 3. So user moved to the Register page but I am not able to check if the Register page is loaded or not? Is there any way to check the snapshot of the "Register" page and link in URL. I have used jest, enzyme and react-test-render as