88826

Writing test cases using Jest and Enzyme

Question:

I am working on writing test cases for a project. I am writing the test for my Container. The container has a function which is as shown below:

getContactDetails = (reqObject) => { app.outageCenterService.getContact(reqObject).then( response => { app.logger.getLogger().info('Below is the response...'); app.logger.getLogger().info(this.state.contactDetails); this.setState({contactDetails: response.contactDetails},()=>{}); if (this.state.contactDetails.isContactPresent) { this.setState({ isVisible: true }); } else { this.setState({ isVisible: false }); } }, reject => { app.logger.getLogger().info(reject); } ); }

While running the test,in the function,the line app.outageCenterService.getContact(reqObject) throws an error saying TypeError: Cannot read property 'getContact' of undefined. I understand its because outageCenterService is globally defined and jest/enzyme is not able to find it. But I don't know how to solve this issue.

My test looks something like this:

describe('test the OutageAlert Component', () => { let outageAlert, errorHandlerFn; errorHandlerFn=jest.fn(); getContactFn=jest.fn(); outageAlert = shallow(<OutageAlertComponent errorHandler={errorHandlerFn} getContact={getContactFn} />); });

Can anyone please help me with this on how to write the test case for this scenario?

Answer1:

You could consider use the globals config

<a href="http://facebook.github.io/jest/docs/api.html#globals-object" rel="nofollow">http://facebook.github.io/jest/docs/api.html#globals-object</a> or create a file and

and create a mock in jest for you global, then you can access it from your test.

Alternatively you could have your container accepting an argument for your contact

getContactDetails = (reqObject, contact) => { ...}

so you can pass it in your test and wherever you container is being used.

Recommend

  • Use Linq or C# to parse Json
  • How to print the webelement in protractor?
  • Keyboard and getting up state on iPhone
  • Is it ok to send normal messages via Interface Builder's binding’s model key path?
  • when keyboard is show, scrolling is limited
  • MySqlCommand Parameter not Working
  • Spotfire IronPython Script to Scroll through a Filter and Update a Visualization Each Step (Play But
  • ActivityIndicator in Xamarin.Forms
  • how do I collapse parent content if all children are collapsed
  • Contextual ActionBar hides when I click on hardware backbutton and the keyboard is out
  • How to assign a prop value to a state in react
  • How to orderByValue onSnapshot using Firestore
  • Mongoose - can't access object properties?
  • Configure SwiftMailer “Local Domain” setting easily, in Symfony 2
  • React 16 Error Boundary component (using componentDidCatch) shows uncaught error
  • Cannot read property setState of undefined in axios callback
  • Getting the result of the promise returned by an asyncValidator
  • TextPad “find in files” not matching on simple OR regex
  • Passing Props from State to Child
  • How can I get a specific version of a dataset row?
  • Center align outputs in ipython notebook
  • How to run “Deployd” on port 80 instead of port 5000 in webserver.
  • FB SDK and cURL: Unknown SSL protocol error in connection to graph.facebook.com:443
  • Using $this when not in object context
  • How do I fake an specific browser client when using Java's Net library?
  • How reduce the height of an mschart by breaking up the y-axis
  • Perl system calls when running as another user using sudo
  • ActionScript 2 vs ActionScript 3 performance
  • ORA-29908: missing primary invocation for ancillary operator
  • How can I estimate amount of memory left with calling System.gc()?
  • Apache 2.4 - remove | delete | uninstall
  • How to disable jQuery.jplayer autoplay?
  • How to stop GridView from loading again when I press back button?
  • Bitwise OR returns boolean when one of operands is nil
  • sending mail using smtp is too slow
  • Busy indicator not showing up in wpf window [duplicate]
  • costura.fody for a dll that references another dll
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Binding checkboxes to object values in AngularJs
  • How can I use `wmic` in a Windows PE script?