showing results for - "mock createref jest react functional component"
Anaïs
23 May 2018
1import React, { useRef } from 'react';
2import { shallow } from 'enzyme';
3import Child2 from './';
4
5jest.mock('react', () => {
6  const originReact = jest.requireActual('react');
7  const mUseRef = jest.fn();
8  return {
9    ...originReact,
10    useRef: mUseRef,
11  };
12});
13
14describe('61782695', () => {
15  it('should pass', () => {
16    const mRef = { current: { offsetWidth: 100 } };
17    useRef.mockReturnValueOnce(mRef);
18    const wrapper = shallow(<Child2></Child2>);
19    expect(wrapper.find('#myDiv').text()).toBe('123');
20    expect(wrapper.find('p').text()).toBe('Div width is: 100');
21  });
22
23  it('should pass - 2', () => {
24    const mRef = { current: { offsetWidth: 300 } };
25    useRef.mockReturnValueOnce(mRef);
26    const wrapper = shallow(<Child2></Child2>);
27    expect(wrapper.find('#myDiv').text()).toBe('ABC');
28    expect(wrapper.find('p').text()).toBe('Div width is: 300');
29  });
30
31  it('should pass - 3', () => {
32    const mRef = {};
33    useRef.mockReturnValueOnce(mRef);
34    const wrapper = shallow(<Child2></Child2>);
35    expect(wrapper.find('#myDiv').text()).toBe('123');
36    expect(wrapper.find('p').text()).toBe('Div width is: ');
37  });
38});
39