enzyme

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

妖精的绣舞 提交于 2019-12-04 07:22:27
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 = { toggleAlert: func.isRequired } constructor() { super() this.state = { searchTerm: '' } this

What is the difference between jest.mock(module) and jest.fn()

北战南征 提交于 2019-12-04 04:23:01
I have tried couple different ways of defining the mock function and all of my tries failed. When I try to define it as follow: jest.mock('../src/data/server', ()=> ({server: {report: jest.fn()}})); expect(server.report.mock).toBeCalledWith(id, data, () => {...}, () => {...}); I get the this error: expect(jest.fn())[.not].toBeCalledWith() jest.fn() value must be a mock function or spy. Received: undefined If I define the mock as : var spy = jest.mock('../src/data/server', ()=> ({server: {report: jest.fn()}})); expect(spy).toBeCalledWith(id, data, () => {...}, () => {...}); it returns the

travis cannot build because Error: Cannot find module 'react-test-renderer/shallow'

南楼画角 提交于 2019-12-04 03:33:17
问题 The npm run test works fine locally. However , travis insists that the build is failed and it shows the following log : react-test-renderer is an implicit dependency in order to support react@15.5+. Please add the appropriate version to your devDependencies. See https://github.com/airbnb/enzyme#installation No coverage information was collected, exit without writing coverage information /home/travis/build/abdennour/react-csv/node_modules/enzyme/build/react-compat.js:159 throw e; ^ Error:

jquery doesn't work with jsdom/enzyme

馋奶兔 提交于 2019-12-04 03:21:31
问题 I have a minimum test react app with following component: import React from 'react'; import $ from 'jquery'; export default class App extends React.Component { componentDidMount() { console.log('componentDidMount', $('#helloDiv').length); } render() { return <div id='helloDiv'> Hello React! </div>; } } this works fine when loading it in browser (Chrome). The console.log() in componentDidMount() prints out 1 helloDiv element found However, if I run the test using mocha + enzyme + jsdom, the

Jest + Enzyme: How to test an image src?

ぃ、小莉子 提交于 2019-12-04 02:23:38
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_PATH=src jest FAIL src/tests/Logo.test.js ● <Logo /> › renders an image TypeError: val.entries is not a

What is the best way to test Window Scroll Event Handlers with Enzyme?

旧城冷巷雨未停 提交于 2019-12-04 01:22:35
问题 I have been working on a React app with a new team and the discussion came up around writing unit tests for components that trigger methods on window.scroll events. So, let's take this component as an example. import React, { Component } from 'react'; class MyComponent extends Component { componentDidMount() { window.addEventListener('scroll', this.props.myScrollMethod); } componentWillUnmount() { window.removeEventListener('scroll', this.props.myScrollMethod); } render() { return ( <div> <h1

Error: It looks like you called `mount()` without a global document being loaded

旧街凉风 提交于 2019-12-04 00:39:52
问题 I'm trying to mount a component for testing with enzyme, and get this error. 回答1: Mocha doesn't run your test in a browser environment,so there is no DOM. To fix this problem, simply you have to use jsdom npm module to create a DOM. From Enzyme docs : Since enzyme's mount API requires a DOM, JSDOM is required in order to use mount if you are not already in a browser environment (ie, a Node environment). JSDOM is a JavaScript based headless browser that can be used to create a realistic

How to unit test React Component shouldComponentUpdate method

谁说我不能喝 提交于 2019-12-04 00:03:04
I have a React Component that implements the shouldComponentUpdate method and I'd like to unit test it. Ideally I could change some prop or state on the component in a unit test and verify it either re-rendered or not. I am using enzyme if that helps. I would probably just call shouldComponentUpdate directly. Something like const comp = shallow(<Comp {...props} />) const shouldUpdate = comp.instance().shouldComponentUpdate(nextProps, nextState) expect(shouldUpdate).toBe(true/false) Trying to test by determining if the component actually rendered/didn't render is probably more trouble than it's

Jest - how to test if a component does not exist?

只谈情不闲聊 提交于 2019-12-03 22:07:42
How do I check if a component is not present, i.e. that a specific component has not been rendered? Andreas Köberle You can use enzymes contains to check if the component was rendered: expect(component.contains(<ComponentName />)).toBe(false) Artem Kislov .contains receives a React Node or array of Nodes as an argument. Instead, use .find : expect(wrapper.find('selector').exists()).toBeTruthy() Providing a slightly updated answer based on the documentation for toExist function Fixture() { return ( <div> <span className="foo" /> <span className="bar baz" /> </div> ); } const wrapper = mount(

Jest Asynchronous API Mocking

假装没事ソ 提交于 2019-12-03 21:55:35
I have searched this issue on stack-overflow, but couldn't find anything similar to me use case. I have container component like this. import React, { Component } from 'react'; import PropTypes from 'prop-types'; // API import BookingAPI from '../../../../api/BookingAPI'; class CustomerProfilePage extends Component { state = { list: [], totalRecords: 0, pageNo: 1, }; componentDidMount() { const { pageNo } = this.state; this.onGetBookingList({ pageNo }); } onGetBookingList = async ({ pageNo = 0 }) => { const { match } = this.props; try { const result = await BookingAPI.onGetBookingList({