react enzyme hasClass example

Example 1: enzyme hasclass example

describe("PageTitle", () => {
  
  it("matches snapshot", () => {
    const tree = renderer.create(<PageTitle pageTitle="Tips" />).toJSON();
    expect(tree).toMatchSnapshot();
  });

  it("should display the title passed into the property", () => {
    const wrapper = shallow(<PageTitle pageTitle="My App"></PageTitle>);
    expect(wrapper.hasClass('pageTitle')).toEqual(true);
    expect(wrapper.render().text()).toEqual('My App');
  });
  
});

Example 2: enzyme hasclass example

// dom.js
import React from 'react'

const DomTest = () => (
  <div>
    <h1 className='header'>
      <span className='sub-header'>Hello World</span>
    </h1>
  </div>
)

export default DomTest

// dom.test.js
test('find element in DomTest Component by class using hasClass', () => {
  expect(wrapper.find('h1').hasClass('header')).toBeTruthy()
  expect(wrapper.find('h1 > span').at(0).hasClass('sub-header')).toBeTruthy()
  expect(wrapper.find('h1').children().find('span').hasClass('sub-header')).toBeTruthy()
  expect(wrapper.find('h1').children().at(0).hasClass('sub-header')).toBeTruthy()
  expect(wrapper.find('h1').childAt(0).hasClass('sub-header')).toBeTruthy()
})