<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Kyle Tozer</title>
    <description>The latest articles on DEV Community by Kyle Tozer (@kyletozer).</description>
    <link>https://dev.to/kyletozer</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F217301%2F3e4d5db2-e697-4d59-b3c8-244699f9bb3e.png</url>
      <title>DEV Community: Kyle Tozer</title>
      <link>https://dev.to/kyletozer</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kyletozer"/>
    <language>en</language>
    <item>
      <title>React Jest Testing Help</title>
      <dc:creator>Kyle Tozer</dc:creator>
      <pubDate>Tue, 31 Dec 2019 17:52:05 +0000</pubDate>
      <link>https://dev.to/kyletozer/react-jest-testing-help-2ce6</link>
      <guid>https://dev.to/kyletozer/react-jest-testing-help-2ce6</guid>
      <description>&lt;p&gt;Hi everyone,&lt;/p&gt;

&lt;p&gt;At the moment I am trying to test a method on a React component that contains some async logic. The method is called on a child component but it method on the child also calls a method on a parent (A Context API Provider). The method on the parent returns a promise, however the method does not resolve in the child. Is there a way I can get this method to complete in my Jest test? Am I going about testing completely wrong? I want the method to resolve so I can assert that the newItem property in Home.js has been reset. Happy New Year!&lt;/p&gt;

&lt;p&gt;Home.test.js&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react';
import { mount } from 'enzyme';
import App from '../App';
import Provider from '../components/Provider';


test('should add to the provider list', () =&amp;gt; {
  const wrapper = mount(
    &amp;lt;Provider&amp;gt;
      &amp;lt;App/&amp;gt;
    &amp;lt;/Provider&amp;gt;
  );

  const child = wrapper.find('Home');
  const spy = jest.spyOn(child.instance(), 'addToList');

  child.setState({ newItem: { name: 'Cheese' } });
  wrapper.find('form').simulate('submit');

  expect(wrapper.state('list')).toHaveLength(1);
  expect(spy).toHaveBeenCalled();
});
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;component method in Home.js&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;addToList(event) {
    event.preventDefault();

    const { newItem } = this.state;

    if (!newItem.name) return;

    this.context.addToList(newItem)
      .then(() =&amp;gt; {
        this.setState(() =&amp;gt; ({
          newItem: { name: '' }
        }));
      })
      .catch(err =&amp;gt; {
        //
      });
  }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;component method in Provider.js&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;addToList(newItem) {
    return new Promise(resolve =&amp;gt; {
      this.setState(state =&amp;gt; {
        state.list.push(newItem);
        return state;
      }, resolve);
    });
  }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



</description>
      <category>react</category>
      <category>jest</category>
      <category>async</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
