enzyme

Enzyme expects an adapter to be configured

北战南征 提交于 2019-11-29 10:39:23
问题 I created a new React application by create-react-app and I wanted to write a unit test to a component named "MessageBox" that I created in the application. This is the unit test that I wrote: import MessageBox from "../MessageBox"; import { shallow } from 'enzyme'; import React from 'react'; test('message box', () => { const app = {setState: jest.fn()}; const wrapper = shallow(<MessageBox app={app}/>); wrapper.find('button').at(0).simulate('click'); expect(app.setState)

How does one access state on a nested React component wrapped by an HOC?

跟風遠走 提交于 2019-11-29 06:17:44
问题 I'm using Enzyme, and we can actually use the example component given in the docs as a foundation for my question. Let's assume this <Foo /> component uses a <Link> component from ReactRouter and thus we need to wrap it in a <MemoryRouter> for testing. Herein lies the problem. it('puts the lotion in the basket', () => { const wrapper = mount( <MemoryRouter> <Foo /> </MemoryRouter> ) wrapper.state('name') // this returns null! We are accessing the MemoryRouter's state, which isn't what we want

Using Jest to mock a React component with props

旧街凉风 提交于 2019-11-29 06:09:01
问题 I have a React component which contains some other components that depend on access to a Redux store etc., which cause issues when doing a full Enzyme mount. Let's say a structure like this: import ComponentToMock from './ComponentToMock'; <ComponentToTest> ...some stuff <ComponentToMock testProp="This throws a warning" /> </ComponentToTest> I want to use Jest's .mock() method to mock out the sub-component, so that it is not a concern for the test. I'm aware that I can mock out a straight

How to test style for a React component attribute with Enzyme

穿精又带淫゛_ 提交于 2019-11-29 05:24:55
I am trying to test a style attribute for a React component. What is the best way to get style params in the test? At this moment, my best option is to test if the HTML includes the string, but I think there is a better option. Case: it('Should render large image when desktop', () => { const dummyUrl = 'http://dummyUrl'; const wrapper = shallow( <MockedStore initialState={{ app: fromJS({ browser: { desktop: true } }), }} > <LandingHero bigImage={dummyUrl} /> </MockedStore> ); }); The Component to test is: // @flow import React, { Component } from 'react'; import gc from 'styles/core.scss';

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

对着背影说爱祢 提交于 2019-11-28 21:23:27
I want to test whether my React component can use FileReader to import the contents of a user-selected file from an <input type="file"/> element. My code below shows a working component with a broken test. In my test I'm attempting to use a blob as a substitute for the file because blobs can also be "read" by FileReader . Is that a valid approach? I also suspect that part of the issue is that reader.onload is asynchronous and that my test needs to take this into consideration. Do I need a promise somewhere? Alternatively, do I perhaps need to mock FileReader using jest.fn() ? I would really

Components using Date objects produce different snapshots in different timezones

你。 提交于 2019-11-28 19:09:20
I'm using Enzyme with enzyme-to-json to do Jest snapshot testing of my React components. I'm testing shallow snapshots of a DateRange component that renders a display field with the current range (e.g. 5/20/2016 - 7/18/2016 ) and two DateInput components that allow selecting a Date value. This means that my snapshot contains the Date s I pass to the component both in the DateInput props and in a text representation it resolves itself. In my test I'm creating some fixed dates using new Date(1995, 4, 23) . When I run my test in different timezones, this produces different snapshots , because the

how to change jest mock function return value in each test?

喜夏-厌秋 提交于 2019-11-28 18:40:41
问题 I have a mock module like this in my component test file jest.mock('../../../magic/index', () => ({ navigationEnabled: () => true, guidanceEnabled: () => true })); these functions will be called in render function of my component to hide and show some specific feature. I want to take a snapshot on different combinations of the return value of those mock functions. for suppose I have a test case like this it('RowListItem should not render navigation and guidance options', () => { const wrapper

Enzyme - How to access and set <input> value?

柔情痞子 提交于 2019-11-28 18:06:09
I'm confused about how to access <input> value when using mount . Here's what I've got as my test: it('cancels changes when user presses esc', done => { const wrapper = mount(<EditableText defaultValue="Hello" />); const input = wrapper.find('input'); console.log(input.render().attr('value')); input.simulate('focus'); done(); }); The console prints out undefined . But if I slightly modify the code, it works: it('cancels changes when user presses esc', done => { const wrapper = render(<EditableText defaultValue="Hello" />); const input = wrapper.find('input'); console.log(input.val()); input

jest + enzyme, using mount(), document.getElementById() returns null on component which appear after _method call

纵然是瞬间 提交于 2019-11-28 08:22:19
I faced a problem with my jest+enzyme mount() testing. I am testing a function, which switches displaying components. Switch between components: when state.infoDisplayContent = 'mission' a missionControl component is mounted, when state.infoDisplayContent = 'profile' - other component steps in: _modifyAgentStatus () { const { currentAgentProfile, agentsDatabase } = this.state; const agentToMod = currentAgentProfile; if (agentToMod.status === 'Free') { this.setState({ infoDisplayContent: 'mission' }); agentToMod.status = 'Waiting'; } else if (agentToMod.status === 'Waiting') { const

Jest mock async calls inside react component

拈花ヽ惹草 提交于 2019-11-28 05:53:13
问题 I'm new to jest/enzyme and am trying to mock a call to an aync function that returns a Promise, the call is made inside a react component in the componentDidMount method. The test is attempting to test that componentDidMount sets the array returned by the Promise in the state. The issue I'm having is that the test finishes and passes before the array is added to the state. I am trying to use the 'done' callback to have the test wait until the promise resolves but this doesn't seem to work. I