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

React Jest causing “SyntaxError: Unexpected token .”

Have a look at the Jest documentation for Webpack integration. The problem is that Jest can’t work with other stuff than JavaScript. So you have to mock all non-JavaScript files you import. The easiest way is to configure a moduleNameMapper in your Jest configuration. { “jest”: { “moduleNameMapper”: { “\\.(css|scss)$”: “<rootDir>/__mocks__/styleMock.js” } } } With … Read more

How to test a React component with RouteComponentProps?

To answer your last question, the recommended approach is to use <MemoryRouter>< *your component here* ></MemoryRouter> in your tests. Typescript does not pick up that this component will pass the required props to your component, as such I assume it not to be a type safe approach. This is for React Router v4 and doesn’t … Read more

what is the best way to mock window.sessionStorage in jest

Here is the solution only use jestjs and typescript, nothing more. index.ts: export function getUserInfo() { const userInfo = window.sessionStorage.getItem(‘userInfo’); if (userInfo) { return JSON.parse(userInfo); } return {}; } index.spec.ts: import { getUserInfo } from ‘./’; const localStorageMock = (() => { let store = {}; return { getItem(key) { return store[key] || null; }, … Read more

The difference between Vue-Test-Utils’ “mount” and “shallowMount”?

What the documentation means by “stubbed child components” is that every components within the tested one will not be rendered. Instead, you will have a placeholder component. This prevent your tests to be parasite by other component’s behaviors. In my opinion, you should always shallow mount your components when doing unit tests, and simply mount … Read more

Found multiple elements error in React Testing Library

Just a quick tip, if you have multiple matching elements, you can query like this: HTML: <a href=”/my-element”>My element</a> <a href=”/my-element”>My element</a> TEST: test(‘renders my element’, () => { let link = screen.getAllByText(‘My element’)[0] as HTMLAnchorElement; expect(link.href).toContain(‘/my-element’); });

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

Why Jest’s toThrow won’t work when create an instance of a ES6 class directly in the constructor?

Here expect(new TestObject()).toThrow(); new TestObject() is evaluated first, then expect(…), then …toThrow(), in accordance with operator precedence. When new TestObject() throws, anything else doesn’t matter. This is why toThrow expects a function that is supposed to throw: expect(() => { new TestObject(); }).toThrow(); This way it can be wrapped with try..catch internally when being called. … Read more

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