How can I test a change handler for a file-type input in React using Jest/Enzyme?

This answers shows how to access all of the different parts of the code using jest. However, it doesn’t necessarily mean that one should test all of these parts this way. The code-under-test is essentially the same as in the question except that I have substituted addEventListener(‘load’, … for onload = …, and I have … Read more

How to mock react-router context

Thanks @Elon Szopos for your answer but I manage to write something much more simple (following https://github.com/airbnb/enzyme/pull/62): import NavLink from ‘../index’; import expect from ‘expect’; import { shallow } from ‘enzyme’; import React from ‘react’; describe(‘<NavLink />’, () => { it(‘should add active class’, () => { const context = { router: { isActive: (a, … Read more

How to verify React props in Jest and Enzyme?

You need to find the StyledButton in the Button component before asserting the props // Button.test.js import React from ‘react’; import { shallow } from ‘enzyme’; import Button from ‘../../components/Button/Button’; import { StyledButton } from ‘./styled’ describe(‘Component: Button’, () => { const minProps = { text: ”, size: ”, }; it(‘renders a button in size … Read more

Unit testing React click outside component

import { mount } from ‘enzyme’ import React from ‘react’ import ReactDOM from ‘react-dom’ it(‘Should not call action on click inside the component’, () => { const map = {} document.addEventListener = jest.fn((event, cb) => { map[event] = cb }) const props = { actions: { something: jest.fn(), } } const wrapper = mount(<Component {… … Read more

Jest / Enzyme – How to test at different viewports?

Background Information: jsdom does not implement window.resizeBy() or window.resizeTo() jsdom defines the window innerWidth and innerHeight to be 1024 x 768 It is possible to simulate a window resize using jsdom by manually setting window.innerWidth and window.innerHeight and firing the resize event Here is an example: comp.js import * as React from ‘react’; export default … Read more

Nested components testing with Enzyme inside of React & Redux

Enzyme’s mount takes optional parameters. The two that are necessary for what you need are options.context: (Object [optional]): Context to be passed into the component options.childContextTypes: (Object [optional]): Merged contextTypes for all children of the wrapper You would mount SampleComponent with an options object like so: const store = { subscribe: () => {}, dispatch: … Read more

Check if child component rendered – Jest, Enzyme

You can check whether a parent component has rendered its child component using containsMatchingElement(). Based on Enzyme docs: Returns whether or not a patternNode react element matches any element in the render tree. Your test should look like this: describe(‘Parent Component’, () => { it(‘renders Child component’, () => { const wrapper = shallow(<Parent store={store} … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)