enzyme

How to test async function with spyOn?

半腔热情 提交于 2021-02-19 02:57:09
问题 I am trying to test an async function in a react native app. class myClass extends React.Component { ... closeModal = async () => { if (someCondition) { await myFunction1(); } else { await myFunction2(); } this.props.navigation.state.params.onGoBack(); this.props.navigation.navigate('Main'); }; ... } This is my test: const navigation = { navigate: jest.fn(), state: { params: { onGoBack: jest.fn() } }, }; const renderComponent = overrides => { props = { navigation, ...overrides, }; return

How to test async function with spyOn?

别来无恙 提交于 2021-02-19 02:57:09
问题 I am trying to test an async function in a react native app. class myClass extends React.Component { ... closeModal = async () => { if (someCondition) { await myFunction1(); } else { await myFunction2(); } this.props.navigation.state.params.onGoBack(); this.props.navigation.navigate('Main'); }; ... } This is my test: const navigation = { navigate: jest.fn(), state: { params: { onGoBack: jest.fn() } }, }; const renderComponent = overrides => { props = { navigation, ...overrides, }; return

What is the purpose of `beforeEach` global in Jest?

狂风中的少年 提交于 2021-02-18 10:16:06
问题 I always find Jest-Enzyme test cases starting with a beforeEach global that resembles like the following: describe('TEST BLOCK' () => { let wrapper; beforeEach(() => { wrapper = shallow(<Component />); )); )); The function inside the beforeEach global runs before each test inside the TEST BLOCK describe block. In this case, it shallow renders the Component and assigns to wrapper before running each test. I am not quite sure why do we do this in the first place. Are we not deliberatly slowing

Enzyme: simple .to.have not working

痴心易碎 提交于 2021-02-16 13:59:27
问题 From the docs something like this should be possible: const wrapper = mount(<Foo name="foo" />); expect(wrapper.find('.foo')).to.have.length(1); But in my case this throws an error saying cannot read property have of undefined. Using this works though: expect(wrapper.find('.foo').length).toBe(1); 回答1: Your expect function is probably expect-enzyme, which has camelCase methods ( toBe() ), .to.have is dot separated expect notation, as seen in these docs : http://chaijs.com/api/bdd/ vs the

Enzyme: simple .to.have not working

守給你的承諾、 提交于 2021-02-16 13:59:03
问题 From the docs something like this should be possible: const wrapper = mount(<Foo name="foo" />); expect(wrapper.find('.foo')).to.have.length(1); But in my case this throws an error saying cannot read property have of undefined. Using this works though: expect(wrapper.find('.foo').length).toBe(1); 回答1: Your expect function is probably expect-enzyme, which has camelCase methods ( toBe() ), .to.have is dot separated expect notation, as seen in these docs : http://chaijs.com/api/bdd/ vs the

Testing component created using styled component with jest and enzyme

微笑、不失礼 提交于 2021-02-11 18:16:10
问题 I have a few tags created using styled components lib: component.js .... const NoJobs = styled.h1` text-align: center; `; .... <div> <NoJobs> No Jobs Found!!! </NoJobs> </div> I want to test this component, below is my test case: component.test.js describe('<JobList />', () => { let wrapper; beforeEach(() => { wrapper = shallow(<JobList />); }); it('should show "no data found" when there is no data', () => { expect(wrapper.contains(<NoJobs>No Jobs Found!!!</NoJobs>)).toEqual(true); }); });

Jest Enzyme impossible to locate imported function in mount()

我是研究僧i 提交于 2021-02-11 14:34:23
问题 I have to mount a component that uses a function from library. The function is used in the componentDidMount cycle. Everything looks somewhat like this: import * as React from 'react'; import * as dayjs from 'dayjs'; class MyComponent extends React.Component { constructor(props) { super(props); this.slider = null; } componentDidMount() { this.setupValues(); } setupValues() { this.slider = { ..., format: dayjs(val).format(...) } } render() {...} } Now the wrapper that I am trying to use in my

Test coverage very low due to these code "campaignGroupListing.findIndex((item) => item.id === parseInt(id)); campaignGroupListing.splice(index, 1);

左心房为你撑大大i 提交于 2021-02-11 12:22:39
问题 i am new in react can any one help us to write unit test code for below react code & basically i write test case for this below code but statement & functions coverage very low due to these code campaignGroupListing its is arrray of object in this code i find matching data and updated id record append in a new array ."campaignGroupListing.findIndex((item) => item.id === parseInt(id)); campaignGroupListing.splice(index, 1); campaignGroupListing.splice(index, 0, newArr); " import React, {

How to test mapStateToProps using React Redux and Jest?

a 夏天 提交于 2021-02-11 06:11:51
问题 When I create a test for my connected React component where I want to test the mapStateToProps logic I run into a problem which I'm not sure how to solve. Error message Expected: 1 Received: undefined 24 | it('should show previously rolled value', () => { 25 | // test that the state values were correctly passed as props > 26 | expect(wrapper.props().lastRolledNumber).toBe(1); When I check the wrapper.props() it only returns the store object and no properties. To make sure it's not my code I

How to test mapStateToProps using React Redux and Jest?

♀尐吖头ヾ 提交于 2021-02-11 06:10:42
问题 When I create a test for my connected React component where I want to test the mapStateToProps logic I run into a problem which I'm not sure how to solve. Error message Expected: 1 Received: undefined 24 | it('should show previously rolled value', () => { 25 | // test that the state values were correctly passed as props > 26 | expect(wrapper.props().lastRolledNumber).toBe(1); When I check the wrapper.props() it only returns the store object and no properties. To make sure it's not my code I