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()
})

0
6
JeffDror 75 points

                                    describe(&quot;PageTitle&quot;, () =&gt; {
  
  it(&quot;matches snapshot&quot;, () =&gt; {
    const tree = renderer.create(&lt;PageTitle pageTitle=&quot;Tips&quot; /&gt;).toJSON();
    expect(tree).toMatchSnapshot();
  });

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

0
0
3.75
8
Nchagnet 85 points

                                    // dom.js
import React from 'react'

const DomTest = () =&gt; (
  &lt;div&gt;
    &lt;h1 className='header'&gt;
      &lt;span className='sub-header'&gt;Hello World&lt;/span&gt;
    &lt;/h1&gt;
  &lt;/div&gt;
)

export default DomTest

// dom.test.js
test('find element in DomTest Component by class using hasClass', () =&gt; {
  expect(wrapper.find('h1').hasClass('header')).toBeTruthy()
  expect(wrapper.find('h1 &gt; 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()
})

3.75 (8 Votes)
0
Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source