3205

Test a form in React with jest and enzyme

<h3>Question</h3>

I'm totally lost trying to test a simple form made with React, how do I know if the submit button is working? After some research I thought that the way to do it is to make a mock function and then check if it was called but I'm pretty sure I'm doing it completely wrong.

onObjSubmit(event){ event.preventDefault() ..... (fetch something) } render(){ return ( <form id="myForm" onSubmit={event => this.onObjSubmit(event)}> <input type="text" id="name" />
<input type="text" id="last_name" />
<input type="submit" value="submit"> </form> ); }

And the test:

it('Test', () => { const wrapper = shallow(<TestComp />); const fn = jest.fn(); wrapper.instance().onObjSubmit = fn; wrapper.update(); wrapper.find('#myForm').simulate('submit'); expect(fn).toBeCalled(); });

Can someone please point me in the right direction?


<h3>Answer1:</h3>

You're not the only one who feels lost with this one. This thread on Enzyme's issues page has a lot of suggested approaches to solving this problem.

来源:https://stackoverflow.com/questions/47316691/test-a-form-in-react-with-jest-and-enzyme

Recommend

  • Algorithm to select a set of numbers to reach a minimum total
  • How to get javascript generated title tooltip for SVG to show up
  • Android GoogleAPIClient - NoClassDefFoundError: com/google/android/gms/internal/zzsa
  • Grab Audio Sessions as they appear in the windows audio mixer (C# or C++)
  • How well does Entity Framework 6 support .NET 4.0?
  • Executing JavaScript function in Code behind — How to Get the Return Value in Code Behind
  • Get All IP and Mac Address in lan
  • maximizing profit for given stock data via DP
  • Two hosts in jdbc url
  • Parse and remove special characters in java regex
  • Unknown type name with typedef struct in C
  • How to layout? (JFrame, JPanel, etc.)
  • why 'read' command in shell script is missing initial characters? [duplicate]
  • Laravel 5 - Cache remember doesn't work
  • create circular Auto Horizontal Scroll View?
  • MayAVI install on Python 3.6 [duplicate]
  • Extracting a small subset of data from XMLs
  • Swift manually rotate view controller
  • Julia 1.0 UndefVarError - Scope of Variable
  • python selenium scraping tbody
  • Annotate objects in a queryset with next and previous object ids
  • How to merge objects within array based on attribute
  • Google Spreadsheet Script to Blink a range of Cells
  • how to get the location(lat/lng) on google maps v3 from the location(x,y)
  • Update cell query for Excel ADO from Delphi
  • ssh remote server login script
  • Why my AngularJS async test in Jasmine 1.3.x is not working?
  • How to call different template for different category archive page in woocommerce
  • customize soft keyboard key preview
  • Cross compile glibc for arm, got undefined reference to some unwind functions
  • Using Service Component Runtime
  • How to use FirstOrDefault inside Include
  • How do I use TagLib-Sharp to write custom (PRIV) ID3 frames?
  • WPF custom control and direct content support
  • PHP Permalinks.. how to change?
  • media foundation H264 decoder not working properly
  • CAS 4 - Not able to retrieve the LDAP groups after successful authentication
  • Running R's aov() mixed effects model from Python using rpy2
  • Access to a Matlab gui from the web
  • JavaScript RegExp Replace