1

I'm currently encountering this error in my unit testing wherein I cannot find the element with an specific classname after I simulate the form submit:

HTML Element in inspect element:

<div class="ant-form-item-control has-error">
  <input type="text" class="ant-input ant-input-lg addSiteBasics_businessName" value="" placeholder="English Business Name" id="businessName">
  <div class="ant-form-explain">Please add the name of your business.</div>
</div>

Test Script:

describe('given business name is empty', () => {
    it('display error if business name empty', () => {
      const form = wrapper.find('form').first();
      form.simulate('submit');
      expect(wrapper.find('div.has-error #businessName').exists()).toBe(true);
    })
  });

This is the error that displays in my terminal:

expect(received).toBe(expected)

    Expected value to be (using ===):
      true
    Received:
      false
Star
  • 3,222
  • 5
  • 32
  • 48
Sydney Loteria
  • 10,171
  • 19
  • 59
  • 73
  • you can try debugging your test by console.log(wrapper.debug()) before and after form submission. See what is happening. – dubes Dec 22 '17 at 10:12
  • Can you show your `react` component that you are testing? – The Reason Dec 22 '17 at 10:12
  • @dubes it seems that the component is the one that render, that's why I cannot traverse the dom element of the businessName textfield component. Is there that I can generate the html of the component? – Sydney Loteria Dec 22 '17 at 10:15
  • @SydneyLoteria you can do that (take a look at this answer: https://stackoverflow.com/a/38747914/1695393 ), however, mount and render slow down your tests and mostly shallow should suffice (i..e you test your react component and not the markup it generates) – dubes Dec 22 '17 at 10:29
  • As far as creating HTML/Dom elements, check this answer: https://stackoverflow.com/questions/46057037/creating-dom-elements-for-javascript-testing – Eliâ Melfior Dec 22 '17 at 11:12
  • I think saw the issue, the form submit methods is from the parent component while unit testing that I'm executing is from the child component. – Sydney Loteria Dec 22 '17 at 11:24

0 Answers0