Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

mock createRef jest react functional component

import React, { useRef } from 'react';
import { shallow } from 'enzyme';
import Child2 from './';

jest.mock('react', () => {
  const originReact = jest.requireActual('react');
  const mUseRef = jest.fn();
  return {
    ...originReact,
    useRef: mUseRef,
  };
});

describe('61782695', () => {
  it('should pass', () => {
    const mRef = { current: { offsetWidth: 100 } };
    useRef.mockReturnValueOnce(mRef);
    const wrapper = shallow(<Child2></Child2>);
    expect(wrapper.find('#myDiv').text()).toBe('123');
    expect(wrapper.find('p').text()).toBe('Div width is: 100');
  });

  it('should pass - 2', () => {
    const mRef = { current: { offsetWidth: 300 } };
    useRef.mockReturnValueOnce(mRef);
    const wrapper = shallow(<Child2></Child2>);
    expect(wrapper.find('#myDiv').text()).toBe('ABC');
    expect(wrapper.find('p').text()).toBe('Div width is: 300');
  });

  it('should pass - 3', () => {
    const mRef = {};
    useRef.mockReturnValueOnce(mRef);
    const wrapper = shallow(<Child2></Child2>);
    expect(wrapper.find('#myDiv').text()).toBe('123');
    expect(wrapper.find('p').text()).toBe('Div width is: ');
  });
});
Comment

PREVIOUS NEXT
Code Example
Javascript :: append item in treeview vuetify 
Javascript :: Replace symbol if it is preceded and followed by a word character js 
Javascript :: webpac-merge 
Javascript :: node_modules/metro/src/lib/attachWebsocketServer.js 
Javascript :: javascript global function 
Javascript :: javascript format time from number 
Javascript :: jquery append text for 5 seconds 
Javascript :: JavaScript Code to Perform GCD using Recursion 
Javascript :: nest js global endpoint 
Javascript :: parentnode javascript 
Javascript :: angular print html 
Javascript :: mongoose autoincrement 
Javascript :: [JsonConverter(typeof(StringEnumConverter))] on list of enums 
Javascript :: url fetch app pass payload and headers 
Javascript :: jquery slider get value on change 
Javascript :: button remove class jquery 
Javascript :: display object in array 
Javascript :: install ejs 
Javascript :: column to comma separated string in mongodb 
Javascript :: mongoose cursor eachasync 
Javascript :: duplicate characters in a string javascript 
Javascript :: how to create instance of class in javascript 
Javascript :: javascript scale values 
Javascript :: str_limit function filter vuejs 
Javascript :: mern heroku Error: ENOENT: no such file or directory 
Javascript :: picture in picture remove from videojs 
Javascript :: send message to user facebook game 
Javascript :: if element in dict javascript 
Javascript :: identity-obj-proxy not working 
Javascript :: material icons angular 
ADD CONTENT
Topic
Content
Source link
Name
1+6 =